java - How to make CommonsMultipartFile from absolute file path? -


i'm creating api application. in gui browser based application file uploaded via form submission. commonsmultipartfile file = request.getfile(myfile). however, api provide absolute path file string rather uploading file. application have access absolute path.

so don't have change underlying methods of application (which accept common interface multipartfile api purposes, read file absolute path , create commonsmultipartfile object can passed around methods using gui browser based application.

how can this? constructor commonsmultipartfile accepts fileitem

this api-specific code. i.e. not usual file upload code.

usual steps to:

  1. construct fileitemfactory
  2. construct servletfileupload, passing factory
  3. call servletfileupload.parserequest(request)

this answer replaces 2 & 3 logic independent of servlets - avoids using servletfileupload (servlet-specific) , ancestor fileupload (so control file location absolute path name). note: (3) examines http request parameters determine lower-level parameters passed fileitemfactory.createitem - these parameters instead provided manually, , used informational metadata. replacement 2 & 3:

  • construct fileitem (via fileitemfactory.createitem - need manually provide lower-level parameters, determined via servletfileupload.upload())
  • write specific file, absolute path
  • upload file via multipartfile

requested code provided below. @ end invokes common code - shared servlet upload.

// initialise apache commons fileitemfactory api use fileitemfactory fif = new diskfileitemfactory(sizethreshold, repositorybasedirfile);  // create apache commons fileitem & write file @ fullfilepathstring fileitem fi = fif.createitem(fieldname, contenttype, isformfield, filename); fi.write(new java.io.file(new java.net.uri(fullfilepathstring));  // convert fileitem spring wrapper: commonsmultipartfile org.springframework.web.multipart.multipartfile mf = new commonsmultipartfile(fi);  // here, reuse same code servlet upload.  operate upon   // spring multipartfile, not servletfileupload, fileitemfactory etc... 

parameters:

  • fullfilepathstring: absolute path (as string) file uploaded
  • fieldname: name of field on form

(because servletfileupload & fileupload avoided, following become metadata fields only, , not used control processing)

  • sizethreshhold: memory size threshold in bytes (usually files smaller uploaded using memory , files larger uploaded via disk - logic has files uploaded via disk). default = diskfileitemfactory.default_size_threshold.
  • repositorybasedirefile: usually file upload 'temp' directory (as file type), logic uses absolute path upload file
  • contenttype: content type (mime type) of field on form (null if not multi-part form field)
  • isformfield: if plain form field, 'true', else false if multi-part field.
  • filename: name of file - specified via form / client.

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -