Spring MVC文件上传下载(转载)
原文地址:
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> |
实现效果:


下载不采用这种方式,参考下面这段代码:
- /**
- * 文件下载
- * @Description:
- * @param fileName
- * @param request
- * @param response
- * @return
- */
- @RequestMapping("/download")
- public String downloadFile(@RequestParam("fileName") String fileName,
- HttpServletRequest request, HttpServletResponse response) {
- if (fileName != null) {
- String realPath = request.getServletContext().getRealPath(
- "WEB-INF/File/");
- File file = new File(realPath, fileName);
- if (file.exists()) {
- response.setContentType("application/force-download");// 设置强制下载不打开
- response.addHeader("Content-Disposition",
- "attachment;fileName=" + fileName);// 设置文件名
- byte[] buffer = new byte[1024];
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- try {
- fis = new FileInputStream(file);
- bis = new BufferedInputStream(fis);
- OutputStream os = response.getOutputStream();
- int i = bis.read(buffer);
- while (i != -1) {
- os.write(buffer, 0, i);
- i = bis.read(buffer);
- }
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- } finally {
- if (bis != null) {
- try {
- bis.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
- return null;
- }
Spring MVC文件上传下载(转载)的更多相关文章
- Spring MVC文件上传下载工具类
import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import ...
- Spring MVC 文件上传下载
本文基于Spring MVC 注解,让Spring跑起来. (1) 导入jar包:ant.jar.commons-fileupload.jar.connom-io.jar. (2) 在src/cont ...
- 【Java Web开发学习】Spring MVC文件上传
[Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...
- Spring MVC 笔记 —— Spring MVC 文件上传
文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...
- Spring MVC文件上传教程 commons-io/commons-uploadfile
Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...
- Spring mvc文件上传实现
Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...
- Spring mvc 文件上传到文件夹(转载+心得)
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- Spring MVC 文件上传 & 文件下载
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...
- spring mvc 文件上传 ajax 异步上传
异常代码: 1.the request doesn't contain a multipart/form-data or multipart/mixed stream, content type he ...
随机推荐
- nodejs之express中间件body-parser使用
1.安装express和body-parser npm install express npm install body-parser 2‘.案例如下 var express = require('e ...
- 解决Prism中Region的GetView不起作用问题
通常情况下在Region中添加View时我们需要先判断View是否在Region中已存在,但如果我们在Region.Add的方法调用不当时,我们在GetView中始终返回Null,原因自然是Add时出 ...
- Python学习笔记:time模块的使用
在使用python的过程中,很多情况下会使用到日期时间,在Python的自建函数中,包含time模块,用来处理与日期时间相关的功能. 1.time.time() time():不能传参数 用来获取时间 ...
- Tensorflow-gpu搭建CUDA 10.0与cuDNN等版本问题
https://blog.csdn.net/weixin_42718092/article/details/85001140
- playbook部署mangodb
playbook文件 [root@localhost ~]# cat deploy_mongo.yaml --- - hosts: webservers become: yes become_meth ...
- 使用boost库获取文件夹下所有文件名字
最近整理项目发现一个曾经找了好久的有用的代码片段,就是获取文件夹下所有文件的名字,和当前文件的绝对路径. 记录一下. 使用的是boost库, #include <boost/filesystem ...
- Python学习之并发基础知识
8 并发编程 8.1 基础知识 8.1.1 操作系统的定义 操作系统是存在于硬件与软件之间,管理.协调.调度软件与硬件的交互. 资源管理解决物理资源数量不足和合理分配资源这两个问题, 通俗来说,操作系 ...
- 【神经网络与深度学习】如何将别人训练好的model用到自己的数据上
caffe团队用imagenet图片进行训练,迭代30多万次,训练出来一个model.这个model将图片分为1000类,应该是目前为止最好的图片分类model了. 假设我现在有一些自己的图片想进行分 ...
- 异常1(Exception)
父类 :Throwable(可抛出的) 有两个子类:Error(错误) Exception(异常) Error是所有错误类的父类,Exception是所有异常类的父类. 如图所示: 格式: ...
- HDU 1865 1sting (递推、大数)
1sting Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...