UploadController:
@Controller
@RequestMapping("/testSSH")
public class UploadController {
@RequestMapping(value = "/upload.do")
public String upload(
@RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request, ModelMap model) {
System.out.println("上传文件");
String path = request.getSession().getServletContext()
.getRealPath("upload");
String fileName = file.getOriginalFilename();
System.out.println("fileName:"+fileName);
System.out.println("path:"+path);
File targetFile = new File(path, fileName);
String filet = file.getContentType();
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("fileUrl", request.getContextPath() + "/upload/"
+ fileName);
return "result";
}
pageEncoding="UTF-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
上传文件html>
上传结果