|
@@ -7,6 +7,7 @@ import cn.keking.service.FilePreview;
|
|
|
import cn.keking.utils.DownloadUtils;
|
|
|
import cn.keking.utils.FileUtils;
|
|
|
import cn.keking.utils.OfficeToPdf;
|
|
|
+import cn.keking.utils.PdfUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -15,6 +16,7 @@ import org.springframework.ui.Model;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.util.List;
|
|
|
|
|
|
|
|
|
* Created by kl on 2018/1/17.
|
|
@@ -26,7 +28,8 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|
|
@Autowired
|
|
|
FileUtils fileUtils;
|
|
|
|
|
|
- String fileDir = ConfigConstants.getFileDir();
|
|
|
+ @Autowired
|
|
|
+ PdfUtils pdfUtils;
|
|
|
|
|
|
@Autowired
|
|
|
DownloadUtils downloadUtils;
|
|
@@ -34,14 +37,22 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|
|
@Autowired
|
|
|
private OfficeToPdf officeToPdf;
|
|
|
|
|
|
+ String fileDir = ConfigConstants.getFileDir();
|
|
|
+
|
|
|
+ public static final String OFFICE_PREVIEW_TYPE_PDF = "pdf";
|
|
|
+ public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
|
|
|
+
|
|
|
@Override
|
|
|
public String filePreviewHandle(String url, Model model) {
|
|
|
+
|
|
|
+ String officePreviewType = model.asMap().get("officePreviewType") == null ? ConfigConstants.getOfficePreviewType() : model.asMap().get("officePreviewType").toString();
|
|
|
FileAttribute fileAttribute=fileUtils.getFileAttribute(url);
|
|
|
String suffix=fileAttribute.getSuffix();
|
|
|
String fileName=fileAttribute.getName();
|
|
|
String decodedUrl=fileAttribute.getDecodedUrl();
|
|
|
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
|
|
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
|
|
|
+ String outFilePath = fileDir + pdfName;
|
|
|
|
|
|
if (!fileUtils.listConvertedFiles().containsKey(pdfName)) {
|
|
|
String filePath = fileDir + fileName;
|
|
@@ -53,7 +64,6 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|
|
}
|
|
|
filePath = response.getContent();
|
|
|
}
|
|
|
- String outFilePath = fileDir + pdfName;
|
|
|
if (StringUtils.hasText(outFilePath)) {
|
|
|
officeToPdf.openOfficeToPDF(filePath, outFilePath);
|
|
|
File f = new File(filePath);
|
|
@@ -68,6 +78,17 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|
|
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
|
|
|
}
|
|
|
}
|
|
|
+ if (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
|
|
|
+ List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, url);
|
|
|
+ if (imageUrls == null || imageUrls.size() < 1) {
|
|
|
+ model.addAttribute("msg", "office转图片异常,请联系管理员");
|
|
|
+ model.addAttribute("fileType",fileAttribute.getSuffix());
|
|
|
+ return "fileNotSupported";
|
|
|
+ }
|
|
|
+ model.addAttribute("imgurls", imageUrls);
|
|
|
+ model.addAttribute("currentUrl", imageUrls.get(0));
|
|
|
+ return "officePicture";
|
|
|
+ }
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
|
|
return isHtml ? "html" : "pdf";
|
|
|
}
|