原文地址:

http://www.cnblogs.com/WJ-163/p/6269409.html 上传参考

http://www.cnblogs.com/lonecloud/p/5990060.html 下载参考

一、关键步骤

①引入核心JAR文件

SpringMVC实现文件上传,需要再添加两个jar包。一个是文件上传的jar包,一个是其所依赖的IO包。这两个jar包,均在Spring支持库的org.apache.commons中。

②书写控制器方法

applicationContext.xml:

错误

index.jsp页面:需指定 enctype="multipart/form-data

1

2

3

4

5

6

7

<body>

<form action="${pageContext.request.contextPath }/first.do" method="post" enctype="multipart/form-data">

<h2>文件上传</h2>

文件:<input type="file" name="uploadFile"/><br/><br/>

<input type="submit" value="上传"/>

</form>

</body>

实现效果:  

二、没有选择要上传的文件&&限制文件上传类型

如果没有选择要上传的文件,可以通过如下判断代码回到错误页,并配置异常类

1

2

3

4

<!-- 配置异常类  报错 -->

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

<property name="defaultErrorView" value="/error.jsp"></property>

</bean>

 

三、多文件上传 

实现效果:

四、文件下载

1

<a href="${pageContext.request.contextPath }/download.do?line.jpg">下载</a>

实现效果:

下载不采用这种方式,参考下面这段代码:

  1. /**
  2.      * 文件下载
  3.      * @Description:
  4.      * @param fileName
  5.      * @param request
  6.      * @param response
  7.      * @return
  8.      */
  9.     @RequestMapping("/download")
  10.     public String downloadFile(@RequestParam("fileName") String fileName,
  11.             HttpServletRequest request, HttpServletResponse response) {
  12.         if (fileName != null) {
  13.             String realPath = request.getServletContext().getRealPath(
  14.                     "WEB-INF/File/");
  15.             File file = new File(realPath, fileName);
  16.             if (file.exists()) {
  17.                 response.setContentType("application/force-download");// 设置强制下载不打开
  18.                 response.addHeader("Content-Disposition",
  19.                         "attachment;fileName=" + fileName);// 设置文件名
  20.                 byte[] buffer = new byte[1024];
  21.                 FileInputStream fis = null;
  22.                 BufferedInputStream bis = null;
  23.                 try {
  24.                     fis = new FileInputStream(file);
  25.                     bis = new BufferedInputStream(fis);
  26.                     OutputStream os = response.getOutputStream();
  27.                     int i = bis.read(buffer);
  28.                     while (i != -1) {
  29.                         os.write(buffer, 0, i);
  30.                         i = bis.read(buffer);
  31.                     }
  32.                 } catch (Exception e) {
  33.                     // TODO: handle exception
  34.                     e.printStackTrace();
  35.                 } finally {
  36.                     if (bis != null) {
  37.                         try {
  38.                             bis.close();
  39.                         } catch (IOException e) {
  40.                             // TODO Auto-generated catch block
  41.                             e.printStackTrace();
  42.                         }
  43.                     }
  44.                     if (fis != null) {
  45.                         try {
  46.                             fis.close();
  47.                         } catch (IOException e) {
  48.                             // TODO Auto-generated catch block
  49.                             e.printStackTrace();
  50.                         }
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.         return null;
  56.     }

Spring MVC文件上传下载(转载)的更多相关文章

  1. Spring MVC文件上传下载工具类

    import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import ...

  2. Spring MVC 文件上传下载

    本文基于Spring MVC 注解,让Spring跑起来. (1) 导入jar包:ant.jar.commons-fileupload.jar.connom-io.jar. (2) 在src/cont ...

  3. 【Java Web开发学习】Spring MVC文件上传

    [Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...

  4. Spring MVC 笔记 —— Spring MVC 文件上传

    文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...

  5. Spring MVC文件上传教程 commons-io/commons-uploadfile

    Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...

  6. Spring mvc文件上传实现

    Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...

  7. Spring mvc 文件上传到文件夹(转载+心得)

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  8. Spring MVC 文件上传 & 文件下载

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...

  9. spring mvc 文件上传 ajax 异步上传

    异常代码: 1.the request doesn't contain a multipart/form-data or multipart/mixed stream, content type he ...

随机推荐

  1. [VBA]合并工作簿优化版

    Sub 合并工作簿数据()Dim arrDim i As Integer, j As Integer, x As IntegerDim f As String, m As String, n As S ...

  2. 47全排列II

    题目:给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入:[1,1,2]输出:[[1,1,2],[1,2,1],[2,1,1]] 来源:https://leetcode-cn.com ...

  3. 使用Nginx做WebSockets代理教程

    WebSocket 协议提供了一种创建支持客户端和服务端实时双向通信Web应用程序的方法.作为HTML5规范的一部分,WebSockets简化了开发Web实时通信程 序的难度.目前主流的浏览器都支持W ...

  4. spring aop影响dubbo返回值问题解决

    问题描述: dubbo服务已经注册,客户端调用提供者服务返回值为空.(考虑动态代理.aop的返回值影响,dubbo基于spring2.5.6.SEC03,本次开发使用的是spring4.3.8) 解决 ...

  5. java并发编程 线程基础

    java并发编程 线程基础 1. java中的多线程 java是天生多线程的,可以通过启动一个main方法,查看main方法启动的同时有多少线程同时启动 public class OnlyMain { ...

  6. LeetCode.1029-两城调度(Two City Scheduling)

    这是小川的第383次更新,第412篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第245题(顺位题号是1029).公司计划采访的人数为2N.将第i个人飞往城市A的费用是[ ...

  7. 【Deep Learning Nanodegree Foundation笔记】第 7 课:NEURAL NETWORKS Intro to Neural Networks

    In this lesson, you'll dive deeper into the intuition behind Logistic Regression and Neural Networks ...

  8. Jmeter+TCP\Sockets(8583)报文压力测试

    Jmeter一般被用来测试HTTP协议,我第一次拿来测试socket协议,pos机传输报文为8583,协议属于socket,也是TCP协议的一种,网上有LR怎么测试8583报文,我就研究了一下怎么用J ...

  9. kafka语句示例

    1.从http://kafka.apache.org/下载kafka安装包:2.tar zxvf kafka_2.8.0.tar.gz,修改配置文件conf/server.properties:bro ...

  10. Node.js使用redis进行订阅发布管理

    redis NPM 官方介绍地址:https://www.npmjs.com/package/redis let redis = require('redis'); let subscriber; l ...