【问题标题】:Upload request ignored by spring boot serverSpring Boot 服务器忽略的上传请求
【发布时间】:2019-11-05 13:54:06
【问题描述】:

我有一个这样的类,应该将文件上传到亚马逊 s3 但我无法到达端点。当我执行http://localhost:8088/api/user/all 时,我正在使用邮递员,它工作正常。但上传返回 401 未经授权。我尝试调试,但我的请求甚至没有进入控制器

    @CrossOrigin
    @RequestMapping(value = "/api")
    @RestController
    public class PostController {

        private static final Logger LOGGER = LogManager.getLogger();

        @Autowired
        private PhotoService photoService;

        @Autowired
        private UserService userService;

        @RequestMapping(method = POST, value = "/upload")
        public Photo submitPhoto(@RequestPart MultipartFile file) throws IOException {
            LOGGER.debug("submitPhoto({})", file);
           return photoService.savePhoto(file);
        }

        @RequestMapping(method = GET, value = "/user/all")
        public List<User> loadAll() {
            return this.userService.findAll();
        }
}

所以我在 SO 上找到了如何使用 POSTMAN 上传文件,我使用了他们的示例,但结果相同,我什至无法调试,因为请求没有命中方法,就好像我的 url 错误一样。

这是我对邮递员的要求 http://localhost:8088/api/upload 还使用 Form-data name key=ile 并从我的电脑中选择一个文件,我尝试过选择标题和不选择标题,我还在控制器中同时使用了 RequestPart 和 RequestBody。

有什么建议吗?

有时我会进入我的环境(Intellij)

org.apache.tomcat.util.http.Parameters   : Character decoding failed. Parameter [���� JFIF      �� �Photoshop 3.0 8BIM     g( bFBMD01000aa80300001424000049540000a05600008b590000827a0000cbc30000eacb00004cd100007cd700003b640100 ��ICC_PROFILE   lcms  mntrRGB XYZ �    ) 9acspAPPL                          ��     �-lcms                                               
desc   �   ^cprt  \   wtpt  h   bkpt  |   rXYZ  �   gXYZ  �   bXYZ  �   rTRC  �   @gTRC  �   @bTRC  �   @desc       c2                                                                                  text    FB  XYZ       ��     �-XYZ         3  �XYZ       o�  8�  �XYZ       b�  ��  �XYZ       $�  �  ��curv          ��ck�?Q4!�)�2;�FQw]�kpz���|�i�}���0���� C 
db° §jʼ@9H<HY-²£W
âyÃèaæÛÒÉ<ÚzhY}SDúØydôâ<¹ôêe½<úú5<ôÝSl,iC:v¢s ¢ÄªÊ!y®²öãÖy:V·¬s®sé!ÒÜ/Dç5Ðj¶(,°ñ ñ"±B<®Jál®ZÑ`íYVÁ)Âʰ,$)Ö L³Z²"0)ÊÉcTKeL;Ó$è~Xu
é«2Ól©³6.Äî^] has been ignored. Note that the name and value quoted here may be corrupted due to the failed decoding. Use debug level logging to see the original, non-corrupted values.
 Note: further occurrences of Parameter errors will be logged at DEBUG level 

【问题讨论】:

    标签: java file file-upload multipart


    【解决方案1】:

    您应该使用适合二进制数据的内容类型。 application/octet-stream 是一种选择。
    您还可以将代码更改为使用@RequestParam("file") 而不是@RequestPart。然后在正文中使用表单数据,以"file" 为键。

    【讨论】:

    • 我尝试了二进制数据 Content-Type-application/octet-stream 但我得到 401 所以我仍在使用表单数据,因为大多数示例都使用它,只是我的由于某种原因不起作用
    • 你得到一个 org.apache.tomcat.util.http.Parameters :字符解码失败。例外吧?
    • 但是当我使用表单数据时,我没有例外,只有 401
    • 你应该使用 application/octet-stream 。您能分享您的请求标头吗?
    • 是的,这就是 X-Content-Type-Options →nosniff X-XSS-Protection →1; mode=block Cache-Control →no-cache, no-store, max-age=0, must-revalidate Pragma →no-cache Expires →0 X-Frame-Options →DENY Content-Length →0 Date →Sun, 23 Jun格林威治标准时间 2019 年 17:53:19
    猜你喜欢
    • 1970-01-01
    • 2017-09-07
    • 2019-07-22
    • 1970-01-01
    • 2014-12-13
    • 2020-05-26
    • 2021-01-24
    • 1970-01-01
    • 2021-11-04
    相关资源
    最近更新 更多