优化下载pdf个例损坏问题

master
hcy 2 years ago
parent 933ca7f5d8
commit 7556161915

@ -13,8 +13,6 @@ import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@ -26,6 +24,8 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @ClassName BatchExportServiceImpl
@ -211,18 +211,13 @@ public class BatchExportServiceImpl implements BatchExportService {
return "没有任务";
}
response.reset();
//response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setContentType("application/zip");
ZipOutputStream zos = null;
BufferedOutputStream bos = null;
ByteArrayOutputStream out = null;
BufferedInputStream bis = null;
try {
String zipName = "任务清单pdf压缩包";
zipName = java.net.URLEncoder.encode(zipName, "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + zipName + "(" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ").zip");
zos = new ZipOutputStream(response.getOutputStream());
bos = new BufferedOutputStream(zos);
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())){
EmrPdfWaterSet emrPdfWaterSet = pdfWaterSetMapper.selectByPrimaryKey(1);
for (ExportTaskDetailsVo list : taskList) {
//根据住院号出院日期查询需要导出病历路径
@ -232,11 +227,9 @@ public class BatchExportServiceImpl implements BatchExportService {
logger.log("病案号为:" + list.getInpNo() + "出院时间为:" + list.getDischargeDateTime() + "的病历找不到。");
continue;
}
//组织导出文件名
//fileName = exportFlieName(list);
out = new ByteArrayOutputStream();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
Boolean aBoolean = imageToPdf1(filePathList, out, emrPdfWaterSet);
//Boolean aBoolean = imageToPdf(filePathList, fileName,exportPdfPath);
//为true时修改任务状态1完成2失败
if (aBoolean) {
exportTaskDetailsMapper.upStatc(1, list.getId());
@ -249,45 +242,25 @@ public class BatchExportServiceImpl implements BatchExportService {
String filePdfName = flieName + "_" + pdfNameStr;
zos.putNextEntry(new ZipEntry(filePdfName + ".pdf"));
byte[] file1 = out.toByteArray(); //这个zip文件的字节
bis = new BufferedInputStream(new ByteArrayInputStream(file1));
//byte[] file1 = out.toByteArray(); //这个zip文件的字节
//bis = new BufferedInputStream(new ByteArrayInputStream(file1));
try (BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(out.toByteArray()))) {
//输出
int len = 0;
byte[] buf = new byte[1024 * 1024];
while ((len = bis.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, len);
zos.write(buf, 0, len);
}
}
bis.close(); // 及时关闭流,避免资源泄露
out.reset(); // 重置ByteArrayOutputStream以便下次使用
} else {
exportTaskDetailsMapper.upStatc(2, list.getId());
logger.log("病案号为:" + list.getInpNo() + "出院时间为:" + list.getDischargeDateTime() + "的病历保存失败。");
}
}
}
} catch (Exception e) {
ExceptionPrintUtil.printException(e);
e.printStackTrace();
} finally {
try {
if (null != bis) {
bis.close();
}
if (null != bos) {
bos.flush();
bos.close();
}
if (null != out) {
out.flush();
out.close();
}
if (null != zos) {
zos.flush();
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "下载完成";
}

Loading…
Cancel
Save