需求 在下载订单图片的时候需要将图片放在压缩包中进行下载,如下:
@SneakyThrows
@GetMapping("/downloadJyzByIdZip")
@ApiOperation(value = "根据uuidStr下载文件", notes = "根据uuidStr下载文件(uuidStr为文件名、uuid的拼接)")
@ApiImplicitParam(name = "uuidStr", value = "文件名和uuid字符串", required = true, dataType = "string", paramType = "path")
public R downloadJyzByIdZip(String orderId, HttpServletResponse response) {
String newManagementRightCert = orderMapper.getInfoDowload(orderId);
if (newManagementRightCert ==null){
return R.ok("图片未上传,请联系管理员!");
}else {
String [] arr = newManagementRightCert.split(",");
List<String> voList = new ArrayList<>();
if (arr.length>0){
for (int i = 0; i < arr.length; i++) {
IaFileStore iaFileStore = iaFileStoreMapper.selectOne(new QueryWrapper<IaFileStore>().eq("id", arr[i]));
if (null ==iaFileStore){
return R.ok(voList);
}else {
IaFiles iaFiles = fileService.getOne(new QueryWrapper<IaFiles>().eq("file_name",iaFileStore.getName()));
fileService.getFileUrl(iaFiles.getUuid());
voList.add(fileService.getFileUrl(iaFiles.getUuid()));
}
}
}else {
return R.ok("文件下载失败,id不能为空!");
}
System.out.println(voList);
if (voList !=null && voList.size()>0){
try {
String downloadFilename="";
String nowTimeString = String.valueOf(System.currentTimeMillis());
downloadFilename = "经营权证.zip";
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
FileInputStream ips = null;
File file =new File("");
String url="";
byte[] buffer = new byte[1024];
int r=0;
for (int i = 0; i < voList.size(); i++) {
if(voList.get(i)!=null){
zos.putNextEntry(new ZipEntry("图片"+".jpg"));
url=voList.get(i);
file = new File(url);
System.out.println(file);
if(file.exists()) {
ips = new FileInputStream(file);
response.setContentType("multipart/form-data");
while ((r = ips.read(buffer)) != -1){
zos.write(buffer, 0, r);
}
if (null != ips) {
ips.close();
}
}else {
System.out.println("为空");
}
}
}
zos.flush();
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return R.ok(voList,"文件下载成功!");
}
}
以上市在实际工作中的代码,自己在学习,分享记录一下,与大家共享,如有不到之处,多多指教
以上若有侵权,请联系删除