I store files uploaded by users as follows:
InputStream inputStream = null;
OutputStream outputStream = null;
MultipartFile file = uploadedFile.getFile();
String fileName = file.getOriginalFilename();
String path ="C:/apache-tomcat-8.0.18/uploads/images/";
try {
inputStream = file.getInputStream();
File newFile = new File(path +fileName);
if (!newFile.exists()) {
System.out.println(newFile.getAbsolutePath());
newFile.createNewFile();
}
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
outputStream.close();
}
What must be the path where the uploaded files should be saved? I want to store images and display them in JSP pages.
I initially stored the files in the webapp/images folder of my Maven project. But when the app is redeployed the uploaded files are also getting deleted. How can I solve this problem?
When I upload in C:/apache-tomcat-8.0.18/uploads/images/ I can't get images to display in my JSP page. Get error Not allowed to load local resource.