3

I am trying to upload a Multipart File using PostMan and getting errors. Here is the code and screenshots:

https://i.stack.imgur.com/RWPdp.jpg

https://i.stack.imgur.com/TtAvl.jpg

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadFileHandler(@RequestParam("name") String name,
        @RequestParam("name") MultipartFile file) {

    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            //String rootPath = System.getProperty("catalina.home");
            String rootPath = "C:\\Desktop\\uploads";
            File dir = new File(rootPath + File.separator + "tmpFiles");
            if (!dir.exists())
                dir.mkdirs();

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath()
                    + File.separator + name);
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();

            System.out.println("Server File Location="
                    + serverFile.getAbsolutePath());

            System.out.println("You successfully uploaded file=" + name);
        } catch (Exception e) {
            System.out.println("You failed to upload " + name + " => " + e.getMessage());
        }
    } else {
        System.out.println("You failed to upload " + name
                + " because the file was empty.");
    }
}
mw02
  • 31
  • 1
  • 1
  • 4

1 Answers1

3

You should have a thing like this:

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = "multipart/form-data")
    public void uploadFileHandler(@RequestParam("name") String name,
                                  @RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();

                // Creating the directory to store file
                //String rootPath = System.getProperty("catalina.home");
                String rootPath = "C:\\Users\\mworkman02\\Desktop\\uploads";
                File dir = new File(rootPath + File.separator + "tmpFiles");
                if (!dir.exists())
                    dir.mkdirs();

                // Create the file on server
                File serverFile = new File(dir.getAbsolutePath()
                        + File.separator + name);
                BufferedOutputStream stream = new BufferedOutputStream(
                        new FileOutputStream(serverFile));
                stream.write(bytes);
                stream.close();

                System.out.println("Server File Location="
                        + serverFile.getAbsolutePath());

                System.out.println("You successfully uploaded file=" + name);
            } catch (Exception e) {
                System.out.println("You failed to upload " + name + " => " + e.getMessage());
            }
        } else {
            System.out.println("You failed to upload " + name
                    + " because the file was empty.");
        }
    }

Please pay attention to consumes = "multipart/form-data". It is necessary for your uploaded file because you should have a multipart call. You should have @RequestParam("file") MultipartFile file instead of @RequestParam("name") MultipartFile file).

Of course you should have configured a multipartview resolver the built-in support for apache-commons file upload and native servlet 3.

lambda
  • 3,295
  • 1
  • 26
  • 32
Valerio Vaudi
  • 4,199
  • 2
  • 24
  • 23
  • Thanks, I added that part. It looks like I'm not sending the request properly. Not sure how to do that. – mw02 May 10 '16 at 19:44
  • if you see I add the multipart/form-data in consume and I change the @RequestParam("name") MultipartFile file in @RequestParam("file") MultipartFile file. In fact if you see the your error say that Reqiured MultipartFile paramiter name. you should pass the multipart part whit mane file and name with name as paramiter name. – Valerio Vaudi May 10 '16 at 19:52
  • 1
    Ok, I made those changes. Thanks. I also had to remove the Content-Type from postman and then add in a return type to the controller. Now it works. – mw02 May 10 '16 at 20:25
  • 2
    @mw02 can you please post a screenshot of what the request params, headers, and body looked like in the Postman UI? – takanuva15 Jan 22 '21 at 19:29