我正在使用 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。我怎样才能做到这一点?
原文由 Harikrishnan K.N. 发布,翻译遵循 CC BY-SA 4.0 许可协议
在邮递员中,将方法类型设置为 POST 。
然后选择 Body -> form-data -> 输入您的参数名称( 文件 根据您的代码)
在 Key 字段的右侧,将鼠标悬停在其上时,有一个 下拉菜单 可供选择 Text/File 。选择文件,然后“选择文件”按钮将出现在值字段中。
对于其余基于 “文本”的参数,您可以像通常使用邮递员一样发布它。只需输入参数名称并从右侧下拉菜单中选择“文本”并为其输入任何值,点击发送按钮。你的控制器方法应该被调用。