public void downloadProfessionalServiceOrder(DownloadOrderParam param, HttpServletResponse response) throws Exception {
Order order = orderRepository.findOne(param.getOrderId());
if (order.getIsDownload()) {
LocalDateTime completeTime = order.getCompleteTime();
LocalDateTime now = LocalDateTime.now();
Duration duration = Duration.between(completeTime, now);
long days = duration.toDays();
if (days > 3) {
throw new BusinessException("订单下载失败,请核实订单是否已过期");
}
}
order.setIsDownload(true);
orderRepository.update2(order);
QueryProfessionalServiceResultParam queryProfessionalServiceResultParam=JSONObject.parseObject(order.getParamContent(),QueryProfessionalServiceResultParam.class);
List<ProfessionalServiceResultDTO> professionalServiceResultDTOS = professionalServicel.queryProfessionalServiceResult(queryProfessionalServiceResultParam);
List<File> files = new ArrayList<>();
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
BufferedWriter bw = null;
for (ProfessionalServiceResultDTO professionalServiceResultDTO : professionalServiceResultDTOS) {
String filePath = orderAddress;
File dir = new File(filePath);
if (!dir.exists()) {
boolean result = dir.mkdirs();
log.info("新建临时目录结果:" + result);
}
if (!professionalServiceResultDTO.getIsUrl()) {
String fileName = professionalServiceResultDTO.getTitle() + ".txt";
bw = new BufferedWriter(new FileWriter(filePath + "/" + fileName));
bw.write(professionalServiceResultDTO.getUrl());
bw.flush();
File file = new File(filePath + "/" + fileName);
files.add(file);
} else {
String url = professionalServiceResultDTO.getUrl();
String serverApi = isUseReal ? "http://" + url : "http://39.102.55.70:8008" + url.substring(18);
URL requsetUrl = new URL(serverApi);
String fileName = professionalServiceResultDTO.getTitle() + ".pdf";
File file = new File(filePath + "/" + fileName);
FileUtils.copyURLToFile(requsetUrl, file);
files.add(file);
}
}
FileUtil.zipFile(files, "专业服务下载", zos);
zos.flush();
zos.close();
if (bw != null) {
bw.close();
}
}
@UtilityClass
@Slf4j
public class FileUtil {
private final DataFormatter dataFormatter = new DataFormatter();
public static boolean isExcel2007(String fileName) {
return fileName.matches("^.+\\.(?i)(xlsx)$");
}
public static String getCellValue(Cell cell) {
String cellValue = "";
if (cell == null) {
return cellValue;
}
switch (cell.getCellTypeEnum()) {
case STRING:
cellValue = cell.getStringCellValue();
break;
case _NONE:
cellValue = StringUtils.EMPTY;
break;
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = String.valueOf(cell.getDateCellValue().getTime());
} else {
cellValue = dataFormatter.formatCellValue(cell);
}
break;
default:
cellValue = cell.toString();
}
return cellValue;
}
public static boolean isRowEmpty(Row row) {
if (row == null) {
return true;
}
for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
if (StringUtils.isNotBlank(getCellValue(cell))) {
return false;
}
}
return true;
}
public static void zipFile(List<File> subs, String baseName, ZipOutputStream zos) throws IOException {
for (int i=0;i<subs.size();i++) {
File f=subs.get(i);
zos.putNextEntry(new ZipEntry(baseName + f.getName()));
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[1024];
int r;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
}
}