Java 如何在 Postman 中上传文件和 JSON 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39037049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload a file and JSON data in Postman?
提问by Harikrishnan K.N.
I am using Spring MVC and this is my method:
我正在使用 Spring MVC,这是我的方法:
/**
* Upload single file using Spring Controller.
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<GenericResponseVO<? extends IServiceVO>> uploadFileHandler(
@RequestParam("name") String name,
@RequestParam("file") MultipartFile file,
HttpServletRequest request,
HttpServletResponse response) {
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
// Creating the directory to store file
String rootPath = System.getProperty("catalina.home");
File dir = new File(rootPath + File.separator + "tmpFiles");
if (!dir.exists()) {
dir.mkdirs();
}
// Create the file on server
File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
System.out.println("Server File Location=" + serverFile.getAbsolutePath());
return null;
} catch (Exception e) {
return null;
}
}
}
I need to pass the session id in postman and also the file. How can I do that?
我需要在邮递员和文件中传递会话 ID。我怎样才能做到这一点?
采纳答案by Sumit Badaya
In postman, set method type to POST.
在邮递员中,将方法类型设置为POST。
Then select Body -> form-data -> Enter your parameter name (fileaccording to your code)
然后选择 Body -> form-data -> 输入你的参数名称(根据你的代码文件)
and on right side next to value column, there will be dropdown "text, file", select File. choose your image file and post it.
在 value 列旁边的右侧,将有下拉菜单 "text, file",选择File。选择您的图像文件并发布它。
For rest of "text" based parameters, you can post it like normally you do with postman. Just enter parameter name and select "text" from that right side dropdown menu and enter any value for it, hit send button. Your controller method should get called.
对于其余的基于“文本”的参数,您可以像通常使用邮递员一样发布它。只需输入参数名称并从右侧下拉菜单中选择“文本”并为其输入任何值,点击发送按钮。您的控制器方法应该被调用。
回答by burakozgul
回答by Sandesh Jain
If you are using cookies to keep session, you can use interceptorto share cookies from browser to postman.
如果您使用 cookie 来保持会话,您可以使用拦截器将 cookie 从浏览器共享给邮递员。
Also to upload a file you can use form-data tab under body tab on postman, In which you can provide data in key-value format and for each key you can select the type of value text/file. when you select file type option appeared to upload the file.
同样要上传文件,您可以使用 postman 的 body 选项卡下的 form-data 选项卡,您可以在其中以键值格式提供数据,并且对于每个键,您可以选择值文本/文件的类型。当您选择文件类型选项时出现上传文件。
回答by Rohit Thakur
- Don't give any headers.
- Put your json data inside a .json file.
- Select your both files one is your .txt file and other is .json file for your request param keys.
- 不要给任何标题。
- 将您的 json 数据放在 .json 文件中。
- 选择您的两个文件,一个是您的 .txt 文件,另一个是您的请求参数键的 .json 文件。
回答by Sebastiao Marcos
回答by Chris F Carroll
The Missing Visual Guide
缺失的视觉指南
You must firstfind the nearly-invisible pale-grey-on-white dropdown for File
which is the magic key that unlocks the Choose Files
button.
您必须首先找到几乎不可见的浅灰色白色下拉菜单,File
它是解锁Choose Files
按钮的魔法钥匙。
Afteryou choose POST
, thenchoose Body->form-data
, thenfind the File dropdown, and thenchoose 'File', only thenwill the 'Choose Files' button magically appear:
之后您选择POST
,然后选择Body->form-data
,然后找到该文件下拉菜单,然后再选择“文件”,只有将“选择文件”按钮,奇迹般地出现:
回答by kubilay
If you want to make a PUT
request, just do everything as a POST
request but add _method
=> PUT
to your form-data
parameters.
如果您想发出PUT
请求,只需将所有内容作为POST
请求执行,但将_method
=>添加PUT
到您的form-data
参数中。
回答by Sebastian Ardila
If somebody wants to send json data in form-data format just need to declare the variables like this
如果有人想以表单数据格式发送json数据只需要声明这样的变量
Postman:
邮差:
As you see, the description parameter will be in basic json format, result of that:
如您所见,描述参数将采用基本的 json 格式,其结果是:
{ description: { spanish: 'hola', english: 'hello' } }
回答by Ajay k
If you need like Upload file in multipartusing form data and send jsondata(Dto object) in same POSTRequest
如果您需要使用表单数据在多部分中上传文件并在同一个POST请求中发送json数据(Dto 对象)
Get yor JSON object as String in Controller and make it Deserialize by adding this line
将您的 JSON 对象作为控制器中的字符串获取,并通过添加此行使其反序列化
ContactDto contactDto = new ObjectMapper().readValue(yourJSONString, ContactDto.class);