I have uploaded a file from my system, I have converted the file in bytes. Now I want to save that file at server. How can I do this. I have searched through the internet but found nothing. Is there any solution of this problem? I am uploading file using JSP.
Asked
Active
Viewed 6,056 times
1
-
1http://bit.ly/1iDibof – vanje Mar 27 '14 at 11:12
-
Open a FileOutputStream on a disk location and write the byte array :) – robermann Mar 27 '14 at 11:12
-
use dropzone if u want.. – Santino 'Sonny' Corleone Mar 27 '14 at 11:13
1 Answers
1
If you are talking about UploadedFile, here is how I achieved this after a huge internet search:
/**
* Save uploaded file to server
* @param path Location of the server to save file
* @param uploadedFile Current uploaded file
*/
public static void saveUploadedFile(String path, UploadedFile uploadedFile) {
try {
//First, Generate file to make directories
String savedFileName = path + "/" + uploadedFile.getFileName();
File fileToSave = new File(savedFileName);
fileToSave.getParentFile().mkdirs();
fileToSave.delete();
//Generate path file to copy file
Path folder = Paths.get(savedFileName);
Path fileToSavePath = Files.createFile(folder);
//Copy file to server
InputStream input = uploadedFile.getInputstream();
Files.copy(input, fileToSavePath, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
}
}

Bahadir Tasdemir
- 10,325
- 4
- 49
- 61
-
So after using the method `copy()` how can we check if the file is in the server? – Peter Haddad Dec 27 '18 at 15:48
-