5

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

Anthony
  • 33,838
  • 42
  • 169
  • 278
  • I am a little confused with your statement CommonsMultipartFile file = request.(myfile). The fileupload api will return a list of items. List items = fileUpload.parseRequest(request); Is that what you are referring to ? – Zenil May 22 '13 at 02:28
  • @Zenil sorry, that should be `request.getFile('myfile')` – Anthony May 22 '13 at 03:41
  • You can use `RandomAccessFile`. Here is the example. https://opencast.jira.com/svn/MH/contrib/BigFileUploader/Upplet/src/upplet/Uploader.java – Ashish Pancholi May 27 '13 at 04:51

2 Answers2

2

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.
Glen Best
  • 22,769
  • 3
  • 58
  • 74
  • bad example dude. it gives me no idea of what actual values to put in the params. – Or Gal Oct 24 '13 at 13:05
  • Text added at bottom. If implementing this, I *still* expect you to glance at javadocs to understand params & high level behaviour. Note Q is specific - has prexisting A.C. servlet file upload solution & part of API solution, needs to obtain CommonsMultipartFile added. Replacing params with numbers and strings would probably tell you less. If you are working in this area, common practice is to 'chip in' and add new information & answers on here. (BTW, friendly tip: 'dude' might be better used on instagram, chat rooms, etc, or with someone you've met :-) ) – Glen Best Oct 24 '13 at 20:37
0

the above one https://stackoverflow.com/a/16682983/688810 has exception:

Cannot invoke "org.apache.commons.io.output.DeferredFileOutputStream.isInMemory()" because "this.dfos" is null

solution: java.lang.NullPointerException while creating DiskFileItem

zd987
  • 1
  • 1
  • 1
    Your solution is just a link? Please have a look at https://stackoverflow.com/help/how-to-answer and get started with writing awesome answers. – Rishabh Kumar Feb 23 '21 at 05:39