【发布时间】:2016-12-26 11:55:58
【问题描述】:
我正在使用 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;
}
}
}
我需要在邮递员和文件中传递会话 ID。我该怎么做?
【问题讨论】:
-
一切都说完并完成后解决了我的问题,将
[]添加到文件上传参数的名称中,例如。image[]file[]upload[] -
这是一个 2 分钟的快速视频教程youtu.be/sFlPa_Vow3w
标签: java json spring-mvc postman