【问题标题】:Can't upload files in spring bootspring boot 无法上传文件
【发布时间】:2017-05-11 01:32:23
【问题描述】:

过去 3 天我一直在努力解决这个问题,当我尝试在我的 Spring Boot 项目中上传文件时,我不断收到以下异常。 org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

我不确定它是否会有所不同,但我正在将我的应用程序部署为 weblogic 上的战争, 这是我的控制器

@PostMapping
public AttachmentDto createAttachment(@RequestParam(value = "file") MultipartFile file) {
    logger.info("createAttachment - {}", file.getOriginalFilename());
    AttachmentDto attachmentDto = null;
    try {
        attachmentDto = attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return attachmentDto;
}

我可以在 spring boot 执行器中看到多部分 bean

在 chrome 中看到的负载

【问题讨论】:

    标签: file-upload spring-boot weblogic


    【解决方案1】:

    @RequestParm 'file' 需要名称属性

    <input type="file" class="file" name="file"/>
    

    【讨论】:

    • 原来我正在使用的库('ng2-file-upload')搞砸了它如何发送其他东西,作为名称,从基本 html 尝试了这段代码并且它有效,抱歉这个
    【解决方案2】:

    您可以尝试使用@RequestPart,因为它使用HttpMessageConverter,它考虑了请求部分的“Content-Type”标头。

    请注意,@RequestParam 注释也可用于将“multipart/form-data”请求的部分与支持相同方法参数类型的方法参数相关联。主要区别在于,当方法参数不是 String 时,@RequestParam 依赖于通过注册的 Converter 或 PropertyEditor 进行类型转换,而 @RequestPart 依赖于 HttpMessageConverters 考虑到请求部分的“Content-Type”标头。 @RequestParam 可能与名称-值表单字段一起使用,而 @RequestPart 可能与包含更复杂内容(例如 JSON、XML)的部分一起使用。

    Spring Documentation

    代码:

    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AttachmentDto createAttachment(@RequestPart("file") MultipartFile file) {
        logger.info("Attachment - {}", file.getOriginalFilename());
    
        try {
            return attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
        } catch (final IOException e) {
            logger.e("Error creating attachment", e);
        }
    
        return null;
    }
    

    【讨论】:

    • 恐怕不走运,以前试过,感谢文档,阅读愉快
    【解决方案3】:

    您正在使用多部分发送文件,因此无需进行太多配置即可获得所需的结果。 我有同样的要求,我的代码运行良好:

    @RestController
    @RequestMapping("/api/v2")
    public class DocumentController {
    
        private static String bucketName = "pharmerz-chat";
        //   private static String keyName        = "Pharmerz"+ UUID.randomUUID();
    
        @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
        public URL uploadFileHandler(@RequestParam("name") String name,
                                     @RequestParam("file") MultipartFile file) throws IOException {
    
    /******* Printing all the possible parameter from @RequestParam *************/
    
            System.out.println("*****************************");
    
            System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
            System.out.println("file.getContentType()" + file.getContentType());
            System.out.println("file.getInputStream() " + file.getInputStream());
            System.out.println("file.toString() " + file.toString());
            System.out.println("file.getSize() " + file.getSize());
            System.out.println("name " + name);
            System.out.println("file.getBytes() " + file.getBytes());
            System.out.println("file.hashCode() " + file.hashCode());
            System.out.println("file.getClass() " + file.getClass());
            System.out.println("file.isEmpty() " + file.isEmpty());
    
    /**
            BUSINESS LOGIC
    Write code to upload file where you want
    *****/
    return "File uploaded";
    }
    

    【讨论】:

    • 谢谢,这已经解决了,这是我用来上传文件的前端库的问题
    【解决方案4】:

    上述解决方案都不适合我,但当我深入挖掘时,我发现弹簧安全性是罪魁祸首。即使我发送 CSRF 令牌,我也反复遇到不支持 POST 的问题。当我在谷歌浏览器中使用开发人员工具进行检查并在网络选项卡中看到状态代码时,我才知道我收到了禁止的 403。我将映射添加到 Spring Security 配置中的 ignoredCsrfMapping 中,然后它绝对可以正常工作,没有任何其他缺陷。不知道为什么我不允许通过安全性发布多部分数据。 application.properties 文件中需要说明的一些强制设置如下:

    spring.servlet.multipart.max-file-size=10MB
    spring.servlet.multipart.max-request-size=10MB
    spring.http.multipart.max-file-size=10MB
    spring.http.multipart.max-request-size=10MB
    spring.http.multipart.enabled=true
    

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 2019-04-06
      • 2017-12-30
      • 2021-01-22
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多