可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using Spring MVC and this is my method:
/**
* 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?
回答1:
In postman, set method type to POST.
Then select
Body -> form-data -> Enter your parameter name (file according to your code)
and on right side next to value column, there will be dropdown "text, file", select File. choose your image file and post it.
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.
回答2:
Maybe you could do it this way:
![](https://www.manongdao.com/static/images/pcload.jpg)
回答3:
I think the visual guide we're missing here is, highlighting the tasteful-but-nearly-invisible pale-grey-on-white dropdown for File.
After you choose POST
, then choose Body->form-data
, then find the dropdown, and then choose 'File', only then will the 'Choose Files' button magically appear:
![](https://www.manongdao.com/static/images/pcload.jpg)
回答4:
Like this :
![](https://www.manongdao.com/static/images/pcload.jpg)
Body -> form-data -> select file
You must write "file" instead of "name"
Also you can send JSON data from Body -> raw field. (Just paste JSON string)
回答5:
- 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.
回答6:
If somebody needed:
body -> form-data
Add field name as array
![](https://www.manongdao.com/static/images/pcload.jpg)
回答7:
If somebody wants to send json data in form-data format just need to declare the variables like this
Postman:
![](https://www.manongdao.com/static/images/pcload.jpg)
As you see, the description parameter will be in basic json format, result of that:
{ description: { spanish: 'hola', english: 'hello' } }
回答8:
If you are using cookies to keep session, you can use interceptor to share cookies from browser to postman.
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.
回答9:
If you want to make a PUT
request, just do everything as a POST
request but add _method
=> PUT
to your form-data
parameters.
回答10:
![](https://www.manongdao.com/static/images/pcload.jpg)
If you need like
Upload file in multipart using form data and send json data(Dto object) in same POST Request
Get yor JSON object as String in Controller and make it Deserialize by adding this line
ContactDto contactDto = new ObjectMapper().readValue(yourJSONString, ContactDto.class);