如何使用postman和spring boot发送multipartFile和Json

时间:2017-06-13 07:21:13

标签: json spring spring-boot postman

我想在一个帖子请求中发送文件和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": "/"
}

该文件在我的系统上。

我能够单独发送filesftpModel但不能一起发送。{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"
}

我用邮差和卷发试了一下。但没有机会。enter image description here

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

有没有办法发送两者?

2 个答案:

答案 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文件上传。

Click here to see the postman image

答案 1 :(得分:0)

请尝试以下代码:

public ResponseEntity<?> uploadFile(@RequestPart MultipartFile file, @RequestPart String user) {
            User users = new ObjectMapper().readValue(user, User.class);
}