获取该FileUtil工具类具体演示,公众号内回复fileutil20200501即可。
package com.example.demo.util;import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class FileUtil { //设置压缩包临时文件目录
public static String zip_temp="D:\\java\\zip_temp"; /**
* 上传文件操作
* @param filePath 文件目录 parentPath/fileName
* @param inputStream 文件输入流
* @throws IOException
*/
public static void uploadFile(String filePath, InputStream inputStream) {
// 获取文件的后缀格式
// String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
File destFile = new File(filePath);
// 生成父级目录
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
try {
// 文件存储操作
BufferedInputStream in=new BufferedInputStream(inputStream);
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(destFile));
int len=-1;
byte[] b=new byte[1024];
while((len=in.read(b))!=-1){
out.write(b,0,len);
}
// 关闭相关流
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 下载单个文件
* @param fileName 文件名
* @param filePath 文件路径
* @param response 响应
* @return
*/
public static void downloadFile(String fileName,String filePath, HttpServletResponse response) throws Exception {
//创建该文件对象
File file = new File(filePath);
//判断该文件是否存在
if (file.exists()){//存在
try {
// 获取文件名
fileName = new String(fileName.getBytes(),"iso-8859-1");
// 设置响应头
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
// 把文件按照字节放入响应体中
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件输入流
BufferedInputStream bis = null;
OutputStream os = null; //输出流
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
// 关闭相关流
bis.close();
fis.close();
}catch (Exception e){
e.printStackTrace();
throw new Exception("文件下载出错!");
}
}else {//不存在
throw new Exception("该文件不存在"+fileName);
}
} /**
* 下载多个文件,将多个文件打包成压缩包,将压缩包进行下载
* @param fileList 就是把多个文件的路径放到一个list里面
* @param response 响应
* @param zipFileName 压缩包名字
* @return
* @throws Exception
*/
public static void downLoadFiles(String zipFileName, List<File> fileList, HttpServletResponse response) throws Exception {
try {
// 创建一个临时压缩文件,存放临时文件,方法最后有删除临时文件的步骤
String path = FileUtil.zip_temp+File.separator + "zips" +File.separator +zipFileName+".zip";
File zipFile = new File(path);
if (!zipFile.getParentFile().exists()) {
zipFile.getParentFile().mkdirs();
}
// 创建文件输出流
FileOutputStream fous = new FileOutputStream(zipFile);
ZipOutputStream zipOut = new ZipOutputStream(fous);
//向zip文件中添加原始文件
for (File file: fileList){
zipFile(file,zipOut);
}
// 关闭相关流
zipOut.close();
fous.close();
// 清空response
response.reset();
// 下载单个文件的操作
FileUtil.downloadFile(zipFile.getName(),zipFile.getPath(),response);
// 删除临时文件
zipFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 根据输入的文件与输出流对文件进行打包
* @param file 当个文件
* @param zipOut 压缩包输出流
*/
private static void zipFile(File file, ZipOutputStream zipOut) throws Exception {
try {
if (file.exists()) {
FileInputStream IN = new FileInputStream(file);
BufferedInputStream bins = new BufferedInputStream(IN, 1024);
ZipEntry entry = new ZipEntry(file.getName());
zipOut.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[1024];
while ((nNumber = bins.read(buffer)) != -1) {
zipOut.write(buffer, 0, nNumber);
}
// 关闭创建的流对象
bins.close();
IN.close();
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("压缩包某个文件添加异常!");
}
}
}
===API
1.文件上传方法
uploadFile(String filePath, InputStream inputStream);
参数解释
filePath:上传文件存储的路径
inputStream:上传文件的文件输入流,从MultipartFile对象中,通过getInputStream();方法即可获取。
2.单个文件下载方法
downloadFile(String fileName,String filePath, HttpServletResponse response) throws Exception
参数解释
fileName:下载后文件的文件名
filePath:下载文件在服务器的路径位置
response:响应体
3.多个文件下载方法
downLoadFiles(String zipFileName, List<File> fileList, HttpServletResponse response) throws Exception
参数解释
zipFileName:下载后压缩包的文件名
fileList:多个文件组成的集合
response:响应体