功能要求
从网络url中下载多个文件,并加入一个新创建文件,整合成一个压缩包返给前端,这里可以返回两种格式
1.返回一个blob格式文件
2.返回一个zip包的url
一.返回一个blob格式文件
优点:抽取文件流,压缩成一个zip返回给前端,不会生成冗余文件
缺点:前端获取略复杂,或与其他端对接时获取方法复杂
@GetMapping("/download/{resourceIds}")
public void download(@ApiParam("资源ids")
@NotNull(message = "资源ids不能为空")
@PathVariable("resourceIds") Long [] resourceIds, HttpServletResponse response) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String dateStr = sdf.format(new Date());
String licensePath ="/data"; //本地存放文件地址
//1.抽取线上文件下载链接
List<String> fileUrlList = new ArrayList<String(Arrays.asList("http://test1.txt",http://test2.txt", "http://test3.docx"));
//2.创建本地文件并得到输入流
String content="Hello";
String license = licensePath + File.separator + dateStr + File.separator + "license.txt";
FileZipUtils.createFile(license);
FileZipUtils.writeToFile(license, content, true);
ByteArrayOutputStream outputStream=null;
outputStream=new ByteArrayOutputStream();
InputStream inStream=new FileInputStream(license); //获取本地文件的输入流
//被压缩文件InputStream
InputStream[] srcFiles = new InputStream[fileUrlList.size()+1];
//被压缩文件名称
String[] srcFileNames = new String[fileUrlList.size()+1];
srcFiles[0] = inStream;
srcFileNames[0] = "license.txt";
//3.循环获取网络文件的输入流
for (int i = 0; i < fileUrlList.size(); i++) {
String fileUrl = fileUrlList.get(i);
InputStream inputStream = null;
inputStream =FileZipUtils.getFileInputStream(fileUrl); //获取网络文件的输入流
if (inputStream == null) {
continue;
}
srcFiles[i+1] = inputStream;
String[] splitFileUrl = fileUrl.split("/");
srcFileNames[i+1] = splitFileUrl[splitFileUrl.length - 1];
}
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("下载资源包" + dateStr + ".zip", "UTF-8"));
ZipUtil.zip(response.getOutputStream(), srcFileNames, srcFiles);
}
二.返回一个zip包的url
优点:返给前端的是一个zip包的 url ,前端好获取,可直接下载
缺点:每次生成一个zip包,在服务器上都会占取一定的储存空间
@GetMapping("/downloadZipUrl/{resourceIds}")
public AjaxResult downloadZipUrl(@ApiParam("资源ids")
@NotNull(message = "资源ids不能为空")
@PathVariable("resourceIds") Long [] resourceIds, HttpServletResponse response) throws Exception {
String licensePath ="/data"; //本地存放文件地址
if(resourceIds.length==0){
return AjaxResult.error("资源id不能为空");
}
//1.抽取线上文件下载链接
List<String> fileUrlList = new ArrayList<String(Arrays.asList("http://test1.txt",http://test2.txt", "http://test3.docx"));
//创建本地文件
LicenseVo licenseVo=new LicenseVo();
licenseVo.setCustomerId(getLoginUser().getUserId());
String zipName=FileZipUtils.fileUpload(licensePath,response,fileUrlList,content);
System.out.println("zip==="+zipName);
return AjaxResult.success("下载成功","http://xx.com".concat(zipName));
// http://xx.com 替换成域名
}
工具类FileZipUtils
/**
*
*/
package com.plan.License;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.plan.business.domain.vo.PResourceVo;
import com.plan.business.domain.vo.PSoftwareVo;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 文件操作工具类
* 实现文件的创建、删除、复制、压缩、解压以及目录的创建、删除、复制、压缩解压等功能
* @author javafast
* @version 2013-06-21
*/
public class FileZipUtils extends org.apache.commons.io.FileUtils {
private static Logger log = LoggerFactory.getLogger(FileZipUtils.class);
/**
* 复制单个文件,如果目标文件存在,则不覆盖
* @param srcFileName 待复制的文件名
* @param descFileName 目标文件名
* @return 如果复制成功,则返回true,否则返回false
*/
public static boolean copyFile(String srcFileName, String descFileName) {
return FileZipUtils.copyFileCover(srcFileName, descFileName, false);
}
/**
* 复制单个文件
* @param srcFileName 待复制的文件名
* @param descFileName 目标文件名
* @param coverlay 如果目标文件已存在,是否覆盖
* @return 如果复制成功,则返回true,否则返回false
*/
public static boolean copyFileCover(String srcFileName,
String descFileName, boolean coverlay) {
File srcFile = new File(srcFileName);
// 判断源文件是否存在
if (!srcFile.exists()) {
log.debug("复制文件失败,源文件 " + srcFileName + " 不存在!");
return false;
}
// 判断源文件是否是合法的文件
else if (!srcFile.isFile()) {
log.debug("复制文件失败," + srcFileName + " 不是一个文件!");
return false;
}
File descFile = new File(descFileName);
// 判断目标文件是否存在
if (descFile.exists()) {
// 如果目标文件存在,并且允许覆盖
if (coverlay) {
log.debug("目标文件已存在,准备删除!");
if (!FileZipUtils.delFile(descFileName)) {
log.debug("删除目标文件 " + descFileName + " 失败!");
return false;
}
} else {
log.debug("复制文件失败,目标文件 " + descFileName + " 已存在!");
return false;
}
} else {
if (!descFile.getParentFile().exists()) {
// 如果目标文件所在的目录不存在,则创建目录
log.debug("目标文件所在的目录不存在,创建目录!");
// 创建目标文件所在的目录
if (!descFile.getParentFile().mkdirs()) {
log.debug("创建目标文件所在的目录失败!");
return false;
}
}
}
// 准备复制文件
// 读取的位数
int readByte = 0;
InputStream ins = null;
OutputStream outs = null;
try {
// 打开源文件
ins = new FileInputStream(srcFile);
// 打开目标文件的输出流
outs = new FileOutputStream(descFile);
byte[] buf = new byte[1024];
// 一次读取1024个字节,当readByte为-1时表示文件已经读取完毕
while ((readByte = ins.read(buf)) != -1) {
// 将读取的字节流写入到输出流
outs.write(buf, 0, readByte);
}
log.debug("复制单个文件 " + srcFileName + " 到" + descFileName
+ "成功!");
return true;
} catch (Exception e) {
log.debug("复制文件失败:" + e.getMessage());
return false;
} finally {
// 关闭输入输出流,首先关闭输出流,然后再关闭输入流
if (outs != null) {
try {
outs.close();
} catch (IOException oute) {
oute.printStackTrace();
}
}
if (ins != null) {
try {
ins.close();
} catch (IOException ine) {
ine.printStackTrace();
}
}
}
}
/**
* 复制整个目录的内容,如果目标目录存在,则不覆盖
* @param srcDirName 源目录名
* @param descDirName 目标目录名
* @return 如果复制成功返回true,否则返回false
*/
public static boolean copyDirectory(String srcDirName, String descDirName) {
return FileZipUtils.copyDirectoryCover(srcDirName, descDirName,
false);
}
/**
* 复制整个目录的内容
* @param srcDirName 源目录名
* @param descDirName 目标目录名
* @param coverlay 如果目标目录存在,是否覆盖
* @return 如果复制成功返回true,否则返回false
*/
public static boolean copyDirectoryCover(String srcDirName,
String descDirName, boolean coverlay) {
File srcDir = new File(srcDirName);
// 判断源目录是否存在
if (!srcDir.exists()) {
log.debug("复制目录失败,源目录 " + srcDirName + " 不存在!");
return false;
}
// 判断源目录是否是目录
else if (!srcDir.isDirectory()) {
log.debug("复制目录失败," + srcDirName + " 不是一个目录!");
return false;
}
// 如果目标文件夹名不以文件分隔符结尾,自动添加文件分隔符
String descDirNames = descDirName;
if (!descDirNames.endsWith(File.separator)) {
descDirNames = descDirNames + File.separator;
}
File descDir = new File(descDirNames);
// 如果目标文件夹存在
if (descDir.exists()) {
if (coverlay) {
// 允许覆盖目标目录
log.debug("目标目录已存在,准备删除!");
if (!FileZipUtils.delFile(descDirNames)) {
log.debug("删除目录 " + descDirNames + " 失败!");
return false;
}
} else {
log.debug("目标目录复制失败,目标目录 " + descDirNames + " 已存在!");
return false;
}
} else {
// 创建目标目录
log.debug("目标目录不存在,准备创建!");
if (!descDir.mkdirs()) {
log.debug("创建目标目录失败!");
return false;
}
}
boolean flag = true;
// 列出源目录下的所有文件名和子目录名
File[] files = srcDir.listFiles();
for (int i = 0; i < files.length; i++) {
// 如果是一个单个文件,则直接复制
if (files[i].isFile()) {
flag = FileZipUtils.copyFile(files[i].getAbsolutePath(),
descDirName + files[i].getName());
// 如果拷贝文件失败,则退出循环
if (!flag) {
break;
}
}
// 如果是子目录,则继续复制目录
if (files[i].isDirectory()) {
flag = FileZipUtils.copyDirectory(files[i]
.getAbsolutePath(), descDirName + files[i].getName());
// 如果拷贝目录失败,则退出循环
if (!flag) {
break;
}
}
}
if (!flag) {
log.debug("复制目录 " + srcDirName + " 到 " + descDirName + " 失败!");
return false;
}
log.debug("复制目录 " + srcDirName + " 到 " + descDirName + " 成功!");
return true;
}
/**
*
* 删除文件,可以删除单个文件或文件夹
*
* @param fileName 被删除的文件名
* @return 如果删除成功,则返回true,否是返回false
*/
public static boolean delFile(String fileName) {
File file = new File(fileName);
if (!file.exists()) {
log.debug(fileName + " 文件不存在!");
return true;
} else {
if (file.isFile()) {
return FileZipUtils.deleteFile(fileName);
} else {
return FileZipUtils.deleteDirectory(fileName);
}
}
}
/**
*
* 删除单个文件
*
* @param fileName 被删除的文件名
* @return 如果删除成功,则返回true,否则返回false
*/
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
if (file.exists() && file.isFile()) {
if (file.delete()) {
log.debug("删除文件 " + fileName + " 成功!");
return true;
} else {
log.debug("删除文件 " + fileName + " 失败!");
return false;
}
} else {
log.debug(fileName + " 文件不存在!");
return true;
}
}
/**
*
* 删除目录及目录下的文件
*
* @param dirName 被删除的目录所在的文件路径
* @return 如果目录删除成功,则返回true,否则返回false
*/
public static boolean deleteDirectory(String dirName) {
String dirNames = dirName;
if (!dirNames.endsWith(File.separator)) {
dirNames = dirNames + File.separator;
}
File dirFile = new File(dirNames);
if (!dirFile.exists() || !dirFile.isDirectory()) {
log.debug(dirNames + " 目录不存在!");
return true;
}
boolean flag = true;
// 列出全部文件及子目录
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
// 删除子文件
if (files[i].isFile()) {
flag = FileZipUtils.deleteFile(files[i].getAbsolutePath());
// 如果删除文件失败,则退出循环
if (!flag) {
break;
}
}
// 删除子目录
else if (files[i].isDirectory()) {
flag = FileZipUtils.deleteDirectory(files[i]
.getAbsolutePath());
// 如果删除子目录失败,则退出循环
if (!flag) {
break;
}
}
}
if (!flag) {
log.debug("删除目录失败!");
return false;
}
// 删除当前目录
if (dirFile.delete()) {
log.debug("删除目录 " + dirName + " 成功!");
return true;
} else {
log.debug("删除目录 " + dirName + " 失败!");
return false;
}
}
/**
* 创建单个文件
* @param descFileName 文件名,包含路径
* @return 如果创建成功,则返回true,否则返回false
*/
public static boolean createFile(String descFileName) {
File file = new File(descFileName);
if (file.exists()) {
log.debug("文件 " + descFileName + " 已存在!");
return false;
}
if (descFileName.endsWith(File.separator)) {
log.debug(descFileName + " 为目录,不能创建目录!");
return false;
}
if (!file.getParentFile().exists()) {
// 如果文件所在的目录不存在,则创建目录
if (!file.getParentFile().mkdirs()) {
log.debug("创建文件所在的目录失败!");
return false;
}
}
// 创建文件
try {
if (file.createNewFile()) {
log.debug(descFileName + " 文件创建成功!");
return true;
} else {
log.debug(descFileName + " 文件创建失败!");
return false;
}
} catch (Exception e) {
e.printStackTrace();
log.debug(descFileName + " 文件创建失败!");
return false;
}
}
/**
* 创建目录
* @param descDirName 目录名,包含路径
* @return 如果创建成功,则返回true,否则返回false
*/
public static boolean createDirectory(String descDirName) {
String descDirNames = descDirName;
if (!descDirNames.endsWith(File.separator)) {
descDirNames = descDirNames + File.separator;
}
File descDir = new File(descDirNames);
if (descDir.exists()) {
log.debug("目录 " + descDirNames + " 已存在!");
return false;
}
// 创建目录
if (descDir.mkdirs()) {
log.debug("目录 " + descDirNames + " 创建成功!");
return true;
} else {
log.debug("目录 " + descDirNames + " 创建失败!");
return false;
}
}
/**
* 写入文件
* @param
*/
public static void writeToFile(String fileName, String content, boolean append) {
try {
FileZipUtils.write(new File(fileName), content, "utf-8", append);
log.debug("文件 " + fileName + " 写入成功!");
} catch (IOException e) {
log.debug("文件 " + fileName + " 写入失败! " + e.getMessage());
}
}
/**
* 写入文件
* @param
*/
public static void writeToFile(String fileName, String content, String encoding, boolean append) {
try {
FileZipUtils.write(new File(fileName), content, encoding, append);
log.debug("文件 " + fileName + " 写入成功!");
} catch (IOException e) {
log.debug("文件 " + fileName + " 写入失败! " + e.getMessage());
}
}
/**
* path 文件存储数据
* file 资源名称,可以考虑多文件循环
*/
public static String fileUpload(String path,HttpServletResponse response,List<String> files,String content) throws Exception {
SimpleDateFormat timef = new SimpleDateFormat("yyyyMMddHHmmss");
String timefFile = timef.format(new Date());
String pathName = path + File.separator + timefFile;
System.out.println("path-------------"+path);
//1.创建本地文件
String license = path + File.separator + timefFile + File.separator + "license.txt";
createFile(license);
writeToFile(license, content, true);
System.out.println(".创建本地文件-------------"+license);
//2.把资源包整合到一个文件里
for (String file : files) {
String fileName = file.substring(file.lastIndexOf("/")+1);
downLoadFromUrl(file,fileName,pathName);
System.out.println("把资源包整合到一个文件里-------------"+fileName);
}
//3.生成一个zip文件
String zipFile = path + File.separator + timefFile + ".zip";
FileZipUtils.zipFiles(path+ File.separator + timefFile, "*", zipFile);
System.out.println("生成一个zip文件-------------"+zipFile);
//压缩成功后,删除原文件夹
FileZipUtils.delFile(pathName);
return zipFile;
}
/**
* 压缩文件或目录
* @param srcDirName 压缩的根目录
* @param fileName 根目录下的待压缩的文件名或文件夹名,其中*或""表示跟目录下的全部文件
* @param descFileName 目标zip文件
*/
public static void zipFiles(String srcDirName, String fileName,
String descFileName) {
// 判断目录是否存在
if (srcDirName == null) {
log.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
return;
}
File fileDir = new File(srcDirName);
if (!fileDir.exists() || !fileDir.isDirectory()) {
log.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
return;
}
String dirPath = fileDir.getAbsolutePath();
File descFile = new File(descFileName);
try {
ZipOutputStream zouts = new ZipOutputStream(new FileOutputStream(
descFile));
if ("*".equals(fileName) || "".equals(fileName)) {
FileZipUtils.zipDirectoryToZipFile(dirPath, fileDir, zouts);
} else {
File file = new File(fileDir, fileName);
if (file.isFile()) {
FileZipUtils.zipFilesToZipFile(dirPath, file, zouts);
} else {
FileZipUtils
.zipDirectoryToZipFile(dirPath, file, zouts);
}
}
zouts.close();
log.debug(descFileName + " 文件压缩成功!");
} catch (Exception e) {
log.debug("文件压缩失败:" + e.getMessage());
e.printStackTrace();
}
}
/**
* 解压缩ZIP文件,将ZIP文件里的内容解压到descFileName目录下
* @param zipFileName 需要解压的ZIP文件
* @param descFileName 目标文件
*/
public static boolean unZipFiles(String zipFileName, String descFileName) {
String descFileNames = descFileName;
if (!descFileNames.endsWith(File.separator)) {
descFileNames = descFileNames + File.separator;
}
try {
// 根据ZIP文件创建ZipFile对象
ZipFile zipFile = new ZipFile(zipFileName);
ZipEntry entry = null;
String entryName = null;
String descFileDir = null;
byte[] buf = new byte[4096];
int readByte = 0;
// 获取ZIP文件里所有的entry
@SuppressWarnings("rawtypes")
Enumeration enums = zipFile.getEntries();
// 遍历所有entry
while (enums.hasMoreElements()) {
entry = (ZipEntry) enums.nextElement();
// 获得entry的名字
entryName = entry.getName();
descFileDir = descFileNames + entryName;
if (entry.isDirectory()) {
// 如果entry是一个目录,则创建目录
new File(descFileDir).mkdirs();
continue;
} else {
// 如果entry是一个文件,则创建父目录
new File(descFileDir).getParentFile().mkdirs();
}
File file = new File(descFileDir);
// 打开文件输出流
OutputStream os = new FileOutputStream(file);
// 从ZipFile对象中打开entry的输入流
InputStream is = zipFile.getInputStream((ZipArchiveEntry) entry);
while ((readByte = is.read(buf)) != -1) {
os.write(buf, 0, readByte);
}
os.close();
is.close();
}
zipFile.close();
log.debug("文件解压成功!");
return true;
} catch (Exception e) {
log.debug("文件解压失败:" + e.getMessage());
return false;
}
}
/**
* 将目录压缩到ZIP输出流
* @param dirPath 目录路径
* @param fileDir 文件信息
* @param zouts 输出流
*/
public static void zipDirectoryToZipFile(String dirPath, File fileDir,
ZipOutputStream zouts) {
if (fileDir.isDirectory()) {
File[] files = fileDir.listFiles();
// 空的文件夹
if (files.length == 0) {
// 目录信息
ZipEntry entry = new ZipEntry(getEntryName(dirPath, fileDir));
try {
zouts.putNextEntry(entry);
zouts.closeEntry();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
// 如果是文件,则调用文件压缩方法
FileZipUtils
.zipFilesToZipFile(dirPath, files[i], zouts);
} else {
// 如果是目录,则递归调用
FileZipUtils.zipDirectoryToZipFile(dirPath, files[i],
zouts);
}
}
}
}
/**
* 将文件压缩到ZIP输出流
* @param dirPath 目录路径
* @param file 文件
* @param zouts 输出流
*/
public static void zipFilesToZipFile(String dirPath, File file,
ZipOutputStream zouts) {
FileInputStream fin = null;
ZipEntry entry = null;
// 创建复制缓冲区
byte[] buf = new byte[4096];
int readByte = 0;
if (file.isFile()) {
try {
// 创建一个文件输入流
fin = new FileInputStream(file);
// 创建一个ZipEntry
entry = new ZipEntry(getEntryName(dirPath, file));
// 存储信息到压缩文件
zouts.putNextEntry(entry);
// 复制字节到压缩文件
while ((readByte = fin.read(buf)) != -1) {
zouts.write(buf, 0, readByte);
}
zouts.closeEntry();
fin.close();
System.out
.println("添加文件 " + file.getAbsolutePath() + " 到zip文件中!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 获取待压缩文件在ZIP文件中entry的名字,即相对于跟目录的相对路径名
* @param
* @param file entry文件名
* @return
*/
private static String getEntryName(String dirPath, File file) {
String dirPaths = dirPath;
if (!dirPaths.endsWith(File.separator)) {
dirPaths = dirPaths + File.separator;
}
String filePath = file.getAbsolutePath();
// 对于目录,必须在entry名字后面加上"/",表示它将以目录项存储
if (file.isDirectory()) {
filePath += "/";
}
int index = filePath.indexOf(dirPaths);
return filePath.substring(index + dirPaths.length());
}
// /**
// * 修复路径,将 \\ 或 / 等替换为 File.separator
// * @param path
// * @return
// */
// public static String path(String path){
// String p = StringUtils.replace(path, "\\", "/");
// p = StringUtils.join(StringUtils.split(p, "/"), "/");
// if (!StringUtils.startsWithAny(p, "/") && StringUtils.startsWithAny(path, "\\", "/")){
// p += "/";
// }
// if (!StringUtils.endsWithAny(p, "/") && StringUtils.endsWithAny(path, "\\", "/")){
// p = p + "/";
// }
// return p;
// }
/**
* 从网络Url中下载文件
*
* @param urlStr
* @param fileName
* @param savePath
* @throws IOException
*/
public static String downLoadFromUrl(String urlStr, String fileName, String savePath) {
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
// 防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// 得到输入流
InputStream inputStream = conn.getInputStream();
// 获取字节数组
byte[] getData = readInputStream(inputStream);
// 文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
// System.out.println("info:"+url+" download success");
return saveDir + File.separator + fileName;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 从输入流中获取字节数组
*
* @param inputStream
* @return
* @throws IOException
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
/*读取网络文件*/
public static InputStream getFileInputStream(String path) {
URL url = null;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
return conn.getInputStream();
} catch (Exception e) {
System.out.println("读取文件异常");
}
return null;
}
}