Spriing mvc上传多文件
2015-12-04 09:10
288 查看
jsp页面
controller.java 接收数据(代码如下)
dispatcherServlet-servlet.xml 代码:
<form action="${pageContext.request.contextPath }/user/upload" method="post" enctype="multipart/form-data"> attach 1:<input type="file" name="attachs" /><br/> attach 2:<input type="file" name="attachs"/><br/> <button type="submit">提交</button> </form>
controller.java 接收数据(代码如下)
@RequestMapping(value="/upload",method=RequestMethod.POST ) public String upload(@RequestParam("attachs") MultipartFile[] attachs,HttpServletRequest req){ String realPath=req.getSession().getServletContext().getRealPath("/resources/upload");//保存的路径 System.out.println(attachs.length); System.out.println(realPath); for(MultipartFile attach:attachs){ System.out.println(attach); if(!attach.isEmpty()){ File file=new File(realPath+"/"+attach.getOriginalFilename()); try { //底层数据io传输 attach.transferTo(file); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } } } return "welcome"; }
dispatcherServlet-servlet.xml 代码:
<!-- 配置MultipartResolver,用于上传文件,使用Spring的CommonsMultiparResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5000000"/> <property name="defaultEncoding" value="UTF-8"/> </bean>
相关文章推荐
- 第十二周项目1 图基本算法库
- wikioi 1002 旁路
- 点击和焦点事件
- poj 1178 Camelot 枚举
- java修改图片大小(留作备用)
- 不要让一个人随便进入你的世界,也不要拼命尝试进入一个人的世界
- OpenCV: 实现将三幅图像合并在一张图片
- [LeetCode]Serialize and Deserialize Binary Tree
- 我理解的正确的代码
- 【学神-RHEL7】1-9-文件的归档和压缩
- 轻松学习JavaScript二:JavaScript语言的基本语法要求
- java的可变类和不可变类
- XML DOM
- 通达OA2015 数据选择控件的使用随笔
- 第十三周项目2—Kruskal算法的验证
- 富士施乐打印机-更改账户
- 第十四周项目2-二叉树排序树中查找的路径
- 【C#】Socket客户端与服务器的简单通信
- iOS9适配 之 关于info.plist 第三方登录 添加URL Schemes白名单
- 数据挖掘领域中的分类和回归区别是什么?