手把手教你用SpringBoot將文件打包成zip存放或?qū)С?/h1>
更新時間:2021年06月11日 16:45:51 作者:熊小哥~
相信各位看官在工作中都會遇到過要把多個文件打包成一個壓縮文件然后導(dǎo)出,或者將文件打包成Zip存放,這就來上代碼,廢話不多說,需要的朋友可以參考下
環(huán)境準(zhǔn)備
其實也沒什么準(zhǔn)備,準(zhǔn)備好Springboot就行,還有幾張圖片:

將文件打包成Zip存放
代碼
Controller代碼:
@RequestMapping("/zip")
@RestController
public class ZipController {
/**
* 將文件打包成zip并存放在特定位置
*/
@PostMapping("package")
public void packageFileToZip() throws IOException {
// 為了方便我直接將文件地址寫好了,各位可以根據(jù)自己的情況修改
String[] filePath = new String[]{"E:\\ykds\\1068128498917799516.jpg", "E:\\ykds\\1068128498917917980.jpg", "E:\\ykds\\1068128498917807874.jpg"};
// 將需要打包的文件都放在一個集合中
List<File> fileList = new ArrayList<>();
for (String s : filePath) {
File file = new File(s);
fileList.add(file);
}
// 先在D盤創(chuàng)建一個壓縮包
File zipFile = new File("D:\\package.zip");
if(!zipFile.exists())
zipFile.createNewFile();
// 將package.zip的File對象傳到toZip對象中
ZipUtils.toZip(fileList, zipFile);
}
}
ZipUTils工具類代碼
public class ZipUtils {
/**
* 把文件集合打成zip壓縮包
* @param srcFiles 壓縮文件集合
* @param zipFile zip文件名
* @throws RuntimeException 異常
*/
public static void toZip(List<File> srcFiles, File zipFile) throws IOException {
if(zipFile == null){
return;
}
if(!zipFile.getName().endsWith(".zip")){
return;
}
ZipOutputStream zos = null;
FileOutputStream out = new FileOutputStream(zipFile);
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
// 讀取文件并寫入到zip中
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
zos.flush();
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (zos != null) {
zos.close();
}
}
}
}
測試
代碼打好了,接下來測試下,打開熟悉的postman:

調(diào)用接口后就會在D盤中新建一個package.zip的壓縮包:

可以看到,我打包的文件都在這里,再看看能不能正常顯示:

