0

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.

manish
  • 19,695
  • 5
  • 67
  • 91
Nastya Gorobets
  • 197
  • 2
  • 10
  • 23
  • When your JSP gets rendered in the browser, what does the HTML code for images (``) look like? – manish May 25 '16 at 07:11

1 Answers1

0

The best way for you to store Files on the disk is using the Content Repository API for Java (JCR). I suggest Jackrabbit.

Take a look at this answer Recommended way to save uploaded files in a servlet application

Community
  • 1
  • 1
Muchael
  • 43
  • 7
  • I looked link There is `File uploads = new File("/path/to/uploads");` But I don't undastand what path I must write ? Is it be not in my webapp? – Nastya Gorobets May 24 '16 at 20:31