springmvc文件上传
2017-10-09 10:31
183 查看
===============fileup.jsp页面主要代码================
===============单个,多个文件上传的控制器方法!上传成功跳转到ok.jsp=========
=================
这样就可以了!注意要导入一些文件上传的包!我用的maven,如果你用的导包方式的话就自己加包,maven导包我也贴出来
<body> 单个文件 <form action="${pageContext.request.contextPath}/fileup" method="post" enctype="multipart/form-data"> <input type="file" name="photo"><br /> <input type="submit" value="上传"> </form> 多个文件 <form action="${pageContext.request.contextPath}/morefileup" method="post" enctype="multipart/form-data"> <input type="file" name="photos"><br /> <input type="file" name="photos"><br /> <input type="file" name="photos"><br /> <input type="submit" value="上传"> </form> </body>
===============单个,多个文件上传的控制器方法!上传成功跳转到ok.jsp=========
/** * 单个文件上传 * @param photo 照片文件 * @param session session对象 * @return ModelAndView * @throws IllegalStateException 异常 * @throws IOException 异常 */ @RequestMapping(value="/fileup",method=RequestMethod.POST) public ModelAndView doFileUp(@RequestParam MultipartFile photo,HttpSession session) throws IllegalStateException, IOException{ System.out.println("file"); if (!photo.isEmpty()) { //判断文件不为空 System.out.println("Photo"); //服务端的image目录手动创建 //path=D:\apache-tomcat-7.0.73\webapps\SpringMVC4.3.7\image //D:\\Eclipseworkspace\\SpringMVC4.3.7\\WebContent\\image String path = session.getServletContext().getRealPath("/image"); //获取原始文件名 String fileName = photo.getOriginalFilename(); //限制文件上传格式 if (fileName.endsWith(".jpg") || fileName.endsWith(".png")) { File file = new File(path, fileName); //判断路径是否存在 if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } //完成上传 photo.transferTo(file); } else{ return new ModelAndView("fileup.jsp"); } } return new ModelAndView("ok.jsp"); } /** * 多个文件上传 * @param photos 照片文件数组 * @param session session对象 * @return ModelAndView * @throws IllegalStateException 异常 * @throws IOException 异常 */ @RequestMapping(value="/morefileup",method=RequestMethod.POST) public ModelAndView doMoreFileUp(@RequestParam MultipartFile[] photos, HttpSession session) throws IllegalStateException, IOException{ //服务端的image目录手动创建 //path=D:\apache-tomcat-7.0.73\webapps\SpringMVC4.3.7\image String path = session.getServletContext().getRealPath("/image"); for (MultipartFile photo : photos) { if (!photo.isEmpty()) { //判断文件不为空 System.out.println("Photo"); //获取原始文件名 String fileName = photo.getOriginalFilename(); //限制文件上传格式 if (fileName.endsWith(".jpg") || fileName.endsWith(".png")) { File file = new File(path, fileName); //判断路径是否存在 if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } //完成上传 photo.transferTo(file); } else{ return new Mode 4000 lAndView("fileup.jsp"); } } } return new ModelAndView("ok.jsp"); }
=================
springmvc.xml中注册multipart解析器==================
<!-- 注册multipart解析器 id名固定,是由DispatcherServlet直接调用 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置编码格式 要与jsp界面一致 --> <property name="defaultEncoding" value="utf-8"></property> <!-- 限制大小10M[10485760] --> <property name="maxUploadSize" value="10485760"></property> </bean>
这样就可以了!注意要导入一些文件上传的包!我用的maven,如果你用的导包方式的话就自己加包,maven导包我也贴出来
<!-- 上传文件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency>
相关文章推荐
- springmvc文件上传404、500之类的问题
- SpringMVC和ajax文件上传
- SpringMVC实现文件上传
- SpringMVC学习总结(四).SpringMVC处理Json与文件的上传下载
- SpringMVC 两种上传文件方法
- springmvc配置上传文件
- springmvc 文件上传
- SpringMVC【参数绑定、数据回显、文件上传】
- springmvc文件上传和下载
- SpringMvc的Common- upload的MutilpartFile转换为io.File--进行Excel文件上传
- SSM框架-SpringMVC 实例文件上传下载
- springmvc 上传文件的时候.The request sent by the client was syntactically incorrect
- SpringMVC 上传文件(超简单-5行代码)
- springMVC入门--5.文件上传下载
- SpringMVC中的文件上传
- SpringMVC框架实现文件、资源的上传和下载
- springMVC实现文件上传
- SpringMVC上传文件的两种方法
- SpringMVC结合ajaxfileupload.js实现异步上传文件
- springmvc利用jquery.form插件异步上传文件