索引:
参看代码 GitHub:
一、要点讲解
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
13 4 8solution 5lm.solution 61.0-SNAPSHOT 74.0.0 9lm.solution 10web 11war 121.0-SNAPSHOT 13web Maven Webapp 14http://maven.apache.org 1516 17 15818 22lm.solution 19service 201.0-SNAPSHOT 2123 27 28 29 30lm.solution 24common 251.0-SNAPSHOT 2631 34 35org.springframework 32spring-webmvc 3336 39 40cglib 37cglib 3841 44 45org.mybatis 42mybatis 4346 49 50org.mybatis 47mybatis-spring 4851 54 55mysql 52mysql-connector-java 5356 60 61com.alibaba 57druid 58 5962 66javax.servlet 63javax.servlet-api 64provided 6567 71javax.servlet.jsp 68jsp-api 69provided 7072 75 76javax.servlet 73jstl 7477 81 82net.sf.json-lib 78json-lib 79jdk15 8083 86 87dom4j 84dom4j 8588 91net.sf.ehcache 89ehcache-core 9092 95 96net.sf.ehcache 93ehcache-web 9497 100commons-fileupload 98commons-fileupload 99101 104 105commons-io 102commons-io 103106 109commons-codec 107commons-codec 108110 113org.apache.commons 111commons-collections4 112114 117 118org.apache.commons 115commons-lang3 116119 128 129commons-beanutils 120commons-beanutils 121122 127123 126commons-logging 124commons-logging 125130 133 134org.freemarker 131freemarker 132135 138org.apache.httpcomponents 136httpcore 137139 142 143org.apache.httpcomponents 140httpclient 141144 147 148redis.clients 145jedis 146149 152 153com.rabbitmq 150amqp-client 151154 157com.alibaba 155fastjson 156159 168web 160 161162 167163 166org.apache.tomcat.maven 164tomcat8-maven-plugin 165
2.WebConfig.java
1 package lm.solution.web.config.configs; 2 3 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 4 import lm.solution.common.web.messageconverter.CustomMessageConverter; 5 import lm.solution.web.config.beans.TimerInterceptor; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.ComponentScan; 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.http.MediaType; 10 import org.springframework.http.converter.HttpMessageConverter; 11 import org.springframework.web.multipart.MultipartResolver; 12 import org.springframework.web.multipart.commons.CommonsMultipartResolver; 13 import org.springframework.web.servlet.config.annotation.*; 14 import org.springframework.web.servlet.view.InternalResourceViewResolver; 15 import org.springframework.web.servlet.view.JstlView; 16 17 import java.util.ArrayList; 18 import java.util.List; 19 20 @Configuration 21 /** 22 * @EnableWebMvc 注解会开启一些默认配置,如:ViewResolver MessageConverter 等, 23 * 若无此注解,重写 WebMvcConfigurerAdapter 方法无效 24 * */ 25 @EnableWebMvc 26 @ComponentScan(value = { 27 "lm.solution.web", 28 "lm.solution.service.mysql", 29 "lm.solution.service.webtest" 30 }) 31 /** 32 * 继承 WebMvcConfigurerAdapter 类,重写其方法可对 spring mvc 进行配置 33 * */ 34 public class WebConfig extends WebMvcConfigurerAdapter { 35 36 // 重写 addViewControllers 简化页面快捷转向,这样就可以不用配置 Controller 了 37 @Override 38 public void addViewControllers(ViewControllerRegistry registry) { 39 40 registry.addViewController("/").setViewName("index"); 41 registry.addViewController("/error").setViewName("error/error"); 42 registry.addViewController("/excel").setViewName("excel/excel"); 43 // 文件上传下载 44 registry.addViewController("/upload").setViewName("fileupload/upload"); 45 registry.addViewController("/ImageValidateCodeLogin").setViewName("login/imageValidateCodeLogin"); 46 registry.addViewController("/restfulapi").setViewName("restful/user"); 47 registry.addViewController("/jaxwsri").setViewName("jaxwsri/wsri"); 48 registry.addViewController("/redis").setViewName("redis/jedis"); 49 registry.addViewController("/mybatisPage").setViewName("db/mybatis"); 50 registry.addViewController("/messageconverter").setViewName("httpmessageconverter/customconverter"); 51 registry.addViewController("/sse").setViewName("serverpushmessage/sse"); 52 53 } 54 55 // 配置JSP视图解析器 56 @Bean 57 public InternalResourceViewResolver viewResolver() { 58 InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 59 /** 60 * views 在 /resources/ 下 61 * */ 62 // 前缀 63 viewResolver.setPrefix("/WEB-INF/classes/views/"); 64 // 后缀 65 viewResolver.setSuffix(".jsp"); 66 viewResolver.setViewClass(JstlView.class); 67 viewResolver.setContentType("text/html"); 68 // 可以在JSP页面中通过${}访问beans 69 viewResolver.setExposeContextBeansAsAttributes(true); 70 return viewResolver; 71 } 72 73 // 配置springMVC处理上传文件的信息 74 @Bean 75 public MultipartResolver multipartResolver() { 76 CommonsMultipartResolver resolver = new CommonsMultipartResolver(); 77 resolver.setDefaultEncoding("UTF-8"); 78 resolver.setMaxUploadSize(10485760000L); 79 resolver.setMaxInMemorySize(40960); 80 return resolver; 81 } 82 83 // 配置静态文件处理 84 @Override 85 public void addResourceHandlers(ResourceHandlerRegistry registry){ 86 87 /** 88 * addResourceHandler 指的是对外暴露的访问路径 89 * addResourceLocations 指的是文件放置的目录 90 * */ 91 registry.addResourceHandler("/assets/**") 92 .addResourceLocations("classpath:/assets/"); 93 94 // href 链接方式 下载文件 95 registry.addResourceHandler("/files/**") 96 .addResourceLocations("classpath:/files/"); 97 98 /** 99 * 解决 No handler found for GET /favicon.ico 异常100 * */101 registry.addResourceHandler("/favicon.ico")102 .addResourceLocations("classpath:/favicon.ico");103 104 }105 106 // 重写 configurePathMatch ,改变路径参数匹配107 @Override108 public void configurePathMatch(PathMatchConfigurer configurer) {109 110 /**111 * Spring mvc 默认 如果路径参数后面带点,如 “/mm/nn/xx.yy” 后面的yy值将被忽略112 * 加入下面的配置,就不会忽略“.”后面的参数了113 * */114 configurer.setUseSuffixPatternMatch(false);115 116 }117 118 //119 // // 负责读取二进制格式的数据和写出二进制格式的数据;120 // @Bean121 // public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {122 //123 // return new ByteArrayHttpMessageConverter();124 //125 // }126 //127 // // 负责读取字符串格式的数据和写出字符串格式的数据;128 // @Bean129 // public StringHttpMessageConverter stringHttpMessageConverter() {130 //131 // StringHttpMessageConverter messageConverter = new StringHttpMessageConverter();132 // messageConverter.setDefaultCharset(Charset.forName("UTF-8"));133 // return messageConverter;134 //135 // }136 //137 // // 负责读取资源文件和写出资源文件数据;138 // @Bean139 // public ResourceHttpMessageConverter resourceHttpMessageConverter() {140 //141 // return new ResourceHttpMessageConverter();142 //143 // }144 //145 // /**146 // * 负责读取form提交的数据147 // * 能读取的数据格式为 application/x-www-form-urlencoded,148 // * 不能读取multipart/form-data格式数据;149 // * 负责写入application/x-www-from-urlencoded和multipart/form-data格式的数据;150 // */151 // @Bean152 // public FormHttpMessageConverter formHttpMessageConverter() {153 //154 // return new FormHttpMessageConverter();155 //156 // }157 //158 // // 负责读取和写入json格式的数据;159 // /**160 // * 配置 fastjson 中实现 HttpMessageConverter 接口的转换器161 // * FastJsonHttpMessageConverter 是 fastjson 中实现了 HttpMessageConverter 接口的类162 // */163 // @Bean(name = "fastJsonHttpMessageConverter")164 // public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {165 // //166 // String txtHtml = "text/html;charset=UTF-8";167 // String txtJson = "text/json;charset=UTF-8";168 // String appJson = "application/json;charset=UTF-8";169 //170 // // 这里顺序不能反,一定先写 text/html,不然 IE 下会出现下载提示171 // ListmediaTypes = new ArrayList<>();172 // mediaTypes.add(MediaType.parseMediaType(txtHtml));173 // mediaTypes.add(MediaType.parseMediaType(txtJson));174 // mediaTypes.add(MediaType.parseMediaType(appJson));175 //176 // // 加入支持的媒体类型,返回 contentType177 // FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();178 // fastjson.setSupportedMediaTypes(mediaTypes);179 // return fastjson;180 //181 // }182 //183 // // 负责读取和写入 xml 中javax.xml.transform.Source定义的数据;184 // @Bean185 // public SourceHttpMessageConverter sourceHttpMessageConverter() {186 //187 // return new SourceHttpMessageConverter();188 //189 // }190 //191 // // 负责读取和写入xml 标签格式的数据;192 // @Bean193 // public Jaxb2RootElementHttpMessageConverter jaxb2RootElementHttpMessageConverter() {194 //195 // return new Jaxb2RootElementHttpMessageConverter();196 //197 // }198 //199 // // 注册消息转换器200 // /**201 // * 重写 configureMessageConverters 会覆盖掉 spring mvc 默认注册的多个 HttpMessageConverter202 // * 所以 推荐使用 extendMessageConverter203 // * */204 // /**205 // * Error:206 // * 400:(错误请求) 服务器不理解请求的语法。207 // * 415:(不支持的媒体类型) 请求的格式不受请求页面的支持。208 // */209 // @Override210 // public void configureMessageConverters(List > converters) {211 //212 // converters.add(this.byteArrayHttpMessageConverter());213 // converters.add(this.stringHttpMessageConverter());214 // converters.add(this.resourceHttpMessageConverter());215 // converters.add(this.formHttpMessageConverter());216 // converters.add(this.fastJsonHttpMessageConverter());217 // converters.add(this.sourceHttpMessageConverter());218 // converters.add(this.jaxb2RootElementHttpMessageConverter());219 //220 // }221 222 // 自定义 HttpMessageConverter223 @Bean224 public CustomMessageConverter customMessageConverter(){225 226 return new CustomMessageConverter();227 228 }229 230 // 负责读取和写入json格式的数据;231 /**232 * 配置 fastjson 中实现 HttpMessageConverter 接口的转换器233 * FastJsonHttpMessageConverter 是 fastjson 中实现了 HttpMessageConverter 接口的类234 */235 @Bean(name = "fastJsonHttpMessageConverter")236 public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {237 //238 String txtHtml = "text/html;charset=UTF-8";239 String txtJson = "text/json;charset=UTF-8";240 String appJson = "application/json;charset=UTF-8";241 242 // 这里顺序不能反,一定先写 text/html,不然 IE 下会出现下载提示243 List mediaTypes = new ArrayList<>();244 mediaTypes.add(MediaType.parseMediaType(txtHtml));245 mediaTypes.add(MediaType.parseMediaType(txtJson));246 mediaTypes.add(MediaType.parseMediaType(appJson));247 248 // 加入支持的媒体类型,返回 contentType249 FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();250 fastjson.setSupportedMediaTypes(mediaTypes);251 return fastjson;252 253 }254 255 /**256 * 重写 extendMessageConverters 方法,仅添加自定义 HttpMessageConverter257 * 不覆盖默认注册的 HttpMessageConverter258 * */259 @Override260 public void extendMessageConverters(List > converters) {261 262 converters.add(customMessageConverter());263 converters.add(fastJsonHttpMessageConverter());264 265 }266 267 // 拦截器 bean268 @Bean269 public TimerInterceptor timerInterceptor(){270 271 return new TimerInterceptor();272 273 }274 275 // 重写 addInterceptors 注册拦截器276 @Override277 public void addInterceptors(InterceptorRegistry registry) {278 279 registry.addInterceptor(timerInterceptor());280 281 }282 283 }
3.index.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 3 4LM Solution 5 6 7 8Spring MVC 仿 Spring Boot
9 Excel 10 文件上传下载11 登录--图片验证码12 Rest API13 Jax-Ws RI14 redis option by jedis15 MyBatis16 GlobalHandlerAdvice --> GlobalExceptionHandle --> ErrorPage 17 自定义 HttpMessageConverter 18 Server Sent Event19 20
4.upload.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 3 4 519File Upload & Download 6 7 8 9 10文件上传
11方式一:采用 fileUpload_multipartFile , file.transferTo 来保存上传的文件
12
20
方式二:采用 fileUpload_multipartRequest file.transferTo 来保存上传文件
21 2829
方式三:采用 CommonsMultipartResolver file.transferTo 来保存上传文件
30 3738
方式四:通过流的方式上传文件
39 4647
方式五:通过ajax插件 ajaxfileupload.js 来异步上传文件
48注:已上传成功,只是js返回时会报错。
49 57 9091
方式六:多文件上传 采用 MultipartFile[] multipartFile 上传文件方法
92注:三个文件都选择上。
93 102103
方式七:通过 a 标签的方式进行文件下载
104 通过 a 标签下载文件 mst.txt105106
方式八:通过 Response 文件流的方式下载文件
107 通过 文件流 的方式下载文件 mst.txt108 1095.FileUploadController.java
1 package lm.solution.web.controller.fileupload; 2 3 import lm.solution.common.file.Files_Utils_DG; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 import org.springframework.web.bind.annotation.RequestParam; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 import org.springframework.web.multipart.MultipartFile; 10 import org.springframework.web.multipart.MultipartHttpServletRequest; 11 import org.springframework.web.multipart.commons.CommonsMultipartResolver; 12 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 import java.util.Iterator; 16 17 18 /** 19 * 使用 MultipartFile 接收上传文件 20 * */ 21 @Controller 22 @RequestMapping(value = "/FileUpload/*") 23 public class FileUploadController { 24 25 /** 26 * 方式一 27 * 采用 fileUpload_multipartFile , file.transferTo 来保存上传的文件 28 * */ 29 @ResponseBody 30 @RequestMapping(value = "fileUpload_multipartFile") 31 public String fileUpload_multipartFile( 32 HttpServletRequest request, 33 @RequestParam("file_upload") MultipartFile multipartFile){ 34 35 // 调用保存文件的帮助类进行保存文件,并返回文件的相对路径 36 String filePath= Files_Utils_DG.FileUpload_transferTo_spring(request,multipartFile,"/files/upload"); 37 38 return "{'TFMark':'true','Msg':'upload success !','filePath':'"+filePath+"'}"; 39 40 } 41 42 /** 43 * 方式二 44 * 采用 fileUpload_multipartRequest file.transferTo 来保存上传文件 45 * 参数不写 MultipartFile multipartFile 在request请求里面直接转成multipartRequest,从multipartRequest中获取到文件流 46 * */ 47 @ResponseBody 48 @RequestMapping(value = "fileUpload_multipartRequest") 49 public String fileUpload_multipartRequest(HttpServletRequest request){ 50 51 // 将request转成MultipartHttpServletRequest 52 MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest)request; 53 // 页面控件的文件流,对应页面控件 input file_upload 54 MultipartFile multipartFile=multipartRequest.getFile("file_upload"); 55 // 调用保存文件的帮助类进行保存文件,并返回文件的相对路径 56 String filePath=Files_Utils_DG.FileUpload_transferTo_spring(request,multipartFile,"/files/upload"); 57 58 return "{'TFMark':'true','Msg':'upload success !','filePath':'"+filePath+"'}"; 59 60 } 61 62 /** 63 * 方式三 64 * 采用 CommonsMultipartResolver file.transferTo 来保存上传文件 65 * 自动扫描全部的input表单 66 * */ 67 @ResponseBody 68 @RequestMapping(value = "fileUpload_CommonsMultipartResolver") 69 public String fileUpload_CommonsMultipartResolver(HttpServletRequest request){ 70 71 // 将当前上下文初始化给 CommonsMultipartResolver (多部分解析器) 72 CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext()); 73 74 // 检查form中是否有enctype="multipart/form-data" 75 if(multipartResolver.isMultipart(request)){ 76 // 将request变成多部分request 77 MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request; 78 // 获取multiRequest 中所有的文件名 79 Iterator iter=multipartRequest.getFileNames(); 80 while (iter.hasNext()){ 81 // 一次遍历所有文件 82 MultipartFile multipartFile=multipartRequest.getFile(iter.next().toString()); 83 // 调用保存文件的帮助类进行保存文件,并返回文件的相对路径 84 String fileName=Files_Utils_DG.FileUpload_transferTo_spring(request,multipartFile,"/files/upload"); 85 86 System.out.println(fileName); 87 } 88 } 89 90 return "upload success !"; 91 } 92 93 /** 94 * 方式四 95 * 通过流的方式上传文件 96 * */ 97 @ResponseBody 98 @RequestMapping("fileUpload_stream") 99 public String upFile(100 HttpServletRequest request,101 @RequestParam("file_upload") MultipartFile multipartFile){102 103 String filePath=Files_Utils_DG.FilesUpload_stream(request,multipartFile,"/files/upload");104 105 return "{'TFMark':'true','Msg':'upload success !','filePath':'"+filePath+"'}";106 107 }108 109 /**110 * 方式五111 * 采用 fileUpload_ajax , file.transferTo 来保存上传的文件 异步112 * */113 @ResponseBody114 @RequestMapping(115 value = "fileUpload_ajax",116 method = RequestMethod.POST,117 produces = "application/json;charset=UTF-8")118 public String fileUpload_ajax(119 HttpServletRequest request,120 @RequestParam("file_AjaxFile") MultipartFile multipartFile){121 122 String filePath=Files_Utils_DG.FileUpload_transferTo_spring(request,multipartFile,"/files/upload");123 124 return "{'TFMark':'true','Msg':'upload success !','filePath':'"+filePath+"'}";125 126 }127 128 /**129 * 方式六130 * 多文件上传131 * 采用 MultipartFile[] multipartFile 上传文件方法132 * */133 @ResponseBody134 @RequestMapping(value = "fileUpload_spring_list")135 public String fileUpload_spring_list(136 HttpServletRequest request,137 @RequestParam("file_upload") MultipartFile[] multipartFile){138 139 // 判断file数组不能为空并且长度大于0140 if(multipartFile!=null&& multipartFile.length>0){141 // 循环获取file数组中得文件142 try{143 for (int i=0;i
6.Files_Utils_DG.java
1 package lm.solution.common.file; 2 3 import org.springframework.web.multipart.MultipartFile; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 import java.io.*; 7 import java.text.SimpleDateFormat; 8 import java.util.Date; 9 import java.util.UUID; 10 11 public final class Files_Utils_DG { 12 13 /** 14 * 构造函数私有,以使此类不可构造实例 15 */ 16 private Files_Utils_DG() { 17 throw new Error("The class Cannot be instance !"); 18 } 19 20 // 获取绝对路径 21 public static String getServerPath( 22 HttpServletRequest request, 23 String filePath) { 24 25 return request.getSession().getServletContext().getRealPath(filePath); 26 27 } 28 29 // 获取一个以日期命名的文件夹名 ; example:20160912 30 public static String getDataPath() { 31 return new SimpleDateFormat("yyyyMMdd").format(new Date()); 32 } 33 34 // 检查文件夹是否存在,不存在新建一个 35 public static void checkDir(String savePath) { 36 File dir = new File(savePath); 37 if (!dir.exists() || !dir.isDirectory()) { 38 // 不能创建多层目录 39 //dir.mkdir(); 40 // 创建多层目录 41 dir.mkdirs(); 42 } 43 } 44 45 // return an UUID Name parameter (suffix cover '.') example: ".jpg"、".txt" 46 public static String getUUIDName(String suffix) { 47 // make new file name 48 return UUID.randomUUID().toString() + suffix; 49 } 50 51 // return server absolute path(real path) the style is “server absolute 52 // path/DataPath/UUIDName”filePath example "/files/Upload" 53 public static String getAndSetAbsolutePath( 54 HttpServletRequest request, 55 String filePath, 56 String suffix) { 57 58 // example:F:/qixiao/files/Upload/20160912/ 59 String savePath = getServerPath(request, filePath) + File.separator + getDataPath() + File.separator; 60 // check if the path has exist if not create it 61 checkDir(savePath); 62 return savePath + getUUIDName(suffix); 63 64 } 65 66 // get the relative path of files style is 67 // “/filePath/DataPath/UUIDName”filePath example "/files/Upload" 68 public static String getRelativePath(String filePath, String suffix) { 69 // example:/files/Upload/20160912/ 70 return filePath + File.separator + getDataPath() + File.separator + getUUIDName(suffix); 71 } 72 73 /** 74 * spring mvc files Upload method (transferTo method) 75 * MultipartFile use TransferTo method upload 76 * 77 * @param request HttpServletRequest 78 * @param multipartFile MultipartFile(spring) 79 * @param filePath filePath example "/files/Upload" 80 * @return 81 */ 82 public static String FileUpload_transferTo_spring( 83 HttpServletRequest request, 84 MultipartFile multipartFile, 85 String filePath) { 86 87 if (multipartFile != null) { 88 // get files suffix 89 String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")); 90 // filePath+fileName the complex file Name 91 String absolutePath = getAndSetAbsolutePath(request, filePath, suffix); 92 // return relative Path 93 String relativePath = getRelativePath(filePath, suffix); 94 95 try { 96 // save file 97 multipartFile.transferTo(new File(absolutePath)); 98 99 return relativePath;100 } catch (IOException ie) {101 ie.printStackTrace();102 return null;103 }104 105 } else {106 return null;107 }108 109 }110 111 /**112 * user stream type save files113 *114 * @param request HttpServletRequest115 * @param multipartFile MultipartFile support CommonsMultipartFile file116 * @param filePath filePath example "/files/Upload"117 * @return118 */119 public static String FilesUpload_stream(120 HttpServletRequest request,121 MultipartFile multipartFile,122 String filePath) {123 124 if (multipartFile != null) {125 // get files suffix126 String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));127 // filePath+fileName the complex file Name128 String absolutePath = getAndSetAbsolutePath(request, filePath, suffix);129 // return relative Path130 String relativePath = getRelativePath(filePath, suffix);131 try {132 InputStream inputStream = multipartFile.getInputStream();133 FileOutputStream fileOutputStream = new FileOutputStream(absolutePath);134 // create a buffer135 byte[] buffer = new byte[4096];136 long fileSize = multipartFile.getSize();137 138 if (fileSize <= buffer.length) {139 buffer = new byte[(int) fileSize];140 }141 142 int line = 0;143 while ((line = inputStream.read(buffer)) > 0) {144 fileOutputStream.write(buffer, 0, line);145 }146 147 fileOutputStream.close();148 inputStream.close();149 150 return relativePath;151 152 } catch (Exception e) {153 e.printStackTrace();154 }155 } else {156 return null;157 }158 159 return null;160 }161 162 /**163 * @param request HttpServletRequest164 * @param response HttpServletResponse165 * @param filePath example "/filesOut/Download/mst.txt"166 *167 * @return168 * */169 public static void FilesDownload_stream(170 HttpServletRequest request,171 HttpServletResponse response,172 String filePath){173 174 // get server path (real path)175 String realPath=request.getSession().getServletContext().getRealPath(filePath);176 File file=new File(realPath);177 String filenames=file.getName();178 InputStream inputStream;179 try{180 inputStream=new BufferedInputStream(new FileInputStream(file));181 byte[] buffer=new byte[inputStream.available()];182 inputStream.read(buffer);183 inputStream.close();184 response.reset();185 // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名186 response.addHeader("Content-Disposition","attachment;filename="+new String(filenames.replaceAll(" ","").getBytes("UTF-8"),"iso8859-1"));187 response.addHeader("Content-Length",""+file.length());188 OutputStream os=new BufferedOutputStream(response.getOutputStream());189 response.setContentType("application/octet-stream");190 os.write(buffer);191 os.flush();192 os.close();193 }catch (Exception e){194 e.printStackTrace();195 }196 197 }198 199 200 201 }
蒙
2018-05-16 22:22 周三