tomcat - Recommended way to save uploaded files in a servlet application -


i read here 1 should not save file in server anyway not portable, transactional , requires external parameters. however, given need tmp solution tomcat (7) , have (relative) control on server machine want know :

  • what best place save file ? should save in /web-inf/uploads (advised against here) or someplace under $catalina_base (see here) or ... ? javaee 6 tutorial gets path user (:wtf:). nb : file should not downloadable means.

  • should set config parameter detailed here ? i'd appreciate code (i'd rather give relative path - @ least tomcat portable) - part.write() looks promising - apparently needs absolute path

  • i'd interested in exposition of disadvantages of approach vs database/jcr repository one

unfortunately fileservlet @balusc concentrates on downloading files, while answer on uploading files skips part on save file.

a solution convertible use db or jcr implementation (like jackrabbit) preferable.

store anywhere in accessible location except of ide's project folder aka server's deploy folder, reasons mentioned in answer uploaded image available after refreshing page:

  1. changes in ide's project folder not reflected in server's work folder. there's kind of background job in ide takes care server's work folder synced last updates (this in ide terms called "publishing"). main cause of problem you're seeing.

  2. in real world code there circumstances storing uploaded files in webapp's deploy folder not work @ all. servers (either default or configuration) not expand deployed war file local disk file system, instead in memory. can't create new files in memory without editing deployed war file , redeploying it.

  3. even when server expands deployed war file local disk file system, newly created files lost on redeploy or simple restart, because new files not part of original war file.

it doesn't matter me or else on local disk file system saved, long do not ever use getrealpath() method. using method in any case alarming.

the path storage location can in turn definied in many ways. have yourself. perhaps confusion caused because somehow expected server automagically. please note @multipartconfig(location) not specify final upload destination, temporary storage location case file size exceeds memory storage threshold.

so, path final storage location can definied in either of following ways:

  • hardcoded:

    file uploads = new file("/path/to/uploads"); 
  • environment variable via set upload_location=/path/to/uploads:

    file uploads = new file(system.getenv("upload_location")); 
  • vm argument during server startup via -dupload.location="/path/to/uploads":

    file uploads = new file(system.getproperty("upload.location")); 
  • *.properties file entry upload.location=/path/to/uploads:

    file uploads = new file(properties.getproperty("upload.location")); 
  • web.xml <context-param> name upload.location , value /path/to/uploads:

    file uploads = new file(getservletcontext().getinitparameter("upload.location")); 
  • if any, use server-provided location, e.g. in jboss as/wildfly:

    file uploads = new file(system.getproperty("jboss.server.data.dir"), "uploads"); 

either way, can reference , save file follows:

file file = new file(uploads, "somefilename.ext");  try (inputstream input = part.getinputstream()) {     files.copy(input, file.topath()); } 

or, when want autogenerate unique file name prevent users overwriting existing files coincidentally same name:

file file = file.createtempfile("somefilename-", ".ext", uploads);  try (inputstream input = part.getinputstream()) {     files.copy(input, file.topath(), standardcopyoption.replace_existing); } 

how obtain part in jsp/servlet answered in how upload files server using jsp/servlet? , how obtain part in jsf answered in how upload file using jsf 2.2 <h:inputfile>? saved file?

note: not use part#write() interprets path relative temporary storage location defined in @multipartconfig(location).

see also:


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -