SSM中文件上传与下载
来源:互联网 发布:淘宝卖家活动报名 编辑:程序博客网 时间:2024/06/05 18:58
单文件上传
在spring-mvc中配置文件上传的解析器
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="40960" /></bean>
编写Controller文件:
@RequestMapping("upload") public ModelAndView uploadFile(MultipartFile uploadFile,HttpSession session){ //获取上传文件名 String filename = uploadFile.getOriginalFilename(); //获取WebRoot下的images文件夹的绝对路径作为前半部分路径 String leftPath = session.getServletContext().getRealPath("/images"); //将文件的前半部分路径与文件名拼接 File file = new File(leftPath, filename); try { uploadFile.transferTo(file); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } ModelAndView mv = new ModelAndView(); mv.setViewName("index"); return mv; }
网页代码:
<form action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data"> <h2>文件上传</h2> 文件:<input type="file" name="uploadFile"/><br/><br/> <input type="submit" value="上传"/></form>
到这里单文件上传就已经完成了,下面就是最后效果
多文件上传
多文件上传即在原本上传的基础上,将传入参数MultipartFile uploadFile修改为MultipartFile[] uploadFile数组形式,在上传时就可以遍历这个数组来完成多文件上传
@RequestMapping("upload") public ModelAndView uploadFile(MultipartFile[] uploadFile,HttpSession session){ //获取上传文件名 for (int i = 0; i < uploadFile.length; i++) { String filename = uploadFile[i].getOriginalFilename(); //获取WebRoot下的images文件夹的绝对路径作为前半部分路径 String leftPath = session.getServletContext().getRealPath("/images"); //将文件的前半部分路径与文件名拼接 File file = new File(leftPath, filename); try { uploadFile[i].transferTo(file); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } } ModelAndView mv = new ModelAndView(); mv.setViewName("index"); return mv; }
jsp页面
<form action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data"> <h2>文件上传</h2> 文件1:<input type="file" name="uploadFile"/><br/> 文件2:<input type="file" name="uploadFile"/><br/> 文件3:<input type="file" name="uploadFile"/><br/> <input type="submit" value="上传"/></form>
文件下载
文件下载的一些内容可以去看看对请求头的分析
@RequestMapping("/download") public ResponseEntity<byte[]> downFile() throws IOException{ File file = new File("C:\\Users\\TU\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\SSM_beta1\\images\\456.png"); HttpHeaders headers = new HttpHeaders(); String filename = new String("helloWorld.png".getBytes("UTF-8"),"iso-8859-1"); //设置文件名 headers.setContentDispositionFormData("attachment", filename); //以文件下载的形式来输出流 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }
下载效果
阅读全文
0 0
- SSM中文件上传与下载
- ROR中文件的上传与下载
- Silverlight中文件的上传与下载
- Struts2中文件的上传与下载
- Java中文件的上传与下载
- Java中文件的上传与下载
- ASP.NET中文件上传与下载
- struts2中文件上传与下载
- 06_Java中文件上传与下载
- struts2中文件上传与下载
- spring中文件上传与下载
- Struts中文件的上传与下载
- strut2中文件的上传与下载
- servlet中文件的上传与下载
- PHP开发中文件的上传与下载
- javaWeb中文件的上传与下载及乱码问题解决
- JSP中文件的上传与下载示例
- docker容器中文件的上传与下载
- usaco 3.1 Contact
- gitlab 9.3.6部署
- 页面滚动的时候自动切换导航栏
- Maven学习笔记 --- Maven入门笔记四
- java特殊注释
- SSM中文件上传与下载
- 【bzoj1698】[Usaco2007 Feb]Lilypad Pond 荷叶池塘
- Hibernate一对多和多不多的操作
- Android中获取字符串长度、宽度(所占像素宽度)的方法
- 1041. Be Unique (20)
- cat
- 代码之髓读后感——名字&作用域&类型
- 仿微信底部菜单栏(ViewPager+ImagerView+TextView)
- Druid简介(Spring Boot + Mybatis + Druid数据源【官方start】)