Spring Boot服务器忽略上传请求

时间:2019-06-23 14:53:35

标签: java file file-upload multipart

我有一个这样的类,应该将文件上传到Amazon 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();
        }
}

所以我发现如何使用POSTMAN上传文件,但我使用了它们的示例,但结果相同,我什至无法调试,因为请求没有像我的URL错误一样打到方法。

这是我对邮递员的要求 http://localhost:8088/api/upload 还使用Form-data name key = ile并从我的PC中选择一个文件,我尝试了选择标头和不选择标头的情况,还使用了控制器中的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 

1 个答案:

答案 0 :(得分:1)

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