Spring 正在尝试使用postman上载多部分文件

Spring 正在尝试使用postman上载多部分文件,spring,rest,multipartform-data,postman,Spring,Rest,Multipartform Data,Postman,我试图上传一个多部分文件使用邮递员和错误。以下是代码和屏幕截图: 你应该有这样一件事: @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = "multipart/form-data") public void uploadFileHandler(@RequestParam("name") String name, @Re

我试图上传一个多部分文件使用邮递员和错误。以下是代码和屏幕截图:


你应该有这样一件事:

@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.");
        }
    }
请注意
consumes=“multipart/form data”
。这对于您上传的文件是必要的,因为您应该有一个多部分调用。您应该有
@RequestParam(“file”)多部分文件,而不是
@RequestParam(“name”)多部分文件)


当然,您应该配置一个multipartview解析器,它内置了对apache commons文件上传和本机servlet 3的支持。

谢谢,我添加了这一部分。看起来我没有正确发送请求。不知道如何做。如果您看到我在consume中添加了多部分/表单数据,并在@RequestParam(“文件”)多部分文件中更改了@RequestParam(“名称”)多部分文件。事实上,如果您看到错误,请说出所需的MultipartFile参数名称。您应该传递包含mane文件和名称的多部分部分,名称为paramiter name。谢谢我还必须从postman中删除内容类型,然后在控制器中添加返回类型。现在它可以工作了。@mw02您可以发布一个屏幕截图,说明在邮递员界面中请求参数、标题和正文是什么样子的吗?
@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.");
        }
    }