读取⽂件解析⽂件发⽣的错误,已解决
错误背景
从⽂件服务器上⾯读取zip压缩⽂件下来,然后解压,放指定⽬录下。获取所有解压后⽂件的路径地址保存在list集合⾥⾯。最后要求是把所有获取到zip⾥⾯的pdf⽂件合成为⼀个总pdf⽂件,放⼊压缩包,上传到⽂件服务器上⾯就完成了。
发⽣错误点:所有pdf合成⼀个总pdf⽂件,在压缩为zip⽂件上传上去后,打开发下合成的pdf损坏了。
错误代码:海尔冰箱质量如何
10省人口现自然负增长public static String start(List<String> savepath){
Stopwatch stopwatch = ateStarted();
// 获取回单⽂件在linux上的临时存放地址
final String linuxFilePath = Value(HippoKeyConstant.FILE_PATH_TEMP);
List<String> fileList =new ArrayList<>();
String downloadUrl = null;
for(int i =0; i < savepath.size(); i++){
fileList.add((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 = NumberOfPages();
for(int j =1; j <= n; j++){
PdfImportedPage page = ImportedPage(reader, j);
copy.addPage(page);
}
}
//放⼊压缩⽂件中
ZipFile zipFile =new ZipFile(pdfZip);
zipFile.addFile(new File(pdfUrl));
String fileRelativePath = FastdfsUtil.uploadFile(pdfZip);
downloadUrl = DownUrl(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 = ateStarted();
// 获取回单⽂件在linux上的临时存放地址
final String linuxFilePath = Value(HippoKeyConstant.FILE_PATH_TEMP);
List<String> fileList =new ArrayList<>();
String downloadUrl = null;
for(int i =0; i < savepath.size(); i++){
fileList.add((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 = NumberOfPages();
for(int j =1; j <= n; j++){
PdfImportedPage page = ImportedPage(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 = DownUrl(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;
}
刚刚开始以为是压缩⽂件的时候,压缩的问题,后来换了⼏种⽅式,还是报⽂件损坏,最后午休起来灵光⼀现,发下这⽂件流放最后⾯关闭,是否是因为这个问题,最后终于可了,哎,纠结了⼀个上午。
发布评论