我想在一个帖子请求中发送文件和json模型。
我的请求映射看起来像这样:
@ResponseBody
@RequestMapping(value = "/sftp/upload", method = RequestMethod.POST)
public ResponseEntity<SftpModel> upload(@RequestPart("file") MultipartFile file, @RequestPart("sftpModel") SftpModel sftpModel) {
我的Json有这样的结构:
{
"sftpHost": "ftp01.Host.de",
"sftpPort": 22,
"sftpUser": "anyUser",
"sftpPassword": "anyPass",
"sftpRemoteDirectory": "/"
}
该文件在我的系统上。
我能够单独发送file
或sftpModel
但不能一起发送。{p}我收到的错误是:
{
"timestamp": 1497336812907,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/octet-stream' not supported",
"path": "/secure-data-transfer-service/sftp/upload"
}
curl --form "file=@test.txt" --form "sftpModel={"sftpHost":"ftp01.Host.de","sftpPort":22,"sftpUser":"anyUser","sftpPassword":"anyPass","sftpRemoteDirectory":"/"}" http://localhost:8080/secure-data-transfer-service/sftp/upload
有没有办法发送两者?
答案 0 :(得分:1)
您的Java代码看起来很完美。
@ResponseBody
@RequestMapping(value = "/sftp/upload", method = RequestMethod.POST)
public ResponseEntity<SftpModel> upload(@RequestPart("file") MultipartFile file, @RequestPart("sftpModel") SftpModel sftpModel) { }
您可以将SftpModel json字符串写入一个json文件,然后尝试使用该json文件上传。
答案 1 :(得分:0)
请尝试以下代码:
public ResponseEntity<?> uploadFile(@RequestPart MultipartFile file, @RequestPart String user) {
User users = new ObjectMapper().readValue(user, User.class);
}