very good!
將文件打包成zip并導(dǎo)出
上面的方法只是將壓縮包保存在本地,如果需要導(dǎo)出的話代碼有點不一樣。
代碼
Controller代碼:
/**
* 將文件打包成zip并下載
*/
@PostMapping("download")
public void download(HttpServletResponse response) throws IOException {
// 這里還是和上面一樣
String[] filePath = new String[]{"E:\\ykds\\1068128498917799516.jpg", "E:\\ykds\\1068128498917917980.jpg", "E:\\ykds\\1068128498917807874.jpg"};
List<File> fileList = new ArrayList<>();
for (String s : filePath) {
File file = new File(s);
fileList.add(file);
}
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip");
ZipUtils.downloadZip(response.getOutputStream(), fileList);
}
ZipUtils工具類代碼
public static void downloadZip(OutputStream outputStream, List<File> fileList){
BufferedInputStream bufferedInputStream = null;
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(outputStream);
for (File file : fileList) {
ZipEntry zipEntry = new ZipEntry(file.getName());
zipOutputStream.putNextEntry(zipEntry);
byte[] buf = new byte[BUFFER_SIZE];
int len;
FileInputStream in = new FileInputStream(file);
while ((len = in.read(buf)) != -1) {
zipOutputStream.write(buf, 0, len);
zipOutputStream.flush();
}
}
zipOutputStream.flush();
zipOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關(guān)閉流
try {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (zipOutputStream != null ) {
zipOutputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
測試
還是用postman:


下載完成后打開看看

到此這篇關(guān)于手把手教你用SpringBoot將文件打包成zip存放或?qū)С龅奈恼戮徒榻B到這了,更多相關(guān)SpringBoot將文件打包成zip內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Springboot如何實現(xiàn)Web系統(tǒng)License授權(quán)認證
- 一篇文章帶你入門Springboot整合微信登錄與微信支付(附源碼)
- springboot中一些比較常用的注解總結(jié)
- springboot @ConfigurationProperties和@PropertySource的區(qū)別
- 解決springboot集成swagger碰到的坑(報404)
- springboot集成opencv實現(xiàn)人臉識別功能的詳細步驟
- SpringBoot中定位切點的兩種常用方法
- Springboot 如何使用@Async整合線程池
- SpringBoot添加License的多種方式
相關(guān)文章
-
Spring配置文件解析之BeanDefinitionParserDelegate詳解
這篇文章主要介紹了Spring配置文件解析之BeanDefinitionParserDelegate詳解,對于Spring的配置文件的解析處理操作是在BeanDefinitionParserDelegate中進行處理操作,接下來我們簡單介紹一下BeanDefinitionParserDelegate所做的處理操作,需要的朋友可以參考下 2024-02-02
-
Spring?Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解
本文主要介紹了Spring?Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧 2023-06-06
-
Java實現(xiàn)ZooKeeper的zNode監(jiān)控
這篇文章主要介紹了Java實現(xiàn)ZooKeeper的zNode監(jiān)控問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下 2019-08-08
-
分布式系統(tǒng)下調(diào)用鏈追蹤技術(shù)面試題
這篇文章主要為大家介紹了分布式系統(tǒng)下調(diào)用鏈追蹤技術(shù)面試問題合集,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪 2022-03-03
-
java一個接口多個實現(xiàn)類的調(diào)用方式
這篇文章主要給大家介紹了關(guān)于java一個接口多個實現(xiàn)類的調(diào)用方式的相關(guān)資料,經(jīng)測試確認,當(dāng)一個接口有多個實現(xiàn)時,調(diào)用時只會執(zhí)行一個,有時候需要多個實現(xiàn)調(diào)用,需要的朋友可以參考下 2023-09-09
最新評論
環(huán)境準(zhǔn)備
其實也沒什么準(zhǔn)備,準(zhǔn)備好Springboot就行,還有幾張圖片:
將文件打包成Zip存放
代碼
Controller代碼:
@RequestMapping("/zip") @RestController public class ZipController { /** * 將文件打包成zip并存放在特定位置 */ @PostMapping("package") public void packageFileToZip() throws IOException { // 為了方便我直接將文件地址寫好了,各位可以根據(jù)自己的情況修改 String[] filePath = new String[]{"E:\\ykds\\1068128498917799516.jpg", "E:\\ykds\\1068128498917917980.jpg", "E:\\ykds\\1068128498917807874.jpg"}; // 將需要打包的文件都放在一個集合中 List<File> fileList = new ArrayList<>(); for (String s : filePath) { File file = new File(s); fileList.add(file); } // 先在D盤創(chuàng)建一個壓縮包 File zipFile = new File("D:\\package.zip"); if(!zipFile.exists()) zipFile.createNewFile(); // 將package.zip的File對象傳到toZip對象中 ZipUtils.toZip(fileList, zipFile); } }
ZipUTils工具類代碼
public class ZipUtils { /** * 把文件集合打成zip壓縮包 * @param srcFiles 壓縮文件集合 * @param zipFile zip文件名 * @throws RuntimeException 異常 */ public static void toZip(List<File> srcFiles, File zipFile) throws IOException { if(zipFile == null){ return; } if(!zipFile.getName().endsWith(".zip")){ return; } ZipOutputStream zos = null; FileOutputStream out = new FileOutputStream(zipFile); try { zos = new ZipOutputStream(out); for (File srcFile : srcFiles) { byte[] buf = new byte[BUFFER_SIZE]; zos.putNextEntry(new ZipEntry(srcFile.getName())); int len; // 讀取文件并寫入到zip中 FileInputStream in = new FileInputStream(srcFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); zos.flush(); } in.close(); } } catch (Exception e) { e.printStackTrace(); }finally { if (zos != null) { zos.close(); } } } }
測試
代碼打好了,接下來測試下,打開熟悉的postman:
調(diào)用接口后就會在D盤中新建一個package.zip的壓縮包:
可以看到,我打包的文件都在這里,再看看能不能正常顯示:
very good!
將文件打包成zip并導(dǎo)出
上面的方法只是將壓縮包保存在本地,如果需要導(dǎo)出的話代碼有點不一樣。
代碼
Controller代碼:
/** * 將文件打包成zip并下載 */ @PostMapping("download") public void download(HttpServletResponse response) throws IOException { // 這里還是和上面一樣 String[] filePath = new String[]{"E:\\ykds\\1068128498917799516.jpg", "E:\\ykds\\1068128498917917980.jpg", "E:\\ykds\\1068128498917807874.jpg"}; List<File> fileList = new ArrayList<>(); for (String s : filePath) { File file = new File(s); fileList.add(file); } response.setHeader("content-type", "application/octet-stream"); response.setContentType("application/octet-stream"); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip"); ZipUtils.downloadZip(response.getOutputStream(), fileList); }
ZipUtils工具類代碼
public static void downloadZip(OutputStream outputStream, List<File> fileList){ BufferedInputStream bufferedInputStream = null; ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(outputStream); for (File file : fileList) { ZipEntry zipEntry = new ZipEntry(file.getName()); zipOutputStream.putNextEntry(zipEntry); byte[] buf = new byte[BUFFER_SIZE]; int len; FileInputStream in = new FileInputStream(file); while ((len = in.read(buf)) != -1) { zipOutputStream.write(buf, 0, len); zipOutputStream.flush(); } } zipOutputStream.flush(); zipOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } finally { // 關(guān)閉流 try { if (bufferedInputStream != null) { bufferedInputStream.close(); } if (zipOutputStream != null ) { zipOutputStream.close(); } if (outputStream != null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }
測試
還是用postman:
下載完成后打開看看
到此這篇關(guān)于手把手教你用SpringBoot將文件打包成zip存放或?qū)С龅奈恼戮徒榻B到這了,更多相關(guān)SpringBoot將文件打包成zip內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot如何實現(xiàn)Web系統(tǒng)License授權(quán)認證
- 一篇文章帶你入門Springboot整合微信登錄與微信支付(附源碼)
- springboot中一些比較常用的注解總結(jié)
- springboot @ConfigurationProperties和@PropertySource的區(qū)別
- 解決springboot集成swagger碰到的坑(報404)
- springboot集成opencv實現(xiàn)人臉識別功能的詳細步驟
- SpringBoot中定位切點的兩種常用方法
- Springboot 如何使用@Async整合線程池
- SpringBoot添加License的多種方式
相關(guān)文章
Spring配置文件解析之BeanDefinitionParserDelegate詳解
這篇文章主要介紹了Spring配置文件解析之BeanDefinitionParserDelegate詳解,對于Spring的配置文件的解析處理操作是在BeanDefinitionParserDelegate中進行處理操作,接下來我們簡單介紹一下BeanDefinitionParserDelegate所做的處理操作,需要的朋友可以參考下2024-02-02Spring?Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解
本文主要介紹了Spring?Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java實現(xiàn)ZooKeeper的zNode監(jiān)控
這篇文章主要介紹了Java實現(xiàn)ZooKeeper的zNode監(jiān)控問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-08-08分布式系統(tǒng)下調(diào)用鏈追蹤技術(shù)面試題
這篇文章主要為大家介紹了分布式系統(tǒng)下調(diào)用鏈追蹤技術(shù)面試問題合集,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-03-03java一個接口多個實現(xiàn)類的調(diào)用方式
這篇文章主要給大家介紹了關(guān)于java一個接口多個實現(xiàn)類的調(diào)用方式的相關(guān)資料,經(jīng)測試確認,當(dāng)一個接口有多個實現(xiàn)時,調(diào)用時只會執(zhí)行一個,有時候需要多個實現(xiàn)調(diào)用,需要的朋友可以參考下2023-09-09