spring mvc上传文件
@ResponseBody
@RequestMapping("/upload")
public String upload(HttpServletRequest req) {
try {
MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) req;
MultipartFile file = mreq.getFile("file");
String fileName = file.getOriginalFilename();
FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/") +
"upload/" + fileName);
fos.write(file.getBytes());
fos.flush();
fos.close();
return "success";
} catch (IOException e) {
return e.getMessage();
}
}
键盘敲烂,薪资上万 -----by 小老弟