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 ...
随机推荐
- android中如何自动获取短信验证码
package com.loaderman.smsbroadcastreceiver; import java.util.regex.Matcher; import java.util.regex.P ...
- Computer Network Homework3’ s hard question
Computer Network Homework3’ s hard question 1. Which kind of protocol does CSMA belong to? A. Random ...
- [pipenv]Warning: Python 3.7 was not found on your system…
前置条件: 切换到pipfile文件所在目录gotest_official 问题描述: 使用pipenv install创建虚拟环境,报错 wangju@wangju-HP--G4:~/Desktop ...
- Selenium 2自动化测试实战20(操作cookie)
一.操作cookie 有时候我们需要验证浏览器中cookie是否正确,因为基于真实cookie的测试是无法通过白盒和集成测试的.WebDriver提供了操作Cookie的相关方法,可以读取.添加和删除 ...
- zabbix服务端安装
1.安装zabbix服务(1)先rpm安装lamp环境 yum install -y httpd mysql mysql-libs php php-mysql mysql-server php-bcm ...
- 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置
复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...
- 如何在Ubuntu / CentOS 6.x上安装Bugzilla 4.4
这里,我们将展示如何在一台Ubuntu 14.04或CentOS 6.5/7上安装Bugzilla.Bugzilla是一款基于web,用来记录跟踪缺陷数据库的bug跟踪软件,它同时是一款免费及开源软件 ...
- Tomcat启动报错:“通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明“
从报错信息就可以明显察觉到是xml配置文件出现的问题 <?xml version="1.0" encoding="UTF-8"?> <bean ...
- 微信小程序 解析html格式内容
需要引入html-view文件 1/js 代码 const HtmlParser=require('../../utils/html-view/index') data: { coupon_text: ...
- 在CentOS 8 Linux中安装使用Cockpit服务器管理软件
在本文中,我们将帮助您在CentOS 8服务器中安装Cockpit Web 控制台,以管理和监视本地系统以及网络环境中的Linux服务器.您还将学习如何将远程Linux主机添加到Cockpit并在Ce ...