How to make CommonsMultipartFile from absolute fil

2020-07-13 08:52发布

问题:

I'm creating an API for my application. In the GUI browser based application the file is uploaded via a form submission. So I simply do CommonsMultipartFile file = request.getFile(myfile). However, the API will provide an absolute path to the file as a string rather than uploading the file. My application will have access to this absolute path.

So that I don't have to change the underlying methods of my application (which accept the common interface MultiPartFile For API purposes, I would like to read the file from this absolute path and create a CommonsMultipartFile object which can be passed around to the methods that I am already using for GUI browser based application.

How can I do this? Constructor to CommonsMultipartFile accepts a FileItem

回答1:

This is API-specific code. i.e. not the usual file upload code.

Usual steps would be to:

  1. construct FileItemFactory
  2. construct ServletFileUpload, passing it the factory
  3. call ServletFileUpload.parseRequest(request)

This answer replaces 2 & 3 with logic independent of servlets - it avoids using ServletFileUpload (servlet-specific) and its ancestor FileUpload (so as to control the file location with an absolute path name). Note: (3) usually examines HTTP request parameters to determine lower-level parameters that are passed to FileItemFactory.createItem - these parameters are instead provided manually, and then only used as informational metadata. Replacement for 2 & 3:

  • construct FileItem (via FileItemFactory.createItem - need to manually provide lower-level parameters, usually determined via ServletFileUpload.upload())
  • write to a specific file, with an absolute path
  • upload the file via MultipartFile

Requested code provided below. At the end it invokes common code - shared with Servlet upload.

// Initialise Apache Commons FileItemFactory for API use only
FileItemFactory fif = new DiskFileItemFactory(sizeThreshold, repositoryBaseDirFile);

// Create Apache Commons FileItem & write file at fullFilePathString into it
FileItem fi = fif.createItem(fieldName, contentType, isFormField, fileName);
fi.write(new java.io.File(new java.net.URI(fullFilePathString));

// Convert FileItem to Spring wrapper: CommonsMultipartFile
org.springframework.web.multipart.MultipartFile mf = new CommonsMultipartFile(fi);

// From here, reuse the same code as the servlet upload.  Operate only upon  
// Spring MultipartFile, but not ServletFileUpload, FileItemFactory etc...

Parameters:

  • fullFilePathString: absolute path (as String) where file will be uploaded
  • fieldName: name of field on the form

(Because ServletFileUpload & FileUpload are avoided, the following become metadata fields only, and are not used to control processing)

  • sizeThreshhold: memory size threshold in bytes (usually files smaller are uploaded using memory only and files larger are uploaded via disk - but this logic has files always uploaded via disk). Default = DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD.
  • repositoryBaseDireFile: usually the file upload 'temp' directory (as a File type), but this logic uses an absolute path to upload file
  • contentType: content type (MIME type) of field on the form (null if not multi-part form field)
  • isFormField: if plain form field, 'true', else false if multi-part field.
  • fileName: the name of the file - usually specified via form / client.