Spring MVC 文件上传 & 文件下载

时间:2022-12-13 00:11:00

索引:

目录索引

参看代码 GitHub:

pom.xml

WebConfig.java

index.jsp

upload.jsp

FileUploadController.java

Files_Utils_DG.java

一、要点讲解

  1.引入文件上传下载的类库

  commons-fileupload

  commons-io

  2.配置 MultipartResolver 组件(bean)

  @Bean public MultipartResolver multipartResolver() : 该组件用来 解析 http multipart 类型的请求体

  3.配置静态文件的请求

  @Override public void addResourceHandlers(ResourceHandlerRegistry registry) :

    该方法中注册请求资源路径,以便 href 链接中静态资源的请求链接与下载。

    registry.addResourceHandler("/files/**").addResourceLocations("classpath:/files/");

  4.添加页面快捷转向,这样就不用写没有逻辑仅做页面转向的 Controller 了

    @Override public void addViewControllers(ViewControllerRegistry registry):

      registry.addViewController("/upload").setViewName("fileupload/upload");

  5.前台表单中的设置

    enctype="multipart/form-data" :在 form 表单中必须指定为 multipart

    fileElementId:'file_AjaxFile' :在 ajax 中要指定 <input type="file"> 的 id

  6.后台中接收设置

    @RequestParam("file_upload") MultipartFile multipartFile : 在方法参数中这样指定后,就可以从请求体中读取上传的文件及文件信息了

  7.其它详细细节可具体参看代码,及代码中的解释

   ... ...

二、详细使用及代码

  1.pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>solution</artifactId>
<groupId>lm.solution</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>lm.solution</groupId>
<artifactId>web</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--Module-->
<dependency>
<groupId>lm.solution</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>lm.solution</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <!--Libary-->
<!--spring mvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--cglib-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<!--mybatis spring 插件 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!-- Mysql数据库驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- connection pool -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<!--<scope>runtime</scope>-->
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 映入JSON lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier>
</dependency>
<!-- 用dom4j解析xml文件 -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</dependency>
<!-- ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-web</artifactId>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<!-- common others -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- org.apache.httpcomponents -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
</dependency>
<!-- com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
<build>
<finalName>web</finalName> <plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat8-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

  2.WebConfig.java

 package lm.solution.web.config.configs;

 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lm.solution.common.web.messageconverter.CustomMessageConverter;
