Commit d55febc2 authored by luzhuang's avatar luzhuang

pdf拆分预览 代码优化

parent 3654656a
......@@ -373,7 +373,7 @@ public class BChapterServiceImpl implements BChapterService {
newBchapter.setCommonBusinessId();
}
newBchapter.setSort(model.getSort()+i-1);
newBchapter.setChapterName(newBchapter.getChapterName()+"-"+i);
newBchapter.setChapterName(newBchapter.getChapterName()+i);
bChapterList.add(newBchapter);
newBfile.setCommonBusinessId();
newBfile.setChapterId(newBchapter.getBusinessId());
......@@ -883,77 +883,33 @@ public class BChapterServiceImpl implements BChapterService {
File fileNew = new File(filePath + File.separator + newName);
LogUtil.fileChangeLog("------------------判断文件类型-----------------------------" + fileNew.getPath());
if (FileTypeEnum.PDF.getCode().equals(bFile.getType())) {
String localPath = "";
if (fileOld.exists()) {
LogUtil.fileChangeLog("------------------存在直接返回路径----------------------------" + fileOld);
int pages = FileReadableUtil.pdfReadable(filePath + File.separator + name);
if(pages > pdfCutPage){
if(bFile.getChapterId()==null || "".equals(bFile.getChapterId())){
//预览
try {
List<String> pdfUrls = CutPdfUtil.toCutPdf(filePath + File.separator + name,name,pdfCutPage);
for(String url : pdfUrls){
pdfOssUrls.add(url);
}
dto.setUrl(pdfUrls.get(0).replace(filePath,returnBasePath));
dto.setPdfNum(pdfUrls.size());
} catch (Exception e) {
e.printStackTrace();
}
return dto;
}
List<String> chapterIds = bChapterRepository.getPdfIdList(bFile.getChapterId());
int x = 0;
for(String s : chapterIds){
x++;
if(s.equals(bFile.getChapterId())){
break;
}
}
String cutPdf = filePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf";
File pdf = new File(cutPdf);
if(pdf.exists()){
dto.setUrl(returnBasePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf");
return dto;
}else{
try {
List<String> pdfUrls = CutPdfUtil.toCutPdf(filePath + File.separator + name,name,pdfCutPage);
for(String url : pdfUrls){
pdfOssUrls.add(url);
}
dto.setUrl(returnBasePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf");
dto.setPdfNum(pdfUrls.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
dto.setUrl(returnBasePath + File.separator + name);
}
return dto;
localPath = filePath + File.separator + name;
} else {
String downLoadName = downLoadFile(bFile, file, filePath);
LogUtil.fileChangeLog("------------------判断pdf是否可读----------------------------" + fileOld);
int pages = FileReadableUtil.fileReadable(filePath + File.separator + downLoadName, fileMaxSize,pdfMaxSize,txtMaxSize, fileMaxPages);
LogUtil.fileChangeLog("------------------pdf文件可读--返回下载后路径-----------------------------" + downLoadName);
if(pages > pdfCutPage){
if(bFile.getChapterId()==null || "".equals(bFile.getChapterId())){
//预览
try {
List<String> pdfUrls = CutPdfUtil.toCutPdf(filePath + File.separator + name,name,pdfCutPage);
for(String url : pdfUrls){
pdfOssUrls.add(url);
}
dto.setUrl(pdfUrls.get(0).replace(filePath,returnBasePath));
dto.setPdfNum(pdfUrls.size());
} catch (Exception e) {
e.printStackTrace();
localPath = filePath + File.separator + downLoadName;
}
int pages = FileReadableUtil.pdfReadable(localPath);
/**
* 文件已存在或已下载 服务器相对文件路径 localPath 和 pdf页数 pages
*/
if(pages > pdfCutPage){
if(bFile.getChapterId() == null || "".equals(bFile.getChapterId())){
//第一次 预览
try {
List<String> pdfUrls = CutPdfUtil.toCutPdf(filePath + File.separator + name,name,pdfCutPage);
for(String url : pdfUrls){
pdfOssUrls.add(url);
}
return dto;
//返回拆分文件第一个路径
dto.setUrl(pdfUrls.get(0).replace(filePath,returnBasePath));
dto.setPdfNum(pdfUrls.size());
} catch (Exception e) {
e.printStackTrace();
}
}else{
//step1 根据章节id获取对应 文件的序号 x
List<String> chapterIds = bChapterRepository.getPdfIdList(bFile.getChapterId());
int x = 0;
for(String s : chapterIds){
......@@ -965,10 +921,10 @@ public class BChapterServiceImpl implements BChapterService {
String cutPdf = filePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf";
File pdf = new File(cutPdf);
//step2 判断文件是否已存在 存在(直接返回) 不存在(拆分后返回)
if(pdf.exists()){
dto.setUrl(returnBasePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf");
return dto;
}else{
try {
List<String> pdfUrls = CutPdfUtil.toCutPdf(filePath + File.separator + name,name,pdfCutPage);
......@@ -983,11 +939,14 @@ public class BChapterServiceImpl implements BChapterService {
e.printStackTrace();
}
}
}else{
dto.setUrl(returnBasePath + File.separator + name);
}
return dto;
}else{
//pdf页数少不满足切割条件 直接返回服务器绝对地址
dto.setUrl(returnBasePath + File.separator + name);
}
return dto;
/** 不下载pdf、文件转发方案(上传快,没有可读判断)
String hostAddress = "";
try{
......
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