Spring MVC文件上传下载工具类
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver; /**
* @Description 操作文件工具类
* @author LJ
* @Date 2016年6月8日 上午11:42:44
* @Version v1.0
*/
public final class OperationFileUtil {
private static final String ENCODING = "utf-8"; /**
* 文件下载
*
* @param filePath
* 文件路径
* @return
* @throws UnsupportedEncodingException
* @throws IOException
*/
public static ResponseEntity<byte[]> download(String filePath) throws UnsupportedEncodingException, IOException {
String fileName = FilenameUtils.getName(filePath);
return downloadAssist(filePath, fileName);
} /**
* 文件下载
*
* @param filePath
* 文件路径
* @param fileName
* 文件名
* @return
* @throws UnsupportedEncodingException
* @throws IOException
*/
public static ResponseEntity<byte[]> download(String filePath, String fileName) throws UnsupportedEncodingException, IOException {
return downloadAssist(filePath, fileName);
} /**
* 文件下载辅助
*
* @param filePath
* 文件路径
* @param fileName
* 文件名
* @return
* @throws UnsupportedEncodingException
* @throws IOException
*/
private static ResponseEntity<byte[]> downloadAssist(String filePath, String fileName) throws UnsupportedEncodingException, IOException {
File file = new File(filePath);
if (!file.isFile() || !file.exists()) {
throw new IllegalArgumentException("filePath 参数必须是真实存在的文件路径:" + filePath);
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", URLEncoder.encode(fileName, ENCODING));
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
} /**
* 多文件上传
*
* @param request
* 当前上传的请求
* @param basePath
* 保存文件的路径
* @throws IOException
* @throws IllegalStateException
* @return Map<String, String> 返回上传文件的保存路径 以文件名做map的key;文件保存路径作为map的value
*/
public static Map<String, String> multiFileUpload(HttpServletRequest request, String basePath) throws IllegalStateException, IOException {
if (!(new File(basePath).isDirectory())) {
throw new IllegalArgumentException("basePath 参数必须是文件夹路径");
}
return multifileUploadAssist(request, basePath, null);
} /**
* 多文件上传
*
* @param request
* 当前上传的请求
* @param basePath
* 保存文件的路径
* @param exclude
* 排除文件名字符串,以逗号分隔的,默认无可传null
* @return
* @throws IllegalStateException
* @throws IOException
*/
public static Map<String, String> multiFileUpload(HttpServletRequest request, String basePath, String exclude) throws IllegalStateException, IOException {
if (!(new File(basePath).isDirectory())) {
throw new IllegalArgumentException("basePath 参数必须是文件夹路径");
} return multifileUploadAssist(request, basePath, exclude);
} /**
* 多文件上传辅助
*
* @param request
* 当前上传的请求
* @param basePath
* 保存文件的路径
* @param exclude
* 排除文件名字符串,以逗号分隔的,默认无可传null
* @return
* @throws IOException
*/
private static Map<String, String> multifileUploadAssist(HttpServletRequest request, String basePath, String exclude) throws IOException {
exclude = exclude == null ? "" : exclude; Map<String, String> filePaths = new HashMap<String, String>();
File file = null;
// 创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
// 判断 request 是否有文件上传,即多部分请求
if (multipartResolver.isMultipart(request)) {
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// get the parameter names of the MULTIPART files contained in this request
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
// 取得上传文件
List<MultipartFile> multipartFiles = multiRequest.getFiles(iter.next());
for (MultipartFile multipartFile : multipartFiles) {
String fileName = multipartFile.getOriginalFilename();
if (StringUtils.isNotEmpty(fileName) && (!exclude.contains(fileName))) {
file = new File(basePath + changeFilename2UUID(fileName));
filePaths.put(fileName, file.getPath());
multipartFile.transferTo(file);
}
}
}
}
return filePaths;
} /**
* 将文件名转变为UUID命名的 ,保留文件后缀
*
* @param filename
* @return
*/
public static String changeFilename2UUID(String filename) {
String uuid = UUID.randomUUID().toString();
return uuid + "." + FilenameUtils.getExtension(filename);
} /**
* 删除文件
*
* @param filePath
*/
public static void deleteFile(String filePath) {
try {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Spring MVC文件上传下载工具类的更多相关文章
- Spring MVC文件上传下载(转载)
原文地址: http://www.cnblogs.com/WJ-163/p/6269409.html 上传参考 http://www.cnblogs.com/lonecloud/p/5990060.h ...
- Spring MVC 文件上传下载
本文基于Spring MVC 注解,让Spring跑起来. (1) 导入jar包:ant.jar.commons-fileupload.jar.connom-io.jar. (2) 在src/cont ...
- SFTP 文件上传下载工具类
SFTPUtils.java import com.jcraft.jsch.*; import com.jcraft.jsch.ChannelSftp.LsEntry; import lombok.e ...
- Spring MVC 笔记 —— Spring MVC 文件上传
文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...
- ftp上传下载工具类
package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...
- 【Java Web开发学习】Spring MVC文件上传
[Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...
- 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解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...
随机推荐
- vue sync
1.使用vue cli建立工程 2.在APP.vue中: <template> <div class="details"> <myComponent ...
- 点击出现黑色背景的解决:-webkit-tap-highlight-color:rgba(0,0,0,0)
在手机上(iphone)点击按钮的时候,屏幕总会闪动一下,这让页面看起来很不友好也不流畅.解决方案加了一句css就解决了: -webkit-tap-highlight-color:rgba(0,0,0 ...
- mybatis性能优化之降低数据库连接
做性能优化的最重要的功能就是降低数据库的交互.非常多程序猿一般在开发的时候仅仅考虑简单的实现功能,无论业务简单复杂,仅仅要实现即可. mybatis有个重要的功能就是考虑在联合查询时技巧: <? ...
- Snubber电路
http://www.elecfans.com/yuanqijian/dianrongqi/20170601520736.html https://wenku.baidu.com/view/166f1 ...
- Struts2学习八----------接收参数
© 版权声明:本文为博主原创文章,转载请注明出处 接收参数 - 使用Action的属性接收参数 - 使用Domain Model接收参数 - 使用ModelDriven接收参数 实例 1.项目结构 2 ...
- Spring中的scope配置和@scope注解
Scope,也称作用域,在 Spring IoC 容器是指其创建的 Bean 对象相对于其他 Bean 对象的请求可见范围.在 Spring IoC 容器中具有以下几种作用域:基本作用域(single ...
- HDFS源码分析心跳汇报之BPServiceActor工作线程运行流程
在<HDFS源码分析心跳汇报之数据结构初始化>一文中,我们了解到HDFS心跳相关的BlockPoolManager.BPOfferService.BPServiceActor三者之间的关系 ...
- 华为云测平台服务再升级!华为M5系列平板调测能力正式上线!
6月1日,华为M5系列平板设备兼容性测试和远程真机调试功能在华为终端开放实验室正式上线!助力您的产品在大屏适配上快人一步! 华为终端开放实验室DevEco平台现已提供基于华为M5系列平板设备的兼 ...
- Android 手机怎么录屏制成gif图片(电脑录制gif图)
参考:http://www.cnblogs.com/dasusu/p/4903511.html 上面的博主说的很详细了,但作为学习记录我就重新写一遍帮助自己加深记忆 一.准备条件 1.你搭建了Andr ...
- 【BZOJ】1003 Cards
[解析]Burnside引理+背包dp+乘法逆元 [Analysis] 这道题卡了好久,就是没想懂置换跟着色是不一样的. 依据burnside引理.在一个置换群作用下不等价类的个数为每一个置换作用下不 ...