java解压rar并返回文件_Java Zip压缩文件返回前端并下载

该博客介绍了如何使用Java创建一个工具类,将多个文件流组合成一个ZIP文件,并通过HTTP响应返回给前端进行下载。文章包含`ZipUtils`类的详细代码,展示了如何创建`ZipEntry`,读取文件流并将其写入压缩包,以及设置HTTP响应头以实现文件下载。此外,还提到了`ZipDto`实体类用于存储文件名和输入流,以及一个名为`exportFile`的方法,该方法处理数据并调用`ZipUtils`进行文件打包和下载操作。
摘要由CSDN通过智能技术生成

ZIP工具类:

@Slf4jpublic classZipUtils {/*** 将多个流转成zip文件输出

*@paramlistStream 文件流实体类对象

*@paramfileName zip包的名称

*@paramresponse

*@return

*/

public static boolean listStreamToZipStream(ListlistStream, String fileName, HttpServletResponse response) {boolean flag = false;

BufferedInputStream bis= null;

FileOutputStream fos= null;

ZipOutputStream zos= null;

OutputStream out= null;try{

out=response.getOutputStream();

response.reset();

response.setHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));

response.setHeader("Access-Control-Allow-Origin","*");

response.setContentType("application/octet-stream; charset=utf-8");

response.setCharacterEncoding("UTF-8");

zos= newZipOutputStream(out);byte[] bufs = new byte[1024 * 10];for(ZipDto zipDto : listStream) {

String streamfilename=zipDto.getName();//创建ZIP实体,并添加进压缩包

ZipEntry zipEntry = newZipEntry(streamfilename);

zos.putNextEntry(zipEntry);//读取待压缩的文件并写进压缩包里

bis = new BufferedInputStream(zipDto.getInputstream(), 1024 * 10);int read = 0;while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {

zos.write(bufs,0, read);

}

}

flag= true;

zos.close();

}catch(FileNotFoundException e) {

e.printStackTrace();throw newRuntimeException(e);

}catch(IOException e) {

e.printStackTrace();throw newRuntimeException(e);

}finally{//关闭流

try{if (null !=bis){

bis.close();

}if (null !=zos){

zos.close();

}if (null !=out){

out.close();

}

}catch(IOException e) {//e.printStackTrace();

log.error(e.getMessage());

}

}returnflag;

}

}

ZIP DTO实体类:

@Datapublic classZipDto {publicString name;publicInputStream inputstream;publicZipDto(String name, InputStream inputstream) {this.name =name;this.inputstream =inputstream;

}

}

ZIP 方法层:

public Boolean exportFile(List fileNames, HttpServletResponse response, HttpServletRequest request) {

QueryWrapper queryWrapper = new QueryWrapper();

queryWrapper.in("file_name",fileNames);

List cmtDealerSpotplanManagements = list(queryWrapper);

// 将数据处理

List spotPlanNames = cmtDealerSpotplanManagements.stream().map(查询数据::getFilePath).collect(Collectors.toList());

List list = new ArrayList<>();

// 数据为空则返回

if ( CollectionUtils.isEmpty(spotPlanNames) ){

return false;

}

// 将数据地址去远程下载

for (String fileUrl : spotPlanNames){

File tempFile = new File(fileUrl);

String newFileUrl = fileUrl.replace(" ", "%20");

InputStream fileInputStream = fileService.download2(newFileUrl);

String tempFileName = tempFile.getName();

list.add(new ZipDto(tempFileName, fileInputStream));

}

String fileName = "test" + ".zip";

ZipUtils.listStreamToZipStream(list, fileName, response);

return true;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值