Spring boot OBS 华为云文件打包下载

原文地址:springboot 多文件打包下载_潇潇暮雨_H的博客-CSDN博客_springboot文件打包下载   可用

 

 

OBS文件打包下载

/**
* 文件打包下载
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/packDown")
public String packDown(HttpServletResponse response) throws Exception {

List files = new ArrayList();
InputStream inputStream = hweiYunOBSService.fileDownload("b71268f1c92f456c96fa42f584594924.zip");
InputStream inputStream1 = hweiYunOBSService.fileDownload("779cc2a36a67449fa92a3debedd75552.zip");
files.add(inputStream);
files.add(inputStream1);
downLoadFiles(files, response);

return "成功";
}

/**
*
* @param files List<File> 作为参数传进来,就是把多个文件的路径放到一个list里面
* @param response
* @return
* @throws Exception
*/
public static HttpServletResponse downLoadFiles(List<File> files, HttpServletResponse response) throws Exception {

try {
// 临时文件夹 最好是放在服务器上,方法最后有删除临时文件的步骤
String zipFilename = "D:/tempFile.zip";
File file = new File(zipFilename);
file.createNewFile();
if (!file.exists()) {
file.createNewFile();
}
response.reset();
// response.getWriter()
// 创建文件输出流
FileOutputStream fous = new FileOutputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(fous);
zipFile(files, zipOut);
zipOut.close();
fous.close();
return downloadZip(file, response);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}

/**
* 把接受的全部文件打成压缩包
*
*/
public static void zipFile(List files, ZipOutputStream outputStream) {
int size = files.size();
for (int i = 0; i < size; i++) {
InputStream file = (InputStream) files.get(i);
zipFile(file, outputStream);
}
}

/**
* 根据输入的文件与输出流对文件进行打包
*
*/
public static void zipFile(InputStream inputFile, ZipOutputStream ouputStream) {
try {
BufferedInputStream bins = new BufferedInputStream(inputFile, 512);
ZipEntry entry = new ZipEntry(UUID.fastUUID()+".zip");
ouputStream.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
// 关闭创建的流对象
bins.close();
inputFile.close();
} 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;
}
posted @ 2022-06-01 13:58  李世恒01  阅读(463)  评论(0)    收藏  举报