spring - 尝试用 postman 上传 MultipartFile

标签 spring rest multipartform-data postman

我正在尝试使用 PostMan 上传多部分文件并出现错误。这是代码和屏幕截图:

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

最佳答案

你应该有这样的东西:

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

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

关于spring - 尝试用 postman 上传 MultipartFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37147395/

相关文章:

spring - RuntimeException 无法映射到响应,重新抛出到 HTTP 容器 java.lang.NullPointerException

rest - GoLang 通过 POST 请求发送文件

php - php mvc framework agavi 是否使用符合 REST 的 CRUD?

spring - 如何从单例中生成原型(prototype)对象? (需要设计帮助)

java - 根据验证结果设置不同的http状态码

java - 何时使用 ParameterizedPreparedStatementSetter 而不是 BatchPreparedStatementSetter,反之亦然,使用 Spring batchUpdate?

c# - 将二进制文件发布到票证 ServiceNow REST API - 删除边界和内容类型

spring - 带有 Spring 依赖注入(inject)的 GWT

c# - Web API POST MultipartFormDataContent : Can response return multipartform content?

ios - 在 ios swift 中使用 alamofire 5 从 multipartformdata 上传带有参数的文件