springmvc文件上传
来源:互联网 发布:乡镇网络舆情应急预案 编辑:程序博客网 时间:2024/06/05 09:56
===============fileup.jsp页面主要代码================
<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 ModelAndView("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>
阅读全文
1 0
- springMVC(11) springMVC文件上传
- SpringMVC-----8、SpringMVC上传文件
- springmvc文件上传总结
- SpringMVC multipart文件上传
- SpringMVC中的文件上传
- springmvc的文件上传
- springMVC上传文件
- springMVC 文件上传
- SpringMVC - 文件上传篇
- springMVC文件上传
- springMVC文件上传
- springMVC文件上传
- SpringMVC 上传文件
- SpringMVC文件上传
- springMVC上传文件
- springMVC上传文件例子
- springMVC上传文件
- springmvc文件上传错误
- 探究Glide的自定义模块功能
- 提高专业技能之 “Codec定点化”
- 详解pycharm新建文件时头部的模板
- hive服务
- CUDA中各种内存及线程布局的性能实验分析(1)
- springmvc文件上传
- C++文件操作
- 练习 4:Bash:处理文件,pwd,ls,cp,mv,rm,touch
- Redis Set命令
- 电子锁项目小结
- MemoryStream内存数据读写
- linux对oracle数据库操作
- C/C++遍历目录下的所有文件(Windows/Linux篇,超详细)
- Java高级常见问题总结