0

I have a folder inside resources folder where I want to upload media files. But I am constantly getting 'No file or directory found' error.

Here is what I have in my controller

@Autowired
ServletContext servletContext;

@RequestMapping(value = "/uploads", method = RequestMethod.POST)
public String insert(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

    // String path = new
    // ClassPathResource("/src/main/resources/uploads").getPath();

    // FileCopyUtils.copy(file.getBytes(), new File(path));

    String webappRoot = servletContext.getRealPath("/");
    String relativeFolder = File.separator + "resources" + File.separator + "uploads" + File.separator;
    System.out.println(webappRoot);
    System.out.println(relativeFolder);

    String filename = webappRoot + relativeFolder + file.getOriginalFilename();

    FileCopyUtils.copy(file.getBytes(), new File(filename));

    return "uploaded successfully";
}

And here is the error I am getting constantly

SEVERE: Servlet.service() for servlet [api] in context with path [/ek-service] threw exception
java.io.FileNotFoundException: /home/ekbana/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ek-service/resources/uploads/2.jpg (No such file or directory)

I have searched all over the web tried ResourceLoader and ClassPath. But still no success figuring it out.

user3127109
  • 3,421
  • 8
  • 24
  • 33
  • 1
    You cannot, nor should you want that. The `src/main/resources` is only there to denote non java sources. When compiling and creating a war there is no more resources folder. It is actually the root of the classpath and you won't be able to upload to that. – M. Deinum May 02 '16 at 11:52
  • Then what should I do? I have been stucked here since early morning! – user3127109 May 02 '16 at 11:54
  • Use a path somewhere on disk. – M. Deinum May 02 '16 at 11:55
  • Sorry I didn't quite get you! Did u mean like path = "D:/Test/Upload/"; – user3127109 May 02 '16 at 11:56
  • go through this answer i gave on a similar question... [link](http://stackoverflow.com/a/36714806/4626402) – Abhishek May 02 '16 at 12:03
  • Isn't it possible to upload images in directory of project itself? How would it work on live project. – user3127109 May 02 '16 at 12:05
  • You upload to an external storage disk or db or ... You don't upload to a project directory. Where should the file go if you have 10 instances of your app? Also when you redeploy what happens on the server is throw away old war directory, unpack new application, launch. Then poof gone are your uploads... Upload to a file path on disk not inside your app. Search SO as that question has been answered before... – M. Deinum May 02 '16 at 12:08
  • use the same folder structure in live project.... System.getenv("HOME"); gives you a the home path... always make sure your uploads folder is in the home path and dont forget about permissions of the folder – Abhishek May 02 '16 at 12:10
  • But how to provide permission to the folder of my home directory? I tried chmod 777 images. But still getting permission denied. – user3127109 May 02 '16 at 12:16
  • sudo chmod -R 777 images – Abhishek May 02 '16 at 12:18
  • I tried the command and I have used this code in my controller. String path = "/home/images"; String filename = webappRoot + relativeFolder + file.getOriginalFilename(); – user3127109 May 02 '16 at 12:23

1 Answers1

2

Resources should be considered read-only, they are not accessible through the file system, but are embedded in the war/jar when you deploy an application.

You must write the file to a path on the file system that exists and that your application has write access to.

You can write to a temporary file, it has the benefit that you most certainly will have write access to it. Useful for testing until you figure out a permanent location to store the file, or for cases when permanent storage is not necessary. See example: http://www.mkyong.com/java/how-to-create-temporary-file-in-java/

Jakabov
  • 106
  • 1
  • 8