支持http/https文件流作为预览源url
parent
cf1ee9c631
commit
fb7cdfbef7
|
@ -37,4 +37,6 @@ nbdist/
|
|||
/producer/tmp
|
||||
/.temfile
|
||||
.temfile
|
||||
convertedFile/
|
||||
convertedFile/
|
||||
jodconverter-web/src/main/file
|
||||
jodconverter-web/src/main/cache
|
|
@ -35,7 +35,7 @@ public class FileConverQueueTask {
|
|||
@PostConstruct
|
||||
public void startTask(){
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
executorService.submit(new ConverTask(previewFactory,cacheService,fileUtils));
|
||||
executorService.submit(new ConverTask(previewFactory, cacheService, fileUtils));
|
||||
logger.info("队列处理文件转换任务启动完成 ");
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,7 @@ public class FileConverQueueTask {
|
|||
|
||||
FileUtils fileUtils;
|
||||
|
||||
public ConverTask(FilePreviewFactory previewFactory, CacheService cacheService,FileUtils fileUtils) {
|
||||
public ConverTask(FilePreviewFactory previewFactory, CacheService cacheService, FileUtils fileUtils) {
|
||||
this.previewFactory = previewFactory;
|
||||
this.cacheService = cacheService;
|
||||
this.fileUtils=fileUtils;
|
||||
|
@ -58,13 +58,13 @@ public class FileConverQueueTask {
|
|||
while (true) {
|
||||
try {
|
||||
String url = cacheService.takeQueueTask();
|
||||
if(url!=null){
|
||||
FileAttribute fileAttribute=fileUtils.getFileAttribute(url);
|
||||
if(url != null){
|
||||
FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
|
||||
logger.info("正在处理转换任务,文件名称【{}】",fileAttribute.getName());
|
||||
FileType fileType=fileAttribute.getType();
|
||||
if(fileType.equals(FileType.compress) || fileType.equals(FileType.office)){
|
||||
FilePreview filePreview=previewFactory.get(url);
|
||||
filePreview.filePreviewHandle(url,new ExtendedModelMap());
|
||||
FilePreview filePreview=previewFactory.get(fileAttribute);
|
||||
filePreview.filePreviewHandle(url, new ExtendedModelMap(), fileAttribute);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cn.keking.service;
|
||||
|
||||
import cn.keking.model.FileAttribute;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
/**
|
||||
|
@ -7,5 +8,5 @@ import org.springframework.ui.Model;
|
|||
* Content :
|
||||
*/
|
||||
public interface FilePreview {
|
||||
String filePreviewHandle(String url, Model model);
|
||||
String filePreviewHandle(String url, Model model, FileAttribute fileAttribute);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import cn.keking.utils.FileUtils;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -22,9 +21,8 @@ public class FilePreviewFactory {
|
|||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
public FilePreview get(String url) {
|
||||
public FilePreview get(FileAttribute fileAttribute) {
|
||||
Map<String, FilePreview> filePreviewMap = context.getBeansOfType(FilePreview.class);
|
||||
FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
|
||||
return filePreviewMap.get(fileAttribute.getType().getInstanceName());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,8 +28,7 @@ public class CompressFilePreviewImpl implements FilePreview{
|
|||
ZipReader zipReader;
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model) {
|
||||
FileAttribute fileAttribute=fileUtils.getFileAttribute(url);
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
String fileName=fileAttribute.getName();
|
||||
String decodedUrl=fileAttribute.getDecodedUrl();
|
||||
String suffix=fileAttribute.getSuffix();
|
||||
|
|
|
@ -19,9 +19,8 @@ public class MediaFilePreviewImpl implements FilePreview {
|
|||
FileUtils fileUtils;
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model) {
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
model.addAttribute("mediaUrl", url);
|
||||
FileAttribute fileAttribute=fileUtils.getFileAttribute(url);
|
||||
String suffix=fileAttribute.getSuffix();
|
||||
if ("flv".equalsIgnoreCase(suffix)) {
|
||||
return "flv";
|
||||
|
|
|
@ -9,9 +9,7 @@ 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;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
@ -44,11 +42,10 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||
public static final String OFFICE_PREVIEW_TYPE_ALLIMAGES = "allImages";
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model) {
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
// 预览Type,参数传了就取参数的,没传取系统默认
|
||||
String officePreviewType = model.asMap().get("officePreviewType") == null ? ConfigConstants.getOfficePreviewType() : model.asMap().get("officePreviewType").toString();
|
||||
String originUrl = (String) model.asMap().get("originUrl");
|
||||
FileAttribute fileAttribute=fileUtils.getFileAttribute(url);
|
||||
String suffix=fileAttribute.getSuffix();
|
||||
String fileName=fileAttribute.getName();
|
||||
String decodedUrl=fileAttribute.getDecodedUrl();
|
||||
|
|
|
@ -17,9 +17,7 @@ public class OtherFilePreviewImpl implements FilePreview {
|
|||
FileUtils fileUtils;
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model) {
|
||||
FileAttribute fileAttribute=fileUtils.getFileAttribute(url);
|
||||
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||
model.addAttribute("msg", "系统还不支持该格式文件的在线预览," +
|
||||
"如有需要请按下方显示的邮箱地址联系系统维护人员");
|
||||
|
|
|
@ -34,8 +34,7 @@ public class PdfFilePreviewImpl implements FilePreview{
|
|||
String fileDir = ConfigConstants.getFileDir();
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model) {
|
||||
FileAttribute fileAttribute=fileUtils.getFileAttribute(url);
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
String decodedUrl=fileAttribute.getDecodedUrl();
|
||||
String suffix=fileAttribute.getSuffix();
|
||||
String fileName=fileAttribute.getName();
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cn.keking.service.impl;
|
||||
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.service.FilePreview;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import com.google.common.collect.Lists;
|
||||
|
@ -21,7 +22,7 @@ public class PictureFilePreviewImpl implements FilePreview {
|
|||
FileUtils fileUtils;
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model) {
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
String fileKey=(String) RequestContextHolder.currentRequestAttributes().getAttribute("fileKey",0);
|
||||
List imgUrls = Lists.newArrayList(url);
|
||||
try{
|
||||
|
|
|
@ -23,8 +23,7 @@ public class SimTextFilePreviewImpl implements FilePreview{
|
|||
FileUtils fileUtils;
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model){
|
||||
FileAttribute fileAttribute=fileUtils.getFileAttribute(url);
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute){
|
||||
String decodedUrl=fileAttribute.getDecodedUrl();
|
||||
String fileName=fileAttribute.getName();
|
||||
ReturnResponse<String> response = simTextUtil.readSimText(decodedUrl, fileName);
|
||||
|
|
|
@ -25,7 +25,7 @@ public class DownloadUtils {
|
|||
* @param type
|
||||
* @return
|
||||
*/
|
||||
public ReturnResponse<String> downLoad(String urlAddress, String type, String fileName){
|
||||
public ReturnResponse<String> downLoad(String urlAddress, String type, String fileName) {
|
||||
ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
|
||||
URL url = null;
|
||||
try {
|
||||
|
@ -40,7 +40,7 @@ public class DownloadUtils {
|
|||
UUID uuid = UUID.randomUUID();
|
||||
if (null == fileName) {
|
||||
fileName = uuid+ "."+type;
|
||||
}else { // 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】)
|
||||
} else { // 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】)
|
||||
fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
|
||||
}
|
||||
String realPath = fileDir + fileName;
|
||||
|
|
|
@ -9,11 +9,13 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -63,13 +65,17 @@ public class FileUtils {
|
|||
* @return
|
||||
*/
|
||||
public FileType typeFromUrl(String url) {
|
||||
String[] simText = ConfigConstants.getSimText();
|
||||
String[] media = ConfigConstants.getMedia();
|
||||
String nonPramStr = url.substring(0, url.indexOf("?") != -1 ? url.indexOf("?") : url.length());
|
||||
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
|
||||
return typeFromFileName(fileName);
|
||||
}
|
||||
|
||||
private FileType typeFromFileName(String fileName) {
|
||||
String[] simText = ConfigConstants.getSimText();
|
||||
String[] media = ConfigConstants.getMedia();
|
||||
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
if (listPictureTypes().contains(fileType.toLowerCase())) {
|
||||
return FileType.picture;
|
||||
return FileType.picture;
|
||||
}
|
||||
if (listArchiveTypes().contains(fileType.toLowerCase())) {
|
||||
return FileType.compress;
|
||||
|
@ -265,22 +271,83 @@ public class FileUtils {
|
|||
private String suffixFromUrl(String url) {
|
||||
String nonPramStr = url.substring(0, url.indexOf("?") != -1 ? url.indexOf("?") : url.length());
|
||||
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
|
||||
return suffixFromFileName(fileName);
|
||||
}
|
||||
|
||||
private String suffixFromFileName(String fileName) {
|
||||
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
return fileType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取url中的参数
|
||||
* @param url
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
private String getUrlParameterReg(String url, String name) {
|
||||
Map<String, String> mapRequest = new HashMap();
|
||||
String strUrlParam = TruncateUrlPage(url);
|
||||
if (strUrlParam == null) {
|
||||
return "";
|
||||
}
|
||||
//每个键值为一组
|
||||
String[] arrSplit=strUrlParam.split("[&]");
|
||||
for(String strSplit:arrSplit) {
|
||||
String[] arrSplitEqual= strSplit.split("[=]");
|
||||
//解析出键值
|
||||
if(arrSplitEqual.length > 1) {
|
||||
//正确解析
|
||||
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
|
||||
} else if (!arrSplitEqual[0].equals("")) {
|
||||
//只有参数没有值,不加入
|
||||
mapRequest.put(arrSplitEqual[0], "");
|
||||
}
|
||||
}
|
||||
return mapRequest.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉url中的路径,留下请求参数部分
|
||||
* @param strURL url地址
|
||||
* @return url请求参数部分
|
||||
*/
|
||||
private static String TruncateUrlPage(String strURL) {
|
||||
String strAllParam = null;
|
||||
strURL = strURL.trim();
|
||||
String[] arrSplit = strURL.split("[?]");
|
||||
if(strURL.length() > 1) {
|
||||
if(arrSplit.length > 1) {
|
||||
if(arrSplit[1] != null) {
|
||||
strAllParam=arrSplit[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return strAllParam;
|
||||
}
|
||||
|
||||
|
||||
public FileAttribute getFileAttribute(String url) {
|
||||
String decodedUrl=null;
|
||||
String decodedUrl = null;
|
||||
try {
|
||||
decodedUrl = URLDecoder.decode(url, "utf-8");
|
||||
}catch (UnsupportedEncodingException e){
|
||||
log.debug("url解码失败");
|
||||
} catch (UnsupportedEncodingException e){
|
||||
log.error("url解码失败");
|
||||
}
|
||||
String fileName;
|
||||
FileType type;
|
||||
String suffix;
|
||||
|
||||
String fullFileName = getUrlParameterReg(decodedUrl, "fullfilename");
|
||||
if (!StringUtils.isEmpty(fullFileName)) {
|
||||
fileName = fullFileName;
|
||||
type = typeFromFileName(fileName);
|
||||
suffix = suffixFromFileName(fileName);
|
||||
} else {
|
||||
fileName = getFileNameFromURL(decodedUrl);
|
||||
type = typeFromUrl(url);
|
||||
suffix = suffixFromUrl(url);
|
||||
}
|
||||
// 路径转码
|
||||
FileType type = typeFromUrl(url);
|
||||
String suffix = suffixFromUrl(url);
|
||||
// 抽取文件并返回文件列表
|
||||
String fileName = getFileNameFromURL(decodedUrl);
|
||||
return new FileAttribute(type,suffix,fileName,url,decodedUrl);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package cn.keking.web.controller;
|
||||
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.service.FilePreview;
|
||||
import cn.keking.service.FilePreviewFactory;
|
||||
|
||||
import cn.keking.service.cache.CacheService;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
|
@ -26,12 +30,17 @@ import java.util.List;
|
|||
@Controller
|
||||
public class OnlinePreviewController {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(OnlinePreviewController.class);
|
||||
|
||||
@Autowired
|
||||
FilePreviewFactory previewFactory;
|
||||
|
||||
@Autowired
|
||||
CacheService cacheService;
|
||||
|
||||
@Autowired
|
||||
private FileUtils fileUtils;
|
||||
|
||||
/**
|
||||
* @param url
|
||||
* @param model
|
||||
|
@ -39,11 +48,12 @@ public class OnlinePreviewController {
|
|||
*/
|
||||
@RequestMapping(value = "onlinePreview", method = RequestMethod.GET)
|
||||
public String onlinePreview(String url, Model model, HttpServletRequest req) {
|
||||
FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
|
||||
req.setAttribute("fileKey", req.getParameter("fileKey"));
|
||||
model.addAttribute("officePreviewType", req.getParameter("officePreviewType"));
|
||||
model.addAttribute("originUrl",req.getRequestURL().toString());
|
||||
FilePreview filePreview = previewFactory.get(url);
|
||||
return filePreview.filePreviewHandle(url, model);
|
||||
model.addAttribute("originUrl", req.getRequestURL().toString());
|
||||
FilePreview filePreview = previewFactory.get(fileAttribute);
|
||||
return filePreview.filePreviewHandle(url, model, fileAttribute);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,7 +115,7 @@ public class OnlinePreviewController {
|
|||
resp.getOutputStream().write(bs, 0, len);
|
||||
}
|
||||
} catch (IOException | URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("下载pdf文件失败", e);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
|
|
Loading…
Reference in New Issue