简体   繁体   English

如何使用邮递员和弹簧靴发送 multipartFile 和 Json

[英]How to send multipartFile and Json with postman and spring boot

I want to send a file and a json model at one post request.我想在一个帖子请求中发送一个文件和一个 json 模型。

My Request Mapping looks like that:我的请求映射如下所示:

@ResponseBody
@RequestMapping(value = "/sftp/upload", method = RequestMethod.POST)
public ResponseEntity<SftpModel> upload(@RequestPart("file") MultipartFile file, @RequestPart("sftpModel") SftpModel sftpModel) {

My Json has this structure:我的 Json 具有以下结构:

{
  "sftpHost": "ftp01.Host.de",
  "sftpPort": 22,
  "sftpUser": "anyUser",
  "sftpPassword": "anyPass",
  "sftpRemoteDirectory": "/"
}

And the file is on my system.该文件在我的系统上。

I'm able to send the file or the sftpModel seperatly but not together.我可以sftpModel发送filesftpModel但不能一起发送。 The error I receive is:我收到的错误是:

{
  "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"
}

I tried it with postman and curl.我用邮递员和卷曲试过了。 But no chance.但是没有机会。 在此处输入图片说明

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

Is there any way to send both?有什么办法可以同时发送吗?

You java code is looks like perfect.你的java代码看起来很完美。

@ResponseBody @RequestMapping(value = "/sftp/upload", method = RequestMethod.POST) public ResponseEntity<SftpModel> upload(@RequestPart("file") MultipartFile file, @RequestPart("sftpModel") SftpModel sftpModel) { }

You can write your SftpModel json string in one json file and try uploading with that json file.您可以在一个 json 文件中编写您的 SftpModel json 字符串,然后尝试使用该 json 文件上传。

Click here to see the postman image单击此处查看邮递员图像

Please try with below code:请尝试使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM