问题描述
- 关于springmvc的视频上传
-
controller里面的代码:
@RequestMapping(value = "/fileUpload2.do", method = RequestMethod.GET)public String fileUpload2(HttpServletRequest request)
throws IllegalStateException, IOException {
System.out.println("1");
// 设置上下方文CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
System.out.println("2");
// 检查form是否有enctype="multipart/form-data"if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
System.out.println("3");
Iterator iter = multiRequest.getFileNames();while (iter.hasNext()) {
// 由CommonsMultipartFile继承而来,拥有上面的方法.
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null) {
String fileName = "demoUpload" + file.getOriginalFilename();
String path = "E:/" + fileName;
File localFile = new File(path); file.transferTo(localFile); System.out.println("4"); } } } return "main/homework"; } jsp页面代码: <form id="form" action="fileUpload2.do" method="GET" enctype="multipart/form-data"> <table align="center" border="1" bgcolor="blue" height="300px"> <tr align="center"><td colspan="2" align="center"> <input type="file" name="myfile" style="height:35px; width:100px;"></td> </tr> <tr align="center"><td colspan="2" align="center" > <input type="submit" name="submit" value="上传" style="height:35px; width:100px;" ></td> </tr> </table> </form>
控制台里只能打印出来controller方法里的1和2, if (multipartResolver.isMultipart(request))这条语句往后的代码没有得到执行,这是为什么呢?很急,有没有大神帮帮忙
解决方案
method = RequestMethod.GET,上传文件能用GET这个动作? 得用POST吧