⼿把⼿教你⽤SpringBoot将⽂件打包成zip存放或导出环境准备
其实也没什么准备,准备好Springboot就⾏,还有⼏张图⽚:
将⽂件打包成Zip存放
代码
Controller代码:
@RequestMapping("/zip")
@RestController
public class ZipController {
/**
* 将⽂件打包成zip并存放在特定位置
*/
@PostMapping("package")
public void packageFileToZip() 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);
}
// 先在D盘创建⼀个压缩包
File zipFile = new File("D:\\package.zip");
if(!ists())
// 将package.zip的File对象传到toZip对象中
}
}
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(!Name().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 Name()));
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:
调⽤接⼝后就会在D盘中新建⼀个package.zip的压缩包:
可以看到,我打包的⽂件都在这⾥,再看看能不能正常显⽰:
very good!
将⽂件打包成zip并导出
上⾯的⽅法只是将压缩包保存在本地,如果需要导出的话代码有点不⼀样。
代码
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.OutputStream(), 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 Name());
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 {
/
/ 关闭流
try {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
springboot原理pdf
if (zipOutputStream != null ) {
zipOutputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试
还是⽤postman:
下载完成后打开看看
到此这篇关于⼿把⼿教你⽤SpringBoot将⽂件打包成zip存放或导出的⽂章就介绍到这了,更多相关SpringBoot将⽂件打包成zip内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。