【问题标题】:Trying to upload MultipartFile with postman尝试使用邮递员上传 MultipartFile
【发布时间】:2016-05-10 19:32:06
【问题描述】:

我正在尝试使用 PostMan 上传 Multipart 文件并出现错误。下面是代码和截图:

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.");
    }
}

【问题讨论】:

    标签: spring rest multipartform-data postman


    【解决方案1】:

    你应该有这样的东西:

    @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") MultipartFile file 而不是@RequestParam("name") MultipartFile file)

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

    【讨论】:

    • 谢谢,我添加了那部分。看起来我没有正确发送请求。不知道该怎么做。
    • 如果你看到我在消费中添加了 multipart/form-data 并在 @RequestParam("file") MultipartFile 文件中更改了 @RequestParam("name") MultipartFile 文件。事实上,如果你看到你的错误说 Reqiured MultipartFile 参数名称。您应该传递多部分的 whit 鬃毛文件和名称,并将名称作为参数名称。
    • 好的,我做了这些改变。谢谢。我还必须从邮递员中删除 Content-Type,然后将返回类型添加到控制器。现在可以了。
    • @mw02 能否请您发布一张 Postman UI 中请求参数、标头和正文的屏幕截图?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2021-05-12
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    相关资源
    最近更新 更多