java-SpringMVC 后台怎么获取前台jsp页面中file中的文件
java-SpringMVC \u540e\u53f0\u600e\u4e48\u83b7\u53d6\u524d\u53f0jsp\u9875\u9762\u4e2dfile\u4e2d\u7684\u6587\u4ef6form\u7684enctype=\u201dmultipart/form-data\u201d \u8fd9\u4e2a\u662f\u4e0a\u4f20\u6587\u4ef6\u5fc5\u987b\u76842.applicationContext.xml\u4e2d \u5173\u4e8e\u6587\u4ef6\u4e0a\u4f20\u7684\u914d\u7f6e\u4e0d@Controller public class UploadAction { @RequestMapping(value = "/upload.do") public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) { System.out.println("\u5f00\u59cb"); String path = request.getSession().getServletContext().getRealPath("upload"); String fileName = file.getOriginalFilename(); // String fileName = new Date().getTime()+".jpg"; System.out.println(path); File targetFile = new File(path, fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } //\u4fdd\u5b58 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName); return "result"; } }
@RequestMapping("presentAction/addPicture")public void addPicture(HttpSession session,HttpServletRequest request,HttpServletResponse response,@RequestParam(value = "presentId", required = true) int presentId){MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; // \u83b7\u5f97\u6587\u4ef6\uff1a MultipartFile file = multipartRequest.getFile("up_img"); if (!file.isEmpty()) { try { // \u6587\u4ef6\u4fdd\u5b58\u8def\u5f84 String filePath = request.getSession().getServletContext().getRealPath("/uploadImages")+"/" + file.getOriginalFilename(); // \u8f6c\u5b58\u6587\u4ef6 String path2="uploadImages/"+ file.getOriginalFilename(); System.out.println(filePath); presentService.addPicture(path2,presentId); file.transferTo(new File(filePath)); hashMap.put("code", "200"); printe(response); } catch (Exception e) { e.printStackTrace(); } } }
页面表单设置类型:enctype="multipart/form-data"
后台通过org.springframework.web.multipart.MultipartFile处理上传
@RequestMapping("presentAction/addPicture")
public void addPicture(HttpSession session,HttpServletRequest request,HttpServletResponse response,
@RequestParam(value = "presentId", required = true) int presentId)
{
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获得文件:
MultipartFile file = multipartRequest.getFile("up_img");
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = request.getSession().getServletContext().getRealPath("/uploadImages")+"/"
+ file.getOriginalFilename();
// 转存文件
String path2="uploadImages/"+ file.getOriginalFilename();
System.out.println(filePath);
presentService.addPicture(path2,presentId);
file.transferTo(new File(filePath));
hashMap.put("code", "200");
printe(response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
MultipartFile file = request.getFile("fileId");
扩展阅读:javascript&jquery ... javascript innerhtml ... java webservice ... javascript window ... javascript cookie ... javascript onload ... java serializable ... javascript substring ... zookeeper老婆hadoop ...