import lm.solution.web.config.beans.TimerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView; import java.util.ArrayList;
import java.util.List; @Configuration
/**
* @EnableWebMvc 注解会开启一些默认配置,如:ViewResolver MessageConverter 等,
* 若无此注解,重写 WebMvcConfigurerAdapter 方法无效
* */
@EnableWebMvc
@ComponentScan(value = {
"lm.solution.web",
"lm.solution.service.mysql",
"lm.solution.service.webtest"
})
/**
* 继承 WebMvcConfigurerAdapter 类,重写其方法可对 spring mvc 进行配置
* */
public class WebConfig extends WebMvcConfigurerAdapter { // 重写 addViewControllers 简化页面快捷转向,这样就可以不用配置 Controller 了
@Override
public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index");
registry.addViewController("/error").setViewName("error/error");
registry.addViewController("/excel").setViewName("excel/excel");
// 文件上传下载
registry.addViewController("/upload").setViewName("fileupload/upload");
registry.addViewController("/ImageValidateCodeLogin").setViewName("login/imageValidateCodeLogin");
registry.addViewController("/restfulapi").setViewName("restful/user");
registry.addViewController("/jaxwsri").setViewName("jaxwsri/wsri");
registry.addViewController("/redis").setViewName("redis/jedis");
registry.addViewController("/mybatisPage").setViewName("db/mybatis");
registry.addViewController("/messageconverter").setViewName("httpmessageconverter/customconverter");
registry.addViewController("/sse").setViewName("serverpushmessage/sse"); } // 配置JSP视图解析器
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
/**
* views 在 /resources/ 下
* */
// 前缀
viewResolver.setPrefix("/WEB-INF/classes/views/");
// 后缀
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
viewResolver.setContentType("text/html");
// 可以在JSP页面中通过${}访问beans
viewResolver.setExposeContextBeansAsAttributes(true);
return viewResolver;
} // 配置springMVC处理上传文件的信息
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
resolver.setMaxUploadSize(10485760000L);
resolver.setMaxInMemorySize(40960);
return resolver;
} // 配置静态文件处理
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){ /**
* addResourceHandler 指的是对外暴露的访问路径
* addResourceLocations 指的是文件放置的目录
* */
registry.addResourceHandler("/assets/**")
.addResourceLocations("classpath:/assets/"); // href 链接方式 下载文件
registry.addResourceHandler("/files/**")
.addResourceLocations("classpath:/files/"); /**
* 解决 No handler found for GET /favicon.ico 异常
* */
registry.addResourceHandler("/favicon.ico")
.addResourceLocations("classpath:/favicon.ico"); } // 重写 configurePathMatch ,改变路径参数匹配
@Override
public void configurePathMatch(PathMatchConfigurer configurer) { /**
* Spring mvc 默认 如果路径参数后面带点,如 “/mm/nn/xx.yy” 后面的yy值将被忽略
* 加入下面的配置,就不会忽略“.”后面的参数了
* */
configurer.setUseSuffixPatternMatch(false); } //
// // 负责读取二进制格式的数据和写出二进制格式的数据;
// @Bean
// public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
//
// return new ByteArrayHttpMessageConverter();
//
// }
//
// // 负责读取字符串格式的数据和写出字符串格式的数据;
// @Bean
// public StringHttpMessageConverter stringHttpMessageConverter() {
//
// StringHttpMessageConverter messageConverter = new StringHttpMessageConverter();
// messageConverter.setDefaultCharset(Charset.forName("UTF-8"));
// return messageConverter;
//
// }
//
// // 负责读取资源文件和写出资源文件数据;
// @Bean
// public ResourceHttpMessageConverter resourceHttpMessageConverter() {
//
// return new ResourceHttpMessageConverter();
//
// }
//
// /**
// * 负责读取form提交的数据
// * 能读取的数据格式为 application/x-www-form-urlencoded,
// * 不能读取multipart/form-data格式数据;
// * 负责写入application/x-www-from-urlencoded和multipart/form-data格式的数据;
// */
// @Bean
// public FormHttpMessageConverter formHttpMessageConverter() {
//
// return new FormHttpMessageConverter();
//
// }
//
// // 负责读取和写入json格式的数据;
// /**
// * 配置 fastjson 中实现 HttpMessageConverter 接口的转换器
// * FastJsonHttpMessageConverter 是 fastjson 中实现了 HttpMessageConverter 接口的类
// */
// @Bean(name = "fastJsonHttpMessageConverter")
// public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
// //
// String txtHtml = "text/html;charset=UTF-8";
// String txtJson = "text/json;charset=UTF-8";
// String appJson = "application/json;charset=UTF-8";
//
// // 这里顺序不能反,一定先写 text/html,不然 IE 下会出现下载提示
// List<MediaType> mediaTypes = new ArrayList<>();
// mediaTypes.add(MediaType.parseMediaType(txtHtml));
// mediaTypes.add(MediaType.parseMediaType(txtJson));
// mediaTypes.add(MediaType.parseMediaType(appJson));
//
// // 加入支持的媒体类型,返回 contentType
// FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
// fastjson.setSupportedMediaTypes(mediaTypes);
// return fastjson;
//
// }
//
// // 负责读取和写入 xml 中javax.xml.transform.Source定义的数据;
// @Bean
// public SourceHttpMessageConverter sourceHttpMessageConverter() {
//
// return new SourceHttpMessageConverter();
//
// }
//
// // 负责读取和写入xml 标签格式的数据;
// @Bean
// public Jaxb2RootElementHttpMessageConverter jaxb2RootElementHttpMessageConverter() {
//
// return new Jaxb2RootElementHttpMessageConverter();
//
// }
//
// // 注册消息转换器
// /**
// * 重写 configureMessageConverters 会覆盖掉 spring mvc 默认注册的多个 HttpMessageConverter
// * 所以 推荐使用 extendMessageConverter
// * */
// /**
// * Error:
// * 400:(错误请求) 服务器不理解请求的语法。
// * 415:(不支持的媒体类型) 请求的格式不受请求页面的支持。
// */
// @Override
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//
// converters.add(this.byteArrayHttpMessageConverter());
// converters.add(this.stringHttpMessageConverter());
// converters.add(this.resourceHttpMessageConverter());
// converters.add(this.formHttpMessageConverter());
// converters.add(this.fastJsonHttpMessageConverter());
// converters.add(this.sourceHttpMessageConverter());
// converters.add(this.jaxb2RootElementHttpMessageConverter());
//
// } // 自定义 HttpMessageConverter
@Bean
public CustomMessageConverter customMessageConverter(){ return new CustomMessageConverter(); } // 负责读取和写入json格式的数据;
/**
* 配置 fastjson 中实现 HttpMessageConverter 接口的转换器
* FastJsonHttpMessageConverter 是 fastjson 中实现了 HttpMessageConverter 接口的类
*/
@Bean(name = "fastJsonHttpMessageConverter")
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
//
String txtHtml = "text/html;charset=UTF-8";
String txtJson = "text/json;charset=UTF-8";
String appJson = "application/json;charset=UTF-8"; // 这里顺序不能反,一定先写 text/html,不然 IE 下会出现下载提示
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.parseMediaType(txtHtml));
mediaTypes.add(MediaType.parseMediaType(txtJson));
mediaTypes.add(MediaType.parseMediaType(appJson)); // 加入支持的媒体类型,返回 contentType
FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
fastjson.setSupportedMediaTypes(mediaTypes);
return fastjson; } /**
* 重写 extendMessageConverters 方法,仅添加自定义 HttpMessageConverter
* 不覆盖默认注册的 HttpMessageConverter
* */
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(customMessageConverter());
converters.add(fastJsonHttpMessageConverter()); } // 拦截器 bean
@Bean
public TimerInterceptor timerInterceptor(){ return new TimerInterceptor(); } // 重写 addInterceptors 注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(timerInterceptor()); } }

  3.index.jsp

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>LM Solution</title>
</head> <body>
<h3>Spring MVC 仿 Spring Boot</h3>
<a href="/excel"> Excel </a><br>
<a href="/upload">文件上传下载</a><br>
<a href="/ImageValidateCodeLogin">登录--图片验证码</a><br>
<a href="/restfulapi">Rest API</a><br>
<a href="/jaxwsri">Jax-Ws RI</a><br>
<a href="/redis">redis option by jedis</a><br>
<a href="/mybatisPage">MyBatis</a><br>
<a href="/advice/globalHandlerAdvice"> GlobalHandlerAdvice --> GlobalExceptionHandle --> ErrorPage </a><br>
<a href="/messageconverter"> 自定义 HttpMessageConverter </a><br>
<a href="/sse">Server Sent Event</a>
</body>
</html>

  4.upload.jsp

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>

 <html>
