在项目开发中,难免会遇到对文件的下载功能,也往往会遇到同时对多个文件一起下载的问题,下面就来介绍对多文件下载是的一种方法。
那么对多文件下载时,我们可以将需要下载的多个文件先压缩成一个zip临时文件,然后下载该zip文件,最后删除zip文件。Java实现代码如下。
编写ZipOutTools.java zip文件操作工具类
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/***
* Zip文件压缩并实现下载工具类
*/
public class ZipOutTools {
//zipTmp zip压缩文件的全路径
//files 需要压缩的文件集合
//response HttpServletResponse对象
public static void zipd(String zipTmp, List<File> files, HttpServletResponse response){
File zipTmpFile = new File(zipTmp);
try {
if (zipTmpFile.exists()) {
zipTmpFile.delete();
}
zipTmpFile.createNewFile();
response.reset();
// 创建文件输出流
FileOutputStream fous = new FileOutputStream(zipTmpFile);
ZipOutputStream zipOut = new ZipOutputStream(fous);
zipFile(files, zipOut);
zipOut.close();
fous.close();
downloadZip(zipTmpFile, response);
} catch (IOException e) {
e.printStackTrace();
}
}
//files打成压缩包
public static void zipFile(List files, ZipOutputStream outputStream) {
int size = files.size();
for (int i = 0; i < size; i++) {
File file = (File) files.get(i);
zipFile(file, outputStream);
}
}
public static void zipFile(File inputFile, ZipOutputStream ouputStream) {
try {
if (inputFile.exists()) {
if (inputFile.isFile()) {
FileInputStream IN = new FileInputStream(inputFile);
BufferedInputStream bins = new BufferedInputStream(IN, 512);
ZipEntry entry = new ZipEntry(inputFile.getName());
ouputStream.putNextEntry(entry);
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
bins.close();
IN.close();
} else {
try {
File[] files = inputFile.listFiles();
for (int i = 0; i < files.length; i++) {
zipFile(files[i], ouputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
if (file.exists() == false) {
System.out.println("待压缩的文件目录:" + file + "不存在.");
} else {
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
// 如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(file.getName().getBytes("GB2312"), "ISO8859-1"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
File f = new File(file.getPath());
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return response;
}
}
Controller中调用方法实现下载
/***
* 地址和参数根据自己的业务需求自定义
* 以下仅供参考
**/
@RequestMapping(value = "/get-file-downloading/zip/{fileId}", method = {RequestMethod.POST, RequestMethod.GET})
public void getFileListZipDownloadMethod(HttpServletRequest request, HttpServletResponse response,
@PathVariable String fileId) throws Exception{
//这里的根据自己的业务逻辑编写代码,获取到自己需要的文件信息
//
List<File> files=new ArrayList<>();
files=自己的文件信息集合;
.............
.............
.............
//Zip压缩全路径
String zipTmp="临时zip文件存储的路径"+".zip";
//调用下载方法实现多文件下载
ZipOutTools.zipd(zipTmp, files, response);
}