在上一篇博客中我们把文件上传上去了,在临时temp文件夹中得文件进行标注后,我们需要进行打包zip下载。
1.FileZipUtil工具类
public class FileZipUtil {
//多个文件,压缩成zip后下载
public static void downloadMoreFile(HttpServletResponse response, String dealFolder,String zipTmp) {
List<File> files = new ArrayList<>();
List<File> xmls = FileUtil.loopFiles(dealFolder);
for (File xml : xmls) {
files.add(xml);
}
zipd(zipTmp, files, response);
}
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;
}
File f = new File(file.getPath());
f.delete();
最后我们在finally 中,我们将文件打包成zip下载下来之后,服务器生成得临时文件dowload.zip 删除。
2.Controller层
@ApiOperation(value="下载zip", notes="下载zip")
@RequestMapping(value="/downloadMoreFile", method = RequestMethod.GET)
public void queryAutoLabelTest(@RequestParam("folderLocation") String folderLocation,
HttpServletResponse response) {
fileService.downloadMoreFile(response,folderLocation);
}
3.Service层
@Override
public void downloadMoreFile(HttpServletResponse response, String folderLocation) {
ApplicationHome home = new ApplicationHome();
File jarFile = home.getSource();
String path = jarFile.getParent();
String zipTmp =path + "\\dowload.zip"; //临时文件 每次生成新的
// folderLocation = folderLocation.replace("\\", "%5C");
System.out.println(folderLocation);
FileZipUtil.downloadMoreFile(response,folderLocation,zipTmp);
}
需要注意得是转义符号\ 可能在前后端得传输得时候发生歧义
本来在后端代码进行代替
folderLocation = folderLocation.replace("\", “%5C”);
可是不知道为啥,所以这块逻辑可以放在前端js中进行替换,将\代替%5C
最终前台js,windows.open(“http://localhost:8021/api/fileupload/downloadMoreFile?folderLocation=E:%5CErupt%5CErupt_parent%5CFileUpload%5Ctarget%5Ctemp”);