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 ...
随机推荐
- np.unique()对一维和二维数组去重
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)[source] 一 ...
- python 提示 :OverflowError: Python int too large to convert to C long
一次在使用orm进行联表查询的时候,出现 Python int too large to convert to C long 的问题: 在分析错误之后,在错误最后面提示中有: File " ...
- mysql登录的三种方式
1.远程登录mysql 先授权:如:grant all on *.* to 'root'@'192.168.81.130' identified by '52033dd';查看是否生效:select ...
- 收货确定 BAPI BAPI_GOODSMVT_CREATE
CLEAR gmhead. gmhead-pstng_date = ls_table-gzdate."sy-datum . gmhead-doc_date = sy-datu ...
- 深入理解Istio核心组件之Pilot
Istio作为当前服务网格(Service Mesh)领域的事实标准,流量治理(Traffic Management)是其最为基础也最为重要的功能.本文将结合源码对Istio流量治理的实现主体——组件 ...
- css换行用省略号代替
css换行用省略号代替,也可以说是长标题的文章可以使用简单的CSS样式实现省略号控制显示. 一般的文字截断(适用于内联与块): .text-overflow{ display:block;/*内联对象 ...
- 移动手机端通过PC转接实现标签打印的解决方案
废话不多讲,由于种种原因项目上出现了移动手持录入标签信息通过pc端转接实现打印的需求,所以简单研究了一下,本来考虑使用webapi方式实现,但是发现这种方式调用打印机实现自动打印比较困难,所以转而求其 ...
- swagger-ui升级swagger-bootstrap-ui界面好看到起飞
如果项目已经集成了swagger,只需要在pom.xml添加,如果你的项目没有集成swagger,自行百度或看最下方的链接 swagger-bootstrap-ui是Swagger的前端UI实现,目的 ...
- Scratch少儿编程系列:(十)系列总结及后续计划
一.系列文章的来由 本篇为该系列文章的一个简单总结, 从初次接触Scratch开始,在写本系列文章过程中,一边读书,一边通过例子做练习. 技术实现,对于我跟人来说,没有什么难度. 我相信,对于一个初次 ...
- 给nginx添加客户端的请求最大单文件限制
在nginx.conf中添加如下. client_max_body_size 10m; #允许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲 ...