我有一个类应该将文件上传到 amazon s3 但我无法到达终点。我在执行 http://localhost:8088/api/user/all 时使用 postman 它工作正常。但是上传返回 401 unauthorized。我尝试调试,但我的请求甚至没有进入 Controller
@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 是错误的。
这是我对 postman 的要求 http://localhost:8088/api/upload 还使用 Form-data name key=ile 并从我的电脑中选择一个文件,我尝试了选择标题和不选择标题,我还在我的 Controller 中同时使用了 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
最佳答案
您应该使用适合二进制数据的内容类型。 application/octet-stream
是一个选项。
您还可以更改代码以使用 @RequestParam("file")
而不是 @RequestPart
。然后在正文中使用表单数据,以 "file"
为键。
关于java - 上传请求被 spring boot 服务器忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56725027/