<head>
<title>File Upload & Download</title>
<script src="/assets/js/jquery-2.1.4.min.js"></script>
<script src="/assets/js/ajaxfileupload.js"></script>
</head>
<body>
<h3>文件上传</h3>
<h3>方式一:采用 fileUpload_multipartFile , file.transferTo 来保存上传的文件</h3>
<form name="form1"
action="/FileUpload/fileUpload_multipartFile"
method="post"
enctype="multipart/form-data">
<input type="file" name="file_upload">
<input type="submit" value="上传">
</form>
<hr>
<h3>方式二:采用 fileUpload_multipartRequest file.transferTo 来保存上传文件</h3>
<form name="form2"
action="/FileUpload/fileUpload_multipartRequest"
method="post"
enctype="multipart/form-data">
<input type="file" name="file_upload">
<input type="submit" value="上传">
</form>
<hr>
<h3>方式三:采用 CommonsMultipartResolver file.transferTo 来保存上传文件</h3>
<form name="form3"
action="/FileUpload/fileUpload_CommonsMultipartResolver"
method="post"
enctype="multipart/form-data">
<input type="file" name="file_upload">
<input type="submit" value="上传">
</form>
<hr>
<h3>方式四:通过流的方式上传文件</h3>
<form name="form4"
action="/FileUpload/fileUpload_stream"
method="post"
enctype="multipart/form-data">
<input type="file" name="file_upload">
<input type="submit" value="上传">
</form>
<hr>
<h3>方式五:通过ajax插件 ajaxfileupload.js 来异步上传文件</h3>
<h5>注:已上传成功,只是js返回时会报错。</h5>
<form name="form5"
enctype="multipart/form-data">
<input type="file" id="file_AjaxFile" name="file_AjaxFile">
<input type="button" value="上传" onclick="fileUploadAjax()" >
<span id="sp_AjaxFile"></span>
<br>
<%--上传进度:<span id="sp_fileUploadProgress">0%</span>--%>
</form>
<script type="text/javascript">
//
var progressInterval=null;
var i=0;
//
function fileUploadAjax() {
if($("#file_AjaxFile").val().length>0){
// progressInterval=setInterval(getProgress(),500);
$.ajaxFileUpload({
// 用于文件上传的服务器端请求地址
url:'/FileUpload/fileUpload_ajax',
type:'post',
// 一般设置为false
secureuri:false,
// 文件上传空间的id属性 <input type="file" id="file1" name="file" />
fileElementId:'file_AjaxFile',
// 返回值类型 一般设置为json
dataType:'application/json',
// 服务器成功响应处理函数
success:function (data) {
var jsonObject=eval('('+data+')');
$("#sp_AjaxFile").html("Upload Success ! filePath:"+jsonObject.filePath);
},
// 服务器响应失败处理函数
error:function (data,status,e) {
alert(e);
}
});
}else {
alert("请选择文件!");
}
}
</script>
<hr>
<h3>方式六:多文件上传 采用 MultipartFile[] multipartFile 上传文件方法</h3>
<h5>注:三个文件都选择上。</h5>
<form name="form5"
action="/FileUpload/fileUpload_spring_list"
method="post"
enctype="multipart/form-data">
<input type="file" name="file_upload">
<input type="file" name="file_upload">
<input type="file" name="file_upload">
<input type="submit" value="上传多个"/>
</form>
<hr>
<h3>方式七:通过 a 标签的方式进行文件下载</h3>
<a href="/files/download/mst.txt">通过 a 标签下载文件 mst.txt</a>
<hr>
<h3>方式八:通过 Response 文件流的方式下载文件</h3>
<a href="/FileUpload/fileDownload_servlet">通过 文件流 的方式下载文件 mst.txt</a>
</body>
</html>

  5.FileUploadController.java

 package lm.solution.web.controller.fileupload;

 import lm.solution.common.file.Files_Utils_DG;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator; /**
* 使用 MultipartFile 接收上传文件
* */
@Controller
@RequestMapping(value = "/FileUpload/*")
public class FileUploadController { /**
* 方式一
* 采用 fileUpload_multipartFile , file.transferTo 来保存上传的文件
* */
@ResponseBody
@RequestMapping(value = "fileUpload_multipartFile")
public String fileUpload_multipartFile(
HttpServletRequest request,
@RequestParam("file_upload") MultipartFile multipartFile){ // 调用保存文件的帮助类进行保存文件,并返回文件的相对路径
String filePath= Files_Utils_DG.FileUpload_transferTo_spring(request,multipartFile,"/files/upload"); return "{'TFMark':'true','Msg':'upload success !','filePath':'"+filePath+"'}"; } /**
* 方式二
* 采用 fileUpload_multipartRequest file.transferTo 来保存上传文件
* 参数不写 MultipartFile multipartFile 在request请求里面直接转成multipartRequest,从multipartRequest中获取到文件流
* */
@ResponseBody
@RequestMapping(value = "fileUpload_multipartRequest")
public String fileUpload_multipartRequest(HttpServletRequest request){ // 将request转成MultipartHttpServletRequest
MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest)request;
// 页面控件的文件流,对应页面控件 input file_upload
MultipartFile multipartFile=multipartRequest.getFile("file_upload");
// 调用保存文件的帮助类进行保存文件,并返回文件的相对路径
String filePath=Files_Utils_DG.FileUpload_transferTo_spring(request,multipartFile,"/files/upload"); return "{'TFMark':'true','Msg':'upload success !','filePath':'"+filePath+"'}"; } /**
* 方式三
* 采用 CommonsMultipartResolver file.transferTo 来保存上传文件
* 自动扫描全部的input表单
* */
@ResponseBody
@RequestMapping(value = "fileUpload_CommonsMultipartResolver")
public String fileUpload_CommonsMultipartResolver(HttpServletRequest request){ // 将当前上下文初始化给 CommonsMultipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext()); // 检查form中是否有enctype="multipart/form-data"
if(multipartResolver.isMultipart(request)){
// 将request变成多部分request
MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request;
// 获取multiRequest 中所有的文件名
Iterator iter=multipartRequest.getFileNames();
while (iter.hasNext()){
// 一次遍历所有文件
MultipartFile multipartFile=multipartRequest.getFile(iter.next().toString());
// 调用保存文件的帮助类进行保存文件,并返回文件的相对路径
String fileName=Files_Utils_DG.FileUpload_transferTo_spring(request,multipartFile,"/files/upload"); System.out.println(fileName);
}
} return "upload success !";
} /**
* 方式四
* 通过流的方式上传文件
* */
@ResponseBody
@RequestMapping("fileUpload_stream")
public String upFile(
HttpServletRequest request,
@RequestParam("file_upload") MultipartFile multipartFile){ String filePath=Files_Utils_DG.FilesUpload_stream(request,multipartFile,"/files/upload"); return "{'TFMark':'true','Msg':'upload success !','filePath':'"+filePath+"'}"; } /**
* 方式五
* 采用 fileUpload_ajax , file.transferTo 来保存上传的文件 异步
* */
@ResponseBody
@RequestMapping(
value = "fileUpload_ajax",
method = RequestMethod.POST,
produces = "application/json;charset=UTF-8")
public String fileUpload_ajax(
HttpServletRequest request,
@RequestParam("file_AjaxFile") MultipartFile multipartFile){ String filePath=Files_Utils_DG.FileUpload_transferTo_spring(request,multipartFile,"/files/upload"); return "{'TFMark':'true','Msg':'upload success !','filePath':'"+filePath+"'}"; } /**
* 方式六
* 多文件上传
* 采用 MultipartFile[] multipartFile 上传文件方法
* */
@ResponseBody
@RequestMapping(value = "fileUpload_spring_list")
public String fileUpload_spring_list(
HttpServletRequest request,
@RequestParam("file_upload") MultipartFile[] multipartFile){ // 判断file数组不能为空并且长度大于0
if(multipartFile!=null&& multipartFile.length>0){
// 循环获取file数组中得文件
try{
for (int i=0;i<multipartFile.length;i++){
MultipartFile file=multipartFile[i];
// 保存文件
String fileName=Files_Utils_DG.FileUpload_transferTo_spring(request,file,"/files/upload"); System.out.println(fileName);
} return "{'TFMark':'true','Msg':'upload success !'}";
}catch (Exception e){
return "{'TFMark':'false','Msg':'参数传递有误!'}";
}
} return "{'TFMark':'false','Msg':'参数传递有误!'}"; } /**
* 方式八
* 文件下载
*
* @param response
* */
@RequestMapping(value = "fileDownload_servlet")
public void fileDownload_servlet(
HttpServletRequest request,
HttpServletResponse response){ Files_Utils_DG.FilesDownload_stream(request,response,"/files/download/mst.txt"); } }

  6.Files_Utils_DG.java

 package lm.solution.common.file;

 import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID; public final class Files_Utils_DG { /**
* 构造函数私有,以使此类不可构造实例
*/
private Files_Utils_DG() {
throw new Error("The class Cannot be instance !");
} // 获取绝对路径
public static String getServerPath(
HttpServletRequest request,
String filePath) { return request.getSession().getServletContext().getRealPath(filePath); } // 获取一个以日期命名的文件夹名 ; example:20160912
public static String getDataPath() {
return new SimpleDateFormat("yyyyMMdd").format(new Date());
} // 检查文件夹是否存在,不存在新建一个
public static void checkDir(String savePath) {
File dir = new File(savePath);
if (!dir.exists() || !dir.isDirectory()) {
// 不能创建多层目录
//dir.mkdir();
// 创建多层目录
dir.mkdirs();
}
} // return an UUID Name parameter (suffix cover '.') example: ".jpg"、".txt"
public static String getUUIDName(String suffix) {
// make new file name
return UUID.randomUUID().toString() + suffix;
} // return server absolute path(real path) the style is “server absolute
// path/DataPath/UUIDName”filePath example "/files/Upload"
public static String getAndSetAbsolutePath(
HttpServletRequest request,
String filePath,
String suffix) { // example:F:/qixiao/files/Upload/20160912/
String savePath = getServerPath(request, filePath) + File.separator + getDataPath() + File.separator;
// check if the path has exist if not create it
checkDir(savePath);
return savePath + getUUIDName(suffix); } // get the relative path of files style is
// “/filePath/DataPath/UUIDName”filePath example "/files/Upload"
public static String getRelativePath(String filePath, String suffix) {
// example:/files/Upload/20160912/
return filePath + File.separator + getDataPath() + File.separator + getUUIDName(suffix);
} /**
* spring mvc files Upload method (transferTo method)
* MultipartFile use TransferTo method upload
*
* @param request HttpServletRequest
* @param multipartFile MultipartFile(spring)
* @param filePath filePath example "/files/Upload"
* @return
*/
public static String FileUpload_transferTo_spring(
HttpServletRequest request,
MultipartFile multipartFile,
String filePath) { if (multipartFile != null) {
// get files suffix
String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
// filePath+fileName the complex file Name
String absolutePath = getAndSetAbsolutePath(request, filePath, suffix);
// return relative Path
String relativePath = getRelativePath(filePath, suffix); try {
// save file
multipartFile.transferTo(new File(absolutePath)); return relativePath;
} catch (IOException ie) {
ie.printStackTrace();
return null;
} } else {
return null;
} } /**
* user stream type save files
*
* @param request HttpServletRequest
* @param multipartFile MultipartFile support CommonsMultipartFile file
* @param filePath filePath example "/files/Upload"
* @return
*/
public static String FilesUpload_stream(
HttpServletRequest request,
MultipartFile multipartFile,
String filePath) { if (multipartFile != null) {
// get files suffix
String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
// filePath+fileName the complex file Name
String absolutePath = getAndSetAbsolutePath(request, filePath, suffix);
// return relative Path
String relativePath = getRelativePath(filePath, suffix);
try {
InputStream inputStream = multipartFile.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(absolutePath);
// create a buffer
byte[] buffer = new byte[4096];
long fileSize = multipartFile.getSize(); if (fileSize <= buffer.length) {
buffer = new byte[(int) fileSize];
} int line = 0;
while ((line = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, line);
} fileOutputStream.close();
inputStream.close(); return relativePath; } catch (Exception e) {
e.printStackTrace();
}
} else {
return null;
} return null;
} /**
* @param request HttpServletRequest
* @param response HttpServletResponse
* @param filePath example "/filesOut/Download/mst.txt"
*
* @return
* */
public static void FilesDownload_stream(
HttpServletRequest request,
HttpServletResponse response,
String filePath){ // get server path (real path)
String realPath=request.getSession().getServletContext().getRealPath(filePath);
File file=new File(realPath);
String filenames=file.getName();
InputStream inputStream;
try{
inputStream=new BufferedInputStream(new FileInputStream(file));
byte[] buffer=new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
response.reset();
// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
response.addHeader("Content-Disposition","attachment;filename="+new String(filenames.replaceAll(" ","").getBytes("UTF-8"),"iso8859-1"));
response.addHeader("Content-Length",""+file.length());
OutputStream os=new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);
os.flush();
os.close();
}catch (Exception e){
e.printStackTrace();
} } }

                                         蒙

                                    2018-05-16 22:22 周三

