我正在使用 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;
}
}
}
我需要在 postman 和文件中传递 session ID。我该怎么做?
最佳答案
在 postman 中,将方法类型设置为 POST。
然后选择 正文 -> 表单数据 -> 输入您的参数名称(文件根据您的代码)
在 Key 字段的右侧,将鼠标悬停在其上时,有一个 下拉菜单 可以在 Text/File 之间进行选择。选择文件,然后“选择文件”按钮将出现在值字段中。
对于其余的基于“文本”的参数,您可以像通常使用 postman 一样发布它。只需输入参数名称并从右侧下拉菜单中选择“文本”并为其输入任何值,点击发送按钮。你的 Controller 方法应该被调用。
关于java - 如何在 Postman 中上传文件和 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39037049/