Commit 8514b7b9 by shiwenbo

提供一个根据文件id批量下载文件的接口

parent 3a524c53
package com.xyst.dinas.biz.web;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.beecode.bap.attachment.AttachmentInfo;
import com.beecode.bap.attachment.exception.AttachmentDataDownLoadException;
import com.beecode.bap.attachment.service.AttachmentService;
import com.beecode.bap.department.service.DepartmentService;
import com.beecode.bap.staff.Staff;
import com.beecode.bap.staff.service.StaffService;
......@@ -40,6 +61,11 @@ public class DinasCommonController {
@Autowired
public PlanningCycleService planningCycleService;
@Autowired
public AttachmentService attachmentService;
private final Logger logger = LoggerFactory.getLogger(getClass());
/**
* @Description: 根据部门id查询全公司及其该部门的子部门(包括该部门自身)
......@@ -200,4 +226,92 @@ public class DinasCommonController {
@DateTimeFormat(pattern = "yyyy-MM-dd") @PathVariable Date endTime) {
return planningCycleService.getPlanningCycleListByDay(startTime, endTime);
}
@RequestMapping(value = "files/all", method = RequestMethod.GET)
public ResponseEntity<Resource> downloadAllFiles(@RequestParam(name = "fileIds") List<UUID> fileIds,
@RequestHeader(name = HttpHeaders.USER_AGENT, required = false) String userAgent) {
List<AttachmentInfo> attachmentInfos = new ArrayList<AttachmentInfo>();
for(int i = 0; i < fileIds.size(); i++) {
Optional<AttachmentInfo> attachmentInfo = attachmentService.find(fileIds.get(i));
if(attachmentInfo.isPresent()) {
attachmentInfos.add(attachmentInfo.get());
}
}
if (attachmentInfos != null && attachmentInfos.size() > 0) {
ZipOutputStream zipOutputStream = null;
try {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
CheckedOutputStream checkedOutputStream = new CheckedOutputStream(arrayOutputStream, new CRC32());
zipOutputStream = new ZipOutputStream(checkedOutputStream);
for (AttachmentInfo attachmentInfo : attachmentInfos) {
if (fileIds != null && !fileIds.contains(attachmentInfo.getId())) {
continue;
}
ZipEntry zipEntry = new ZipEntry(attachmentInfo.getFileName());
zipOutputStream.putNextEntry(zipEntry);
InputStream inputStream = attachmentService.getInputStream(attachmentInfo.getId());
int count = -1;
byte[] temp = new byte[1024];
try {
while ((count = inputStream.read(temp)) != -1) {
zipOutputStream.write(temp, 0, count);
}
zipOutputStream.closeEntry();
} catch (IOException e) {
logger.error("数据下载异常: 数据读写异常", e);
throw new AttachmentDataDownLoadException("数据下载异常: 数据读写异常", e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
logger.error("数据下载异常: 流关闭异常!", e);
throw new AttachmentDataDownLoadException("数据下载异常: 流关闭异常!", e);
}
}
}
zipOutputStream.flush();
zipOutputStream.finish();
HttpHeaders headers = new HttpHeaders();
headers.add("charset", "utf-8");
String fileName = new String("附件.zip".getBytes("utf-8"), "iso-8859-1");
if (isIE(userAgent)) {
fileName = URLEncoder.encode(fileName, "UTF-8");
}
headers.add("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
Resource resource = new ByteArrayResource(arrayOutputStream.toByteArray());
return ResponseEntity.ok().headers(headers).contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
} catch (Exception e) {
logger.error("数据下载异常: 创建流错误!", e);
throw new AttachmentDataDownLoadException("数据下载异常: 创建流错误!", e);
} finally {
try {
if (zipOutputStream != null) {
zipOutputStream.close();
}
} catch (Exception e) {
logger.error("数据下载异常: 流关闭异常!", e);
throw new AttachmentDataDownLoadException("数据下载异常: 流关闭异常!", e);
}
}
} else {
logger.warn("没有找到要下载的文件! ");
}
return null;
}
/**
* 判断是否是ie
*
* @return
*/
private boolean isIE(String userAgent) {
if (userAgent != null) {
userAgent = userAgent.toLowerCase();
return userAgent.contains("msie") || userAgent.contains("trident") || userAgent.contains("edge");
}
return false;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment