Trying to upload MultipartFile with postman

13,009

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.

Share:
13,009
mw02
Author by

mw02

Updated on June 14, 2022

Comments

  • mw02
    mw02 almost 2 years

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

    http://imgur.com/pZ5cXrh

    http://imgur.com/NaWQceO

    @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
    mw02 about 8 years
    Thanks, I added that part. It looks like I'm not sending the request properly. Not sure how to do that.
  • Valerio Vaudi
    Valerio Vaudi about 8 years
    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.
  • mw02
    mw02 about 8 years
    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.
  • takanuva15
    takanuva15 over 3 years
    @mw02 can you please post a screenshot of what the request params, headers, and body looked like in the Postman UI?