java - FilesAlreadyExistsException when using Files.copy() on File.createTempFile() -


i'm using jsf <h:inputfile> upload images server.

<h:form enctype="multipart/form-data">     <h:inputfile value="#{filehandlerbean.file}"/>     <h:commandbutton action="#{filehandlerbean.uploadimage()}"/> </h:form> 

i created virtual host /projectname/webapp/images. can create files folder.

private part file;  public string uploadimage(){     inputstream = null;     try {         string extension = filenameutils.getextension(file.getsubmittedfilename());         file tempfile = file.createtempfile("picture-", "."+extension, new file("/projectname/webapp/images"));         logger.getlogger("file size").warning(string.valueof(file.getsize()));         logger.getlogger("file extension").warning(extension);         = file.getinputstream();         logger.getlogger("stream available size").warning(string.valueof(is.available()));         files.copy(is, tempfile.topath());     } catch (ioexception ex) {         logger.getlogger(filehandlerbean.class.getname()).log(level.severe, null, ex);     } {         if( is!= null){             try {                 is.close();             } catch (ioexception ex) {                 logger.getlogger(filehandlerbean.class.getname()).log(level.severe, null, ex);             }         }     }     return "success"; } 

but of them empty , java.nio.file.filealreadyexistsexception everytime.

java.nio.file.filealreadyexistsexception: \projectname\webapp\images\picture-3433673623996534194.png     @ sun.nio.fs.windowsexception.translatetoioexception(windowsexception.java:81)     @ sun.nio.fs.windowsexception.rethrowasioexception(windowsexception.java:97)     @ sun.nio.fs.windowsexception.rethrowasioexception(windowsexception.java:102)     @ sun.nio.fs.windowsfilesystemprovider.newbytechannel(windowsfilesystemprovider.java:230)     @ java.nio.file.spi.filesystemprovider.newoutputstream(filesystemprovider.java:434)     @ java.nio.file.files.newoutputstream(files.java:216)     @ java.nio.file.files.copy(files.java:3016)     @ com.toolmanagement.backingbeans.filehandlerbean.uploadimage(filehandlerbean.java:41)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:483) 

i use logger check filesize, extension , streamsize, fine.

how caused , how can solve it?


update: added standardcopyoption.replace_existing files.copy() , works, still not solving problem. use createtempfile() create unique, random filenames. why file exists?

the file#createtempfile() indeed creates empty file. guarantee filename reserved , available use, hereby removing (very small) risk thread coincidentally concurrently generates same filename @ same moment.

you should indeed using standardcopyoption.replace_existing in files#copy(). flag not necessary in old fileoutputstream approach defaults overwriting file.

i gather got createtempfile() example this answer; has in meanwhile been updated fix oversight.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -