word、ppt文档新增图片预览模式
This commit is contained in:
@ -17,6 +17,7 @@ public class ConfigConstants {
|
||||
private static String[] simText = {};
|
||||
private static String[] media = {};
|
||||
private static String convertedFileCharset;
|
||||
private static String officePreviewType;
|
||||
private static String fileDir = OfficeUtils.getHomePath() + File.separator + "file" + File.separator;
|
||||
|
||||
public static String[] getSimText() {
|
||||
@ -43,6 +44,14 @@ public class ConfigConstants {
|
||||
ConfigConstants.convertedFileCharset = convertedFileCharset;
|
||||
}
|
||||
|
||||
public static String getOfficePreviewType() {
|
||||
return officePreviewType;
|
||||
}
|
||||
|
||||
public static void setOfficePreviewType(String officePreviewType) {
|
||||
ConfigConstants.officePreviewType = officePreviewType;
|
||||
}
|
||||
|
||||
public static String getFileDir() {
|
||||
return fileDir;
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package cn.keking.config;
|
||||
|
||||
import cn.keking.service.impl.OfficeFilePreviewImpl;
|
||||
import org.artofsolving.jodconverter.office.OfficeUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -40,8 +41,9 @@ public class ConfigRefreshComponent {
|
||||
String text;
|
||||
String media;
|
||||
String convertedFileCharset = sysProperties.getProperty("sun.jnu.encoding");
|
||||
String[] textArray ;
|
||||
String[] textArray;
|
||||
String[] mediaArray;
|
||||
String officePreviewType;
|
||||
String configFilePath = OfficeUtils.getCustomizedConfigPath();
|
||||
while (true) {
|
||||
BufferedReader bufferedReader = new BufferedReader(new FileReader(configFilePath));
|
||||
@ -49,11 +51,13 @@ public class ConfigRefreshComponent {
|
||||
text = properties.getProperty("simText", DEFAULT_TXT_TYPE);
|
||||
media = properties.getProperty("media", DEFAULT_MEDIA_TYPE);
|
||||
convertedFileCharset = properties.getProperty("converted.file.charset", convertedFileCharset);
|
||||
officePreviewType = properties.getProperty("office.preview.type", OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE);
|
||||
textArray = text.split(",");
|
||||
mediaArray = media.split(",");
|
||||
ConfigConstants.setSimText(textArray);
|
||||
ConfigConstants.setMedia(mediaArray);
|
||||
ConfigConstants.setConvertedFileCharset(convertedFileCharset);
|
||||
ConfigConstants.setOfficePreviewType(officePreviewType);
|
||||
Thread.sleep(1000L);
|
||||
}
|
||||
} catch (IOException | InterruptedException e) {
|
||||
|
||||
@ -11,19 +11,26 @@ import java.util.Map;
|
||||
public interface CacheService {
|
||||
final String REDIS_FILE_PREVIEW_PDF_KEY = "converted-preview-pdf-file";
|
||||
final String REDIS_FILE_PREVIEW_IMGS_KEY = "converted-preview-imgs-file";//压缩包内图片文件集合
|
||||
final String REDIS_FILE_PREVIEW_PDF_IMGS_KEY = "converted-preview-pdfimgs-file";
|
||||
|
||||
|
||||
final Integer DEFAULT_PDF_CAPACITY = 500000;
|
||||
final Integer DEFAULT_IMG_CAPACITY = 500000;
|
||||
final Integer DEFAULT_PDFIMG_CAPACITY = 500000;
|
||||
|
||||
void initPDFCachePool(Integer capacity);
|
||||
void initIMGCachePool(Integer capacity);
|
||||
public void initPdfImagesCachePool(Integer capacity);
|
||||
void putPDFCache(String key, String value);
|
||||
void putImgCache(String key, List<String> value);
|
||||
Map<String, String> getPDFCache();
|
||||
String getPDFCache(String key);
|
||||
Map<String, List<String>> getImgCache();
|
||||
List<String> getImgCache(String key);
|
||||
Integer getPdfImageCache(String key);
|
||||
void putPdfImageCache(String pdfFilePath, int num);
|
||||
|
||||
void addQueueTask(String url);
|
||||
String takeQueueTask() throws InterruptedException;
|
||||
|
||||
}
|
||||
|
||||
@ -25,6 +25,8 @@ public class CacheServiceJDKImpl implements CacheService {
|
||||
|
||||
private Map<String, List<String>> imgCache;
|
||||
|
||||
private Map<String, Integer> pdfImagesCache;
|
||||
|
||||
private static final int QUEUE_SIZE = 500000;
|
||||
|
||||
private BlockingQueue blockingQueue = new ArrayBlockingQueue(QUEUE_SIZE);
|
||||
@ -43,6 +45,13 @@ public class CacheServiceJDKImpl implements CacheService {
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initPdfImagesCachePool(Integer capacity) {
|
||||
pdfImagesCache = new ConcurrentLinkedHashMap.Builder<String, Integer>()
|
||||
.maximumWeightedCapacity(capacity).weigher(Weighers.singleton())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPDFCache(String key, String value) {
|
||||
if (pdfCache == null) {
|
||||
@ -91,6 +100,22 @@ public class CacheServiceJDKImpl implements CacheService {
|
||||
return imgCache.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPdfImageCache(String key) {
|
||||
if (pdfImagesCache == null) {
|
||||
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
|
||||
}
|
||||
return pdfImagesCache.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPdfImageCache(String pdfFilePath, int num) {
|
||||
if (pdfImagesCache == null) {
|
||||
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
|
||||
}
|
||||
pdfImagesCache.put(pdfFilePath, num);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addQueueTask(String url) {
|
||||
blockingQueue.add(url);
|
||||
|
||||
@ -43,6 +43,11 @@ public class CacheServiceRedisImpl implements CacheService {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initPdfImagesCachePool(Integer capacity) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPDFCache(String key, String value) {
|
||||
RMapCache<String, String> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY);
|
||||
@ -77,6 +82,18 @@ public class CacheServiceRedisImpl implements CacheService {
|
||||
return convertedList.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPdfImageCache(String key) {
|
||||
RMapCache<String, Integer> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_IMGS_KEY);
|
||||
return convertedList.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPdfImageCache(String pdfFilePath, int num) {
|
||||
RMapCache<String, Integer> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_IMGS_KEY);
|
||||
convertedList.fastPut(pdfFilePath, num);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addQueueTask(String url) {
|
||||
RBlockingQueue<String> queue = redissonClient.getBlockingQueue(FileConverQueueTask.queueTaskName);
|
||||
|
||||
@ -51,6 +51,10 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
Map<String, List<String>> initIMGCache = new HashMap<>();
|
||||
db.put(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(initIMGCache));
|
||||
}
|
||||
if (db.get(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes()) == null) {
|
||||
Map<String, Integer> initPDFIMGCache = new HashMap<>();
|
||||
db.put(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache));
|
||||
}
|
||||
} catch (RocksDBException | IOException e) {
|
||||
LOGGER.error("Uable to init RocksDB" + e);
|
||||
}
|
||||
@ -67,6 +71,11 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initPdfImagesCachePool(Integer capacity) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPDFCache(String key, String value) {
|
||||
try {
|
||||
@ -136,6 +145,30 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPdfImageCache(String key) {
|
||||
Integer result = 0;
|
||||
Map<String, Integer> map;
|
||||
try{
|
||||
map = (Map<String, Integer>) toObject(db.get(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
|
||||
result = map.get(key);
|
||||
} catch (RocksDBException | IOException | ClassNotFoundException e) {
|
||||
LOGGER.error("Get from RocksDB Exception" + e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPdfImageCache(String pdfFilePath, int num) {
|
||||
try {
|
||||
Map<String, Integer> pdfImageCacheItem = new HashMap<>();
|
||||
pdfImageCacheItem.put(pdfFilePath, num);
|
||||
db.put(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(pdfImageCacheItem));
|
||||
} catch (RocksDBException | IOException e) {
|
||||
LOGGER.error("Put into RocksDB Exception" + e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addQueueTask(String url) {
|
||||
blockingQueue.add(url);
|
||||
|
||||
@ -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) {
|
||||
// 预览Type,参数传了就取参数的,没传取系统默认
|
||||
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";
|
||||
}
|
||||
|
||||
@ -47,6 +47,15 @@ public class FileUtils {
|
||||
return cacheService.getPDFCache(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 已将pdf转换成图片的图片本地路径
|
||||
* @param key pdf本地路径
|
||||
* @return
|
||||
*/
|
||||
public Integer getConvertedPdfImage(String key) {
|
||||
return cacheService.getPdfImageCache(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看文件类型(防止参数中存在.点号或者其他特殊字符,所以先抽取文件名,然后再获取文件类型)
|
||||
*
|
||||
@ -161,6 +170,15 @@ public class FileUtils {
|
||||
cacheService.putPDFCache(fileName, value);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param pdfFilePath
|
||||
* @param num
|
||||
*/
|
||||
public void addConvertedPdfImage(String pdfFilePath, int num){
|
||||
cacheService.putPdfImageCache(pdfFilePath, num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取redis中压缩包内图片文件
|
||||
* @param fileKey
|
||||
|
||||
66
jodconverter-web/src/main/java/cn/keking/utils/PdfUtils.java
Normal file
66
jodconverter-web/src/main/java/cn/keking/utils/PdfUtils.java
Normal file
@ -0,0 +1,66 @@
|
||||
package cn.keking.utils;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.rendering.ImageType;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class PdfUtils {
|
||||
|
||||
private final Logger LOGGER = LoggerFactory.getLogger(PdfUtils.class);
|
||||
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
|
||||
public List<String> pdf2jpg(String pdfFilePath, String pdfName, String url) {
|
||||
List<String> imageUrls = new ArrayList<>();
|
||||
Integer imageCount = fileUtils.getConvertedPdfImage(pdfFilePath);
|
||||
String imageFileSuffix = ".jpg";
|
||||
// https://8个字符 http://7个字符 从这后面开始出现的第一个/就是当前file.Dir下的根目录
|
||||
int index1 = url.indexOf("/", 8);
|
||||
String pdfFolder = pdfName.substring(0, pdfName.length() - 4);
|
||||
String urlPrefix = url.substring(0, index1 + 1) + pdfFolder;
|
||||
if (imageCount != null && imageCount.intValue() > 0) {
|
||||
for (int i = 0; i < imageCount ; i++)
|
||||
imageUrls.add(urlPrefix + "/" + i + imageFileSuffix);
|
||||
return imageUrls;
|
||||
}
|
||||
try {
|
||||
File pdfFile = new File(pdfFilePath);
|
||||
PDDocument doc = PDDocument.load(pdfFile);
|
||||
int pageCount = doc.getNumberOfPages();
|
||||
PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
||||
|
||||
int index = pdfFilePath.lastIndexOf(".");
|
||||
String folder = pdfFilePath.substring(0, index);
|
||||
|
||||
File path = new File(folder);
|
||||
if (!path.exists()) {
|
||||
path.mkdirs();
|
||||
}
|
||||
String imageFilePath;
|
||||
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
|
||||
imageFilePath = folder + File.separator + pageIndex + imageFileSuffix;
|
||||
BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 105, ImageType.RGB);
|
||||
ImageIOUtil.writeImage(image, imageFilePath, 105);
|
||||
imageUrls.add(urlPrefix + "/" + pageIndex + imageFileSuffix);
|
||||
}
|
||||
doc.close();
|
||||
fileUtils.addConvertedPdfImage(pdfFilePath, pageCount);
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Convert pdf to jpg exception", e);
|
||||
}
|
||||
return imageUrls;
|
||||
}
|
||||
}
|
||||
@ -45,6 +45,7 @@ public class OnlinePreviewController {
|
||||
@RequestMapping(value = "onlinePreview", method = RequestMethod.GET)
|
||||
public String onlinePreview(String url, Model model, HttpServletRequest req) {
|
||||
req.setAttribute("fileKey", req.getParameter("fileKey"));
|
||||
model.addAttribute("officePreviewType", req.getParameter("officePreviewType"));
|
||||
FilePreview filePreview = previewFactory.get(url);
|
||||
return filePreview.filePreviewHandle(url, model);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user