springMVC的文件上传于下载

1、springmvc 文件的上传也是借助于两个工具所以需要添加两个jarapache-commons-fileupload.jarapache-commons-io.jar
2、在spring-servlet.xml中添加文件上传的处理bean的配置。<bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="utf-8" /> <!-- 默认编码 (ISO-8859-1) --><property name="maxInMemorySize" value="10240" /> <!-- 最大内存大小 (10240) --><property name="uploadTempDir" value="/temp/" /> <!--(临时文件存储目录) 上传后的目录名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) --><property name="maxUploadSize" value="-1" /> <!-- 最大文件大小,-1为无限止(-1) --></bean>其中属性<property name="uploadTempDir" value="/temp/" />的配置配置的是临时文件目录,spring会将文件先传到临时文件,然后我们再调用对应的API将临时文件写到目标文件。
3、编写上传文件的controller3.1上传一个文件直接在处理的方法中设置形参@RequestParam("file") CommonsMultipartFile  file注意这里的参数必须使用@RequestParam指定。然后调用方法file.getFileItem().write(targetFile);将临时文件写出到目标文件。
示例:
/*** 上传一个文件* @param name* @param file* @param session* @return*/@RequestMapping(value="/upload.do",method=RequestMethod.POST)public String fileUpLoad(String name,@RequestParam("file") CommonsMultipartFile  file,HttpSession session){if(!file.isEmpty()){String path = session.getServletContext().getRealPath("/upload/");String fileName = file.getOriginalFilename();String fileType = fileName.substring(fileName.lastIndexOf("."));File targetFile = new File(path,new Date().getTime()+fileType);try {file.getFileItem().write(targetFile);} catch (Exception e) {e.printStackTrace();}}return "showData";}3.2上传多个文件上传多个文件时,其实和上传一个文件一样,只是将形参改为@RequestParam("file") CommonsMultipartFile[]  file然后我们只需在方法中循环处理这些文件即可。
示例:
/*** 上传多个文件* @param name* @param files* @param session* @return*/@RequestMapping(value="/mupload.do",method=RequestMethod.POST)public String muFileUpLoad(String name,@RequestParam("file") CommonsMultipartFile[]  files,HttpSession session){if(files!=null && files.length>0){String path = session.getServletContext().getRealPath("/upload/");for (CommonsMultipartFile file : files) {String fileName = file.getOriginalFilename();String fileType = fileName.substring(fileName.lastIndexOf("."));File targetFile = new File(path,new Date().getTime()+fileType);try {file.getFileItem().write(targetFile);} catch (Exception e) {e.printStackTrace();}}}return "showData";}
4、文件下载文件下载其实和spring没关系,还是使用最普通的方式实现下载即可,在这里不赘述。
示例:
/*** 文件下载* @param session* @param response* @param fileName* @param isOnline* @throws Exception*/@RequestMapping(value="/downLoad.do",method=RequestMethod.GET)public void downLoad(HttpSession session,HttpServletResponse response,String fileName,boolean isOnline)throws Exception{String path = session.getServletContext().getRealPath("/upload/")+"\\"+fileName;File file = new File(path);System.out.println(path);if(!file.exists()){response.sendError(404, "您要下载的文件没找到");return;}BufferedInputStream bufIn = new BufferedInputStream(new FileInputStream(file));byte [] buff = new byte[1024];int len = -1;response.reset();if(isOnline){URL u = new URL("file:///"+path);response.setContentType(u.openConnection().getContentType());response.setHeader("Content-Disposition", "inline;filename="+fileName);}else{response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition", "attachment;filename="+fileName);}OutputStream out = response.getOutputStream();while((len=bufIn.read(buff))!=-1){out.write(buff,0,len);out.flush();}bufIn.close();out.close();}

springMVC的文件上传于下载相关推荐

  1. 使用SpringMVC模拟文件上传与下载案例

    文件上传下载 SpringMVC封装了Tomcat的上传文件功能 MultipartResolver接口 MultipartResolver接口定义了文件上传过程中的相关操作,并对通用性操作进行了封装 ...

  2. SpringBoot实现文件上传和下载

    文件上传需要使用到 MultipartResolver接口. Spring MVC 使用 MultipartResolver接口的实现类:CommonsMultipartResolver .Commo ...

  3. SpringMVC 文件上传及下载

    文件下载 inline 访问资源时如果没有设置响应头Content-Disposition,浏览器默认按照inline进行处理 inline:能显示就显示,不能显示就下载 响应头 只需修改响应头Con ...

  4. SpringMvc文件上传和下载

    最近博主在做SpringMvc文件上传和下载的功能实现,上网查了很多资料很多都不太符合理想,找啊找,终于找到一个可以用的,然后再此基础上,我加以改进,可以支持多文件上传,而且代码非常精简,大家可以看看 ...

  5. 最全面的SpringMVC教程(五)——文件上传与下载

    前言 本文为 [SpringMVC教程]文件上传与下载 相关知识,具体将对使用MultipartResolver处理文件上传的步骤,两种文件下载方式(直接向response的输出流中写入对应的文件流. ...

  6. 使用SpringMVC框架实现文件上传和下载功能

    使用SpringMVC框架实现文件上传和下载功能 (一)单个文件上传 ①配置文件上传解释器 <!-配置文件上传解释器 --> <mvc:annotation-driven>&l ...

  7. SSM之SpringMVC 04 —— Ajax、拦截器、文件上传和下载

    系列文章 SSM之SpringMVC 01 -- SpringMVC原理及概念.Hello SpringMVC 注解版和配置版 SSM之SpringMVC 02 -- Controller和RestF ...

  8. layui上传文件请求接口异常_SpringMVC实现文件上传与下载,拦截器,异常处理

    第一章:响应数据和结果视图 1. 返回字符串 Controller方法返回字符串可以指定逻辑视图的名称,根据视图解析器为物理视图的地址. @RequestMapping(value="/he ...

  9. SpringBoot整合阿里云OSS文件上传、下载、查看、删除

    SpringBoot整合阿里云OSS文件上传.下载.查看.删除 该项目源码地址:https://github.com/ggb2312/springboot-integration-examples ( ...

最新文章

  1. FeignClientAutoConfiguration
  2. centos php 局域网访问,CentOS8安装搭建php环境
  3. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)
  4. free movie
  5. Java中的参数传递 --Java
  6. java doc转pdf_java 完美解决 ppt/pptx 转pdf 源码
  7. Struts 2基础
  8. springboot No Java compiler available for configuration options compilerClassName
  9. getFilterFromRunTimeService - what is the trigger point of data load
  10. linux脚本开机挂载,案例七:shell实现开机自动挂载本地YUM仓库程序
  11. 洛谷 P1313 计算系数
  12. 刚学unity3d,跟着仿作了flappy bird,记下一些琐碎的心得!
  13. Productivity Power Tools 动画演示(转)
  14. 如何区分真的工厂还是假的工厂
  15. 您如何轻松地水平居中 div 使用CSS? [重复]
  16. 2018C/C++蓝桥杯解析
  17. rsync aws ec2 pem
  18. Java太阳系行星运动模型
  19. MAXDOS网刻教程~~(虚拟机与物理机 / 两台或者多台电脑之间)
  20. 33个地区发iPhone5,老外纳闷中国没人排队_-Chaz-_新浪博客

热门文章

  1. shell每日一句(3)
  2. Java-6.5上机作业
  3. HUAWEI USG6000系列 NGFW Module V100R001 典型配置案例
  4. 移动端页面兼容性问题解决方案整理
  5. 2015.12.21 内存管理(memory management)
  6. [翻译] LASIImageView - 显示进度指示并异步下载图片
  7. Notepad++ 配置java编译环境
  8. 阅读作业二-----读Lost in CatB有感 by 李栋
  9. 山东建筑大学校内购物网(SdaiBuy.com )V1.2 Beta
  10. Nginx防止恶意解析-禁止通过IP访问网站