網(wǎng)站首頁 編程語言 正文
controller 代碼
@ApiOperation(value = "系統(tǒng)文件批量下載接口", produces = "application/octet-stream")
@PostMapping(value = "/downloadzip")
public void downloadzip(@ApiParam("文件 url 路徑") @RequestBody CommonProjectSearchVo searchVo, HttpServletResponse response) throws Exception {
String[] paths = searchVo.getPaths();
if (Objects.isNull(searchVo) || Objects.isNull(searchVo.getPaths()) || paths.length == 0) {
throw new CustomException("請選擇附件");
}
if (paths.length != 0) {
// 創(chuàng)建臨時(shí)路徑,存放壓縮文件
String myFileName = OperatorUtils.getOperator().getOperator()
+ String.valueOf(new Date().getTime()) + ".zip";
String zipFilePath = downloadPath + "/" + myFileName;
File file = new File(downloadPath);
//如果文件夾不存在則創(chuàng)建
if (!file.exists() && !file.isDirectory()) {
file.mkdir();
}
// 壓縮輸出流,包裝流,將臨時(shí)文件輸出流包裝成壓縮流,將所有文件輸出到這里,打成zip包
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
// 循環(huán)調(diào)用壓縮文件方法,將一個(gè)一個(gè)需要下載的文件打入壓縮文件包
for (String path : paths) {
FileConvertUtil.fileToZip(path, zipOut);
}
// 壓縮完成后,關(guān)閉壓縮流
zipOut.close();
//拼接下載默認(rèn)名稱并轉(zhuǎn)為ISO-8859-1格式
String fileName = new String((myFileName).getBytes(), "ISO-8859-1");
response.setHeader("Content-Disposition", "attchment;filename=" + fileName);
//該流不可以手動(dòng)關(guān)閉,手動(dòng)關(guān)閉下載會(huì)出問題,下載完成后會(huì)自動(dòng)關(guān)閉
ServletOutputStream outputStream = response.getOutputStream();
FileInputStream inputStream = new FileInputStream(zipFilePath);
// 如果是SpringBoot框架,在這個(gè)路徑
// org.apache.tomcat.util.http.fileupload.IOUtils產(chǎn)品
// 否則需要自主引入apache的 commons-io依賴
// copy方法為文件復(fù)制,在這里直接實(shí)現(xiàn)了下載效果
IOUtils.copy(inputStream, outputStream);
// 關(guān)閉輸入流
inputStream.close();
//下載完成之后,刪掉這個(gè)zip包
File fileTempZip = new File(zipFilePath);
fileTempZip.delete();
}
}
工具類FileConvertUtil.fileToZip
/**
* 文件格式轉(zhuǎn)換工具類
*/
public class FileConvertUtil {
/**
* 【文件壓縮】網(wǎng)絡(luò)文件
*
* @param filePath:
* @param zipOut:
* @return void
*/
public static void fileToZip(String filePath, ZipOutputStream zipOut) throws IOException {
filePath = getEncodeUrl(filePath).replaceAll("\\+", "%20");
// 需要壓縮的文件
File file = new File(filePath);
// 獲取文件名稱,為解決壓縮時(shí)重復(fù)名稱問題,對文件名加時(shí)間戳處理
String fileName = FilenameUtils.getBaseName(URLDecoder.decode(file.getName(), "UTF-8")) + "-"
+ String.valueOf(new Date().getTime()) + "."
+ FilenameUtils.getExtension(file.getName());
InputStream fileInput = getInputStream(filePath);
// 緩沖
byte[] bufferArea = new byte[1024 * 10];
BufferedInputStream bufferStream = new BufferedInputStream(fileInput, 1024 * 10);
// 將當(dāng)前文件作為一個(gè)zip實(shí)體寫入壓縮流,fileName代表壓縮文件中的文件名稱
zipOut.putNextEntry(new ZipEntry(fileName));
int length = 0;
// 最常規(guī)IO操作,不必緊張
while ((length = bufferStream.read(bufferArea, 0, 1024 * 10)) != -1) {
zipOut.write(bufferArea, 0, length);
}
//關(guān)閉流
fileInput.close();
// 需要注意的是緩沖流必須要關(guān)閉流,否則輸出無效
bufferStream.close();
// 壓縮流不必關(guān)閉,使用完后再關(guān)
}
/**
* 【獲取網(wǎng)絡(luò)文件的輸入流】
*
* @param filePath: 網(wǎng)絡(luò)文件路徑
* @return java.io.InputStream
*/
public static InputStream getInputStream(String filePath) throws IOException {
InputStream inputStream = null;
// 創(chuàng)建URL
URL url = new URL(filePath);
// 試圖連接并取得返回狀態(tài)碼
URLConnection urlconn = url.openConnection();
urlconn.connect();
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
int httpResult = httpconn.getResponseCode();
if (httpResult == HttpURLConnection.HTTP_OK) {
inputStream = urlconn.getInputStream();
}
return inputStream;
}
}
原文鏈接:https://blog.csdn.net/qq_41512902/article/details/125842512
相關(guān)推薦
- 2022-09-15 Go位集合相關(guān)操作bitset庫安裝使用_Golang
- 2022-11-01 zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的實(shí)戰(zhàn)教程_Android
- 2022-11-21 初識(shí)React及React開發(fā)依賴詳解_React
- 2022-05-06 python使用xlrd模塊讀取excel的方法實(shí)例_python
- 2022-08-01 Flask-SQLALchemy基本使用方法_python
- 2022-04-16 C#基于Socket實(shí)現(xiàn)多人聊天功能_C#教程
- 2023-06-20 C++模擬實(shí)現(xiàn)vector示例代碼圖文講解_C 語言
- 2023-04-12 Python批量刪除txt文本指定行的思路與代碼_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支