Spring MVC 文件上传 & 文件下载的更多相关文章

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

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

  2. Spring MVC文件上传教程 commons-io&sol;commons-uploadfile

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

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

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

  4. Spring mvc文件上传实现

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

  5. Spring mvc 文件上传到文件夹(转载&plus;心得)

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

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

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

  7. spring mvc文件上传&lpar;单个文件上传&vert;多个文件上传&rpar;

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  8. Strut2 和Spring MVC 文件上传对比

    在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 (http://commons.apache.org/proper/commo ...

  9. 【Spring】Spring MVC文件上传--整合bootstrap-fileinput和jQuery-File-Upload

    前言 这里分享两个使用Spring MVC进行文件上传的简单示例, 分别整合bootstrap-fileinput 和 Jquery File Upload , 代码十分简单, 都是入门的示例,因此这 ...

随机推荐

  1. Thread&period;join简单介绍

    百度了一下,终于明白了.这个解释最简单: 前提:join()方法肯定是被某个线程调用的.   A线程正在执行,突然执行的时候碰到了B.join(), 那么,A线程就必须要等到B线程执行完之后才能执行. ...

  2. encodeURIComponent编码后java后台的解码 (AJAX中文解决方案)

    encodeURIComponent编码后java后台的解码 (AJAX中文解决方案) 同学的毕业设计出现JavaScript用encodeURIComponentt编码后无法再后台解码的问题. 原来 ...

  3. Dynamic Rankings ZOJ - 2112(主席树&plus;树状数组)

    The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...

  4. jQuery 知识点总结

    jQuery 是一个“写的更少,但做的更多”的轻量级JavaScript 库.对于网页开发者来说,学会jQuery是必要的.因为它让你了解业界最通用的技术,为将来学习更高级的库打下基础,并且确实可以很 ...

  5. 机器学习入门-数值特征-连续数据离散化&lpar;进行分段标记处理&rpar; 1&period;hist&lpar;Dataframe格式直接画直方图&rpar;

    函数说明: 1. .hist 对于Dataframe格式的数据,我们可以使用.hist直接画出直方图 对于一些像年龄和工资一样的连续数据,我们可以对其进行分段标记处理,使得这些连续的数据变成离散化 就 ...

  6. openresty &plus; lua 4、openresty kafka

    kafka 官网: https://kafka.apache.org/quickstart zookeeper 官网:https://zookeeper.apache.org/ kafka 运行需要 ...

  7. bzoj2616&colon; SPOJ PERIODNI——笛卡尔树&plus;DP

    不连续的处理很麻烦 导致序列DP又找不到优秀的子问题 自底向上考虑? 建立小根堆笛卡尔树 每个点的意义是:高度是(自己-father)的横着的极大矩形 子问题具有递归的优秀性质 f[i][j]i为根子 ...

  8. idea插件安装的通用操作

    序:今天下午看到一个bug,很神奇,粘出来大家看看 看到这个异常栈,有经验的或者查到的答案都是mapper.xml中哪个的方法配置错了,应替换parameterMap为parameterType, 奇 ...

  9. tsinsen A1067&period; Fibonacci数列整除问题 dp

    A1067. Fibonacci数列整除问题 时间限制:1.0s   内存限制:512.0MB   总提交次数:2796   AC次数:496   平均分:51.83 将本题分享到:     查看未格 ...

  10. 为什么很多大公司继续使用 Objective-C,不用 Swift

    为什么很多大公司继续使用 Objective-C,不用 Swift   我觉得这个问题最核心的原因就一点:历史包袱. 猿题库算是比较新兴的应用了,代码量级也是 10 万的级别.很多稍微有些年头的应用, ...