错误背景
从文件服务器上面读取zip压缩文件下来,然后解压,放指定目录下。获取所有解压后文件的路径地址保存在list集合里面。最后要求是把所有获取到zip里面的pdf文件合成为一个总pdf文件,放入压缩包,上传到文件服务器上面就完成了。
发生错误点:所有pdf合成一个总pdf文件,在压缩为zip文件上传上去后,打开发下合成的pdf损坏了。
错误代码:
public static String start(List<String> savepath){
Stopwatch stopwatch = Stopwatch.createStarted();
// 获取回单文件在linux上的临时存放地址
final String linuxFilePath = HippoClientUtil.getValue(HippoKeyConstant.FILE_PATH_TEMP);
List<String> fileList = new ArrayList<>();
String downloadUrl = null;
for (int i = 0; i < savepath.size(); i++) {
fileList.add(linuxFilePath+savepath.get(i));
}
log.info("fileList集合:{}",fileList.size());
Document document = null;
String pdfUrl = linuxFilePath+System.currentTimeMillis()+".pdf";
String pdfZip = linuxFilePath+System.currentTimeMillis()+".zip";
try {
document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(pdfUrl));
document.open();
for (String s : fileList) {
PdfReader reader = new PdfReader(s);
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
//放入压缩文件中
ZipFile zipFile = new ZipFile(pdfZip);
zipFile.addFile(new File(pdfUrl));
String fileRelativePath = FastdfsUtil.uploadFile(pdfZip);
downloadUrl = FastdfsUtil.getDownUrl(fileRelativePath);
log.info("相对路径:{},下载地址:{}", fileRelativePath, downloadUrl);
} catch (Exception e) {
e.printStackTrace();
}
for (String s : fileList) {
FileUtils.deleteQuietly(new File(s));
FileUtils.deleteQuietly(new File(pdfUrl));
FileUtils.deleteQuietly(new File(pdfZip));
}
document.close();
stopwatch.stop();
log.info("耗时:{}",stopwatch.elapsed(TimeUnit.SECONDS));
return downloadUrl;
}
解释下:获取文件路径然后把这些pdf文件合成一个,然后放入压缩包中,在上传到文件服务器上面去
问题原因:在pdf文件合成一个总pdf文件时,文件流还未关闭,就进行压缩操作,这是获取到的文件就是损坏的。
解决方法:
把
document.close();
stopwatch.stop();
代码块放入合成总pdf文件之后,然后在做其他操作,文件就不是损坏的了。
正确代码示例:
public static String start(List<String> savepath){
Stopwatch stopwatch = Stopwatch.createStarted();
// 获取回单文件在linux上的临时存放地址
final String linuxFilePath = HippoClientUtil.getValue(HippoKeyConstant.FILE_PATH_TEMP);
List<String> fileList = new ArrayList<>();
String downloadUrl = null;
for (int i = 0; i < savepath.size(); i++) {
fileList.add(linuxFilePath+savepath.get(i));
}
log.info("fileList集合:{}",fileList.size());
Document document = null;
String pdfUrl = linuxFilePath+System.currentTimeMillis()+".pdf";
String pdfZip = linuxFilePath+System.currentTimeMillis()+".zip";
try {
document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(pdfUrl));
document.open();
for (String s : fileList) {
PdfReader reader = new PdfReader(s);
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
document.close();
stopwatch.stop();
//放入压缩文件中
ZipFile zipFile = new ZipFile(pdfZip);
zipFile.addFile(new File(pdfUrl));
String fileRelativePath = FastdfsUtil.uploadFile(pdfZip);
downloadUrl = FastdfsUtil.getDownUrl(fileRelativePath);
log.info("相对路径:{},下载地址:{}", fileRelativePath, downloadUrl);
} catch (Exception e) {
e.printStackTrace();
}
for (String s : fileList) {
FileUtils.deleteQuietly(new File(s));
FileUtils.deleteQuietly(new File(pdfUrl));
FileUtils.deleteQuietly(new File(pdfZip));
}
log.info("耗时:{}",stopwatch.elapsed(TimeUnit.SECONDS));
return downloadUrl;
}
刚刚开始以为是压缩文件的时候,压缩的问题,后来换了几种方式,还是报文件损坏,最后午休起来灵光一现,发下这文件流放最后面关闭,是否是因为这个问题,最后终于可了,哎,纠结了一个上午。