明明表单提交的method设置为post,然后在controller那设置只能请求为post请求
然后第一次post请求是正常的,第二次开始后便不正常了,出现post的请求302,get请求405;这个结果很纳闷吧
<!--注意action的路径--> <form id="form1" action="upload" method="post" enctype="multipart/form-data"> <div> <h2>一次选择多个文件进行上传</h2> </div> <input type="file" value="" name="file"/> <input type="submit"> </form>
请求
@RequestMapping(value="/upload",method=RequestMethod.POST) public void uploadify(@RequestParam("file")MultipartFile multiFile,HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{ System.out.println("上传文件"); System.out.println("name:"+multiFile.getOriginalFilename()); System.out.println("inputstream"+multiFile.getInputStream()); /*MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multiFile = multipartRequest.getFile("Filedata");*/ ServletContext sc = request.getSession().getServletContext(); String dir = sc.getRealPath("/upload");//附件存放服务器的路径 System.out.println(dir); File file = new File(dir); if(!file.exists()){ file.mkdirs(); } String filename = multiFile.getOriginalFilename(); String realname = filename.substring(0, filename.indexOf(".")); //防止文件被覆盖,以纳秒生成文件 Long _l = System.nanoTime(); String _extfilename = filename.substring(filename.indexOf(".")); filename = _l+_extfilename; String uploadPath = request.getContextPath()+""+dir+"/"+filename; String responseStr = ""; try { FileUtils.writeByteArrayToFile(new File(dir, filename), multiFile.getBytes()); responseStr = "上传成功!"; } catch (Exception e) { e.printStackTrace(); responseStr = "fail"; System.out.println("上传失败!"); } }
然后,我再改一下action路径——在action路径的基础上加上"/"
结果,就正常发送post的请求,后台服务器也不报WARNING: Request method ‘GET‘ not supported
<form id="form1" action="upload/" method="post" enctype="multipart/form-data"> <div> <h2>一次选择多个文件进行上传</h2> </div> <input type="file" value="" name="file"/> <input type="submit"> </form>
分析:
在请求中可能出现重名的请求情况
这样spingmvc就不能正常解析请求了
springmvc表单提交出现WARNING: Request method 'GET' not supported
时间: 2024-11-08 00:48:17