引入cpdetector解决文件编码识别问题

encoding
chenkailing 2020-12-26 19:13:50 +08:00
parent f2d929e6fa
commit d1a9a230e3
23 changed files with 240 additions and 400 deletions

Binary file not shown.

View File

@ -193,6 +193,14 @@
<scope>system</scope> <scope>system</scope>
<systemPath>${basedir}/lib/aspose-cad-19.9.jar</systemPath> <systemPath>${basedir}/lib/aspose-cad-19.9.jar</systemPath>
</dependency> </dependency>
<!-- -->
<dependency>
<groupId>cpdetector</groupId>
<artifactId>cpdetector</artifactId>
<version>1.04</version>
<scope>system</scope>
<systemPath>${basedir}/lib/cpdetector-1.04.jar</systemPath>
</dependency>
</dependencies> </dependencies>
<build> <build>
<resources> <resources>

View File

@ -1,8 +1,8 @@
package cn.keking.utils; package cn.keking.service;
import cn.keking.config.ConfigConstants; import cn.keking.config.ConfigConstants;
import cn.keking.model.FileType; import cn.keking.model.FileType;
import cn.keking.service.FilePreviewCommonService; import cn.keking.utils.FileUtils;
import cn.keking.web.filter.BaseUrlFilter; import cn.keking.web.filter.BaseUrlFilter;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -26,37 +26,34 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
*
* @author yudian-it * @author yudian-it
* @date 2017/11/27 * create 2017/11/27
*/ */
@Component @Component
public class ZipReader { public class CompressFileReader {
static Pattern pattern = Pattern.compile("^\\d+");
private final FilePreviewCommonService filePreviewCommonService;
private static final Pattern pattern = Pattern.compile("^\\d+");
private final FileHandlerService fileHandlerService;
private final String fileDir = ConfigConstants.getFileDir(); private final String fileDir = ConfigConstants.getFileDir();
private final ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); private final ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public ZipReader(FilePreviewCommonService filePreviewCommonService) { public CompressFileReader(FileHandlerService fileHandlerService) {
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
} }
public String readZipFile(String filePath,String fileKey) { public String readZipFile(String filePath, String fileKey) {
String archiveSeparator = "/"; String archiveSeparator = "/";
Map<String, FileNode> appender = new HashMap<>(); Map<String, FileNode> appender = new HashMap<>();
List<String> imgUrls = new LinkedList<>(); List<String> imgUrls = new LinkedList<>();
String baseUrl = BaseUrlFilter.getBaseUrl(); String baseUrl = BaseUrlFilter.getBaseUrl();
String archiveFileName = filePreviewCommonService.getFileNameFromPath(filePath); String archiveFileName = fileHandlerService.getFileNameFromPath(filePath);
try { try {
ZipFile zipFile = new ZipFile(filePath, filePreviewCommonService.getFileEncodeUTFGBK(filePath)); ZipFile zipFile = new ZipFile(filePath, FileUtils.getFileEncode(filePath));
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
// 排序 // 排序
entries = sortZipEntries(entries); entries = sortZipEntries(entries);
List<Map<String, ZipArchiveEntry>> entriesToBeExtracted = new LinkedList<>(); List<Map<String, ZipArchiveEntry>> entriesToBeExtracted = new LinkedList<>();
while (entries.hasMoreElements()){ while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement(); ZipArchiveEntry entry = entries.nextElement();
String fullName = entry.getName(); String fullName = entry.getName();
int level = fullName.split(archiveSeparator).length; int level = fullName.split(archiveSeparator).length;
@ -69,10 +66,10 @@ public class ZipReader {
entriesToBeExtracted.add(Collections.singletonMap(childName, entry)); entriesToBeExtracted.add(Collections.singletonMap(childName, entry));
} }
String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName); String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName);
parentName = (level-1) + "_" + parentName; parentName = (level - 1) + "_" + parentName;
FileType type= filePreviewCommonService.typeFromUrl(childName); FileType type = fileHandlerService.typeFromUrl(childName);
if (type.equals(FileType.picture)){//添加图片文件到图片列表 if (type.equals(FileType.picture)) {//添加图片文件到图片列表
imgUrls.add(baseUrl+childName); imgUrls.add(baseUrl + childName);
} }
FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
addNodes(appender, parentName, node); addNodes(appender, parentName, node);
@ -80,7 +77,7 @@ public class ZipReader {
} }
// 开启新的线程处理文件解压 // 开启新的线程处理文件解压
executors.submit(new ZipExtractorWorker(entriesToBeExtracted, zipFile, filePath)); executors.submit(new ZipExtractorWorker(entriesToBeExtracted, zipFile, filePath));
filePreviewCommonService.putImgCache(fileKey,imgUrls); fileHandlerService.putImgCache(fileKey, imgUrls);
return new ObjectMapper().writeValueAsString(appender.get("")); return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -90,28 +87,28 @@ public class ZipReader {
private Enumeration<ZipArchiveEntry> sortZipEntries(Enumeration<ZipArchiveEntry> entries) { private Enumeration<ZipArchiveEntry> sortZipEntries(Enumeration<ZipArchiveEntry> entries) {
List<ZipArchiveEntry> sortedEntries = new LinkedList<>(); List<ZipArchiveEntry> sortedEntries = new LinkedList<>();
while(entries.hasMoreElements()){ while (entries.hasMoreElements()) {
sortedEntries.add(entries.nextElement()); sortedEntries.add(entries.nextElement());
} }
sortedEntries.sort(Comparator.comparingInt(o -> o.getName().length())); sortedEntries.sort(Comparator.comparingInt(o -> o.getName().length()));
return Collections.enumeration(sortedEntries); return Collections.enumeration(sortedEntries);
} }
public String unRar(String filePath,String fileKey){ public String unRar(String filePath, String fileKey) {
Map<String, FileNode> appender = new HashMap<>(); Map<String, FileNode> appender = new HashMap<>();
List<String> imgUrls = new ArrayList<>(); List<String> imgUrls = new ArrayList<>();
String baseUrl = BaseUrlFilter.getBaseUrl(); String baseUrl = BaseUrlFilter.getBaseUrl();
try { try {
Archive archive = new Archive(new FileInputStream(new File(filePath))); Archive archive = new Archive(new FileInputStream(filePath));
List<FileHeader> headers = archive.getFileHeaders(); List<FileHeader> headers = archive.getFileHeaders();
headers = sortedHeaders(headers); headers = sortedHeaders(headers);
String archiveFileName = filePreviewCommonService.getFileNameFromPath(filePath); String archiveFileName = fileHandlerService.getFileNameFromPath(filePath);
List<Map<String, FileHeader>> headersToBeExtracted =new ArrayList<>(); List<Map<String, FileHeader>> headersToBeExtracted = new ArrayList<>();
for (FileHeader header : headers) { for (FileHeader header : headers) {
String fullName; String fullName;
if (header.isUnicode()) { if (header.isUnicode()) {
fullName = header.getFileNameW(); fullName = header.getFileNameW();
}else { } else {
fullName = header.getFileNameString(); fullName = header.getFileNameString();
} }
// 展示名 // 展示名
@ -123,16 +120,16 @@ public class ZipReader {
headersToBeExtracted.add(Collections.singletonMap(childName, header)); headersToBeExtracted.add(Collections.singletonMap(childName, header));
} }
String parentName = getLast2FileName(fullName, "\\", archiveFileName); String parentName = getLast2FileName(fullName, "\\", archiveFileName);
FileType type = filePreviewCommonService.typeFromUrl(childName); FileType type = fileHandlerService.typeFromUrl(childName);
if (type.equals(FileType.picture)){//添加图片文件到图片列表 if (type.equals(FileType.picture)) {//添加图片文件到图片列表
imgUrls.add(baseUrl+childName); imgUrls.add(baseUrl + childName);
} }
FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
addNodes(appender, parentName, node); addNodes(appender, parentName, node);
appender.put(childName, node); appender.put(childName, node);
} }
executors.submit(new RarExtractorWorker(headersToBeExtracted, archive, filePath)); executors.submit(new RarExtractorWorker(headersToBeExtracted, archive, filePath));
filePreviewCommonService.putImgCache(fileKey,imgUrls); fileHandlerService.putImgCache(fileKey, imgUrls);
return new ObjectMapper().writeValueAsString(appender.get("")); return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (RarException | IOException e) { } catch (RarException | IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -140,19 +137,19 @@ public class ZipReader {
return null; return null;
} }
public String read7zFile(String filePath,String fileKey) { public String read7zFile(String filePath, String fileKey) {
String archiveSeparator = "/"; String archiveSeparator = "/";
Map<String, FileNode> appender = new HashMap<>(); Map<String, FileNode> appender = new HashMap<>();
List<String> imgUrls = new ArrayList<>(); List<String> imgUrls = new ArrayList<>();
String baseUrl= BaseUrlFilter.getBaseUrl(); String baseUrl = BaseUrlFilter.getBaseUrl();
String archiveFileName = filePreviewCommonService.getFileNameFromPath(filePath); String archiveFileName = fileHandlerService.getFileNameFromPath(filePath);
try { try {
SevenZFile zipFile = new SevenZFile(new File(filePath)); SevenZFile zipFile = new SevenZFile(new File(filePath));
Iterable<SevenZArchiveEntry> entries = zipFile.getEntries(); Iterable<SevenZArchiveEntry> entries = zipFile.getEntries();
// 排序 // 排序
Enumeration<SevenZArchiveEntry> newEntries = sortSevenZEntries(entries); Enumeration<SevenZArchiveEntry> newEntries = sortSevenZEntries(entries);
List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted = new ArrayList<>(); List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted = new ArrayList<>();
while (newEntries.hasMoreElements()){ while (newEntries.hasMoreElements()) {
SevenZArchiveEntry entry = newEntries.nextElement(); SevenZArchiveEntry entry = newEntries.nextElement();
String fullName = entry.getName(); String fullName = entry.getName();
int level = fullName.split(archiveSeparator).length; int level = fullName.split(archiveSeparator).length;
@ -165,10 +162,10 @@ public class ZipReader {
entriesToBeExtracted.add(Collections.singletonMap(childName, entry)); entriesToBeExtracted.add(Collections.singletonMap(childName, entry));
} }
String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName); String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName);
parentName = (level-1) + "_" + parentName; parentName = (level - 1) + "_" + parentName;
FileType type= filePreviewCommonService.typeFromUrl(childName); FileType type = fileHandlerService.typeFromUrl(childName);
if (type.equals(FileType.picture)){//添加图片文件到图片列表 if (type.equals(FileType.picture)) {//添加图片文件到图片列表
imgUrls.add(baseUrl+childName); imgUrls.add(baseUrl + childName);
} }
FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
addNodes(appender, parentName, node); addNodes(appender, parentName, node);
@ -176,7 +173,7 @@ public class ZipReader {
} }
// 开启新的线程处理文件解压 // 开启新的线程处理文件解压
executors.submit(new SevenZExtractorWorker(entriesToBeExtracted, filePath)); executors.submit(new SevenZExtractorWorker(entriesToBeExtracted, filePath));
filePreviewCommonService.putImgCache(fileKey,imgUrls); fileHandlerService.putImgCache(fileKey, imgUrls);
return new ObjectMapper().writeValueAsString(appender.get("")); return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -210,7 +207,7 @@ public class ZipReader {
List<FileHeader> sortedHeaders = new ArrayList<>(); List<FileHeader> sortedHeaders = new ArrayList<>();
Map<Integer, FileHeader> mapHeaders = new TreeMap<>(); Map<Integer, FileHeader> mapHeaders = new TreeMap<>();
headers.forEach(header -> mapHeaders.put(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length(), header)); headers.forEach(header -> mapHeaders.put(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length(), header));
for (Map.Entry<Integer, FileHeader> entry : mapHeaders.entrySet()){ for (Map.Entry<Integer, FileHeader> entry : mapHeaders.entrySet()) {
for (FileHeader header : headers) { for (FileHeader header : headers) {
if (entry.getKey().equals(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length())) { if (entry.getKey().equals(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length())) {
sortedHeaders.add(header); sortedHeaders.add(header);
@ -222,7 +219,7 @@ public class ZipReader {
private static String getLast2FileName(String fullName, String seperator, String rootName) { private static String getLast2FileName(String fullName, String seperator, String rootName) {
if (fullName.endsWith(seperator)) { if (fullName.endsWith(seperator)) {
fullName = fullName.substring(0, fullName.length()-1); fullName = fullName.substring(0, fullName.length() - 1);
} }
// 1.获取剩余部分 // 1.获取剩余部分
int endIndex = fullName.lastIndexOf(seperator); int endIndex = fullName.lastIndexOf(seperator);
@ -237,7 +234,7 @@ public class ZipReader {
private static String getLastFileName(String fullName, String seperator) { private static String getLastFileName(String fullName, String seperator) {
if (fullName.endsWith(seperator)) { if (fullName.endsWith(seperator)) {
fullName = fullName.substring(0, fullName.length()-1); fullName = fullName.substring(0, fullName.length() - 1);
} }
String newName = fullName; String newName = fullName;
if (fullName.contains(seperator)) { if (fullName.contains(seperator)) {
@ -248,10 +245,11 @@ public class ZipReader {
public static Comparator<FileNode> sortComparator = new Comparator<FileNode>() { public static Comparator<FileNode> sortComparator = new Comparator<FileNode>() {
final Collator cmp = Collator.getInstance(Locale.US); final Collator cmp = Collator.getInstance(Locale.US);
@Override @Override
public int compare(FileNode o1, FileNode o2) { public int compare(FileNode o1, FileNode o2) {
// 判断两个对比对象是否是开头包含数字,如果包含数字则获取数字并按数字真正大小进行排序 // 判断两个对比对象是否是开头包含数字,如果包含数字则获取数字并按数字真正大小进行排序
BigDecimal num1,num2; BigDecimal num1, num2;
if (null != (num1 = isStartNumber(o1)) if (null != (num1 = isStartNumber(o1))
&& null != (num2 = isStartNumber(o2))) { && null != (num2 = isStartNumber(o2))) {
return num1.subtract(num2).intValue(); return num1.subtract(num2).intValue();
@ -287,14 +285,16 @@ public class ZipReader {
this.childList = childList; this.childList = childList;
this.directory = directory; this.directory = directory;
} }
public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory,String fileKey) {
public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory, String fileKey) {
this.originName = originName; this.originName = originName;
this.fileName = fileName; this.fileName = fileName;
this.parentFileName = parentFileName; this.parentFileName = parentFileName;
this.childList = childList; this.childList = childList;
this.directory = directory; this.directory = directory;
this.fileKey=fileKey; this.fileKey = fileKey;
} }
public String getFileKey() { public String getFileKey() {
return fileKey; return fileKey;
} }
@ -382,17 +382,15 @@ public class ZipReader {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (new File(filePath).exists()) { FileUtils.deleteFileByPath(filePath);
new File(filePath).delete();
}
} }
private void extractZipFile(String childName, InputStream zipFile) { private void extractZipFile(String childName, InputStream zipFile) {
String outPath = fileDir + childName; String outPath = fileDir + childName;
try (OutputStream ot = new FileOutputStream(outPath)){ try (OutputStream ot = new FileOutputStream(outPath)) {
byte[] inByte = new byte[1024]; byte[] inByte = new byte[1024];
int len; int len;
while ((-1 != (len = zipFile.read(inByte)))){ while ((-1 != (len = zipFile.read(inByte)))) {
ot.write(inByte, 0, len); ot.write(inByte, 0, len);
} }
} catch (IOException e) { } catch (IOException e) {
@ -441,10 +439,7 @@ public class ZipReader {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
FileUtils.deleteFileByPath(filePath);
if (new File(filePath).exists()) {
new File(filePath).delete();
}
} }
} }
@ -473,14 +468,12 @@ public class ZipReader {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (new File(filePath).exists()) { FileUtils.deleteFileByPath(filePath);
new File(filePath).delete();
}
} }
private void extractRarFile(String childName, FileHeader header, Archive archive) { private void extractRarFile(String childName, FileHeader header, Archive archive) {
String outPath = fileDir + childName; String outPath = fileDir + childName;
try(OutputStream ot = new FileOutputStream(outPath)) { try (OutputStream ot = new FileOutputStream(outPath)) {
archive.extractFile(header, ot); archive.extractFile(header, ot);
} catch (IOException | RarException e) { } catch (IOException | RarException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -21,18 +21,18 @@ public class FileConvertQueueTask {
private final Logger logger = LoggerFactory.getLogger(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass());
private final FilePreviewFactory previewFactory; private final FilePreviewFactory previewFactory;
private final CacheService cacheService; private final CacheService cacheService;
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
public FileConvertQueueTask(FilePreviewFactory previewFactory, CacheService cacheService, FilePreviewCommonService filePreviewCommonService) { public FileConvertQueueTask(FilePreviewFactory previewFactory, CacheService cacheService, FileHandlerService fileHandlerService) {
this.previewFactory = previewFactory; this.previewFactory = previewFactory;
this.cacheService = cacheService; this.cacheService = cacheService;
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
} }
@PostConstruct @PostConstruct
public void startTask(){ public void startTask(){
ExecutorService executorService = Executors.newFixedThreadPool(1); ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.submit(new ConvertTask(previewFactory, cacheService, filePreviewCommonService)); executorService.submit(new ConvertTask(previewFactory, cacheService, fileHandlerService));
logger.info("队列处理文件转换任务启动完成 "); logger.info("队列处理文件转换任务启动完成 ");
} }
@ -41,14 +41,14 @@ public class FileConvertQueueTask {
private final Logger logger = LoggerFactory.getLogger(ConvertTask.class); private final Logger logger = LoggerFactory.getLogger(ConvertTask.class);
private final FilePreviewFactory previewFactory; private final FilePreviewFactory previewFactory;
private final CacheService cacheService; private final CacheService cacheService;
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
public ConvertTask(FilePreviewFactory previewFactory, public ConvertTask(FilePreviewFactory previewFactory,
CacheService cacheService, CacheService cacheService,
FilePreviewCommonService filePreviewCommonService) { FileHandlerService fileHandlerService) {
this.previewFactory = previewFactory; this.previewFactory = previewFactory;
this.cacheService = cacheService; this.cacheService = cacheService;
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
} }
@Override @Override
@ -58,7 +58,7 @@ public class FileConvertQueueTask {
try { try {
url = cacheService.takeQueueTask(); url = cacheService.takeQueueTask();
if(url != null){ if(url != null){
FileAttribute fileAttribute = filePreviewCommonService.getFileAttribute(url,null); FileAttribute fileAttribute = fileHandlerService.getFileAttribute(url,null);
FileType fileType = fileAttribute.getType(); FileType fileType = fileAttribute.getType();
logger.info("正在处理预览转换任务url{},预览类型:{}", url, fileType); logger.info("正在处理预览转换任务url{},预览类型:{}", url, fileType);
if(fileType.equals(FileType.compress) || fileType.equals(FileType.office) || fileType.equals(FileType.cad)) { if(fileType.equals(FileType.compress) || fileType.equals(FileType.office) || fileType.equals(FileType.cad)) {

View File

@ -9,23 +9,23 @@ import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.*; import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* @author yudian-it * @author yudian-it
* @date 2017/11/13 * @date 2017/11/13
*/ */
@Component @Component
public class FilePreviewCommonService { public class FileHandlerService {
private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding"); private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
private final String fileDir = ConfigConstants.getFileDir(); private final String fileDir = ConfigConstants.getFileDir();
private final CacheService cacheService; private final CacheService cacheService;
public FilePreviewCommonService(CacheService cacheService) { public FileHandlerService(CacheService cacheService) {
this.cacheService = cacheService; this.cacheService = cacheService;
} }
@ -141,31 +141,6 @@ public class FilePreviewCommonService {
cacheService.putImgCache(fileKey, imgs); cacheService.putImgCache(fileKey, imgs);
} }
/**
*
*
* @param path
* @return
*/
public String getFileEncodeUTFGBK(String path) {
String enc = Charset.forName("GBK").name();
File file = new File(path);
InputStream in;
try {
in = new FileInputStream(file);
byte[] b = new byte[3];
in.read(b);
in.close();
if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
enc = StandardCharsets.UTF_8.name();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件编码格式为:" + enc);
return enc;
}
/** /**
* () * ()
* *
@ -291,10 +266,10 @@ public class FilePreviewCommonService {
if (req != null) { if (req != null) {
String officePreviewType = req.getParameter("officePreviewType"); String officePreviewType = req.getParameter("officePreviewType");
String fileKey = req.getParameter("fileKey"); String fileKey = req.getParameter("fileKey");
if(StringUtils.hasText(officePreviewType)){ if (StringUtils.hasText(officePreviewType)) {
attribute.setOfficePreviewType(officePreviewType); attribute.setOfficePreviewType(officePreviewType);
} }
if(StringUtils.hasText(fileKey)){ if (StringUtils.hasText(fileKey)) {
attribute.setFileKey(fileKey); attribute.setFileKey(fileKey);
} }
} }

View File

@ -1,6 +1,5 @@
package cn.keking.service; package cn.keking.service;
import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute; import cn.keking.model.FileAttribute;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -9,9 +8,5 @@ import org.springframework.ui.Model;
* Content : * Content :
*/ */
public interface FilePreview { public interface FilePreview {
String TEXT_TYPE = "textType";
String DEFAULT_TEXT_TYPE = "simText";
String filePreviewHandle(String url, Model model, FileAttribute fileAttribute); String filePreviewHandle(String url, Model model, FileAttribute fileAttribute);
} }

View File

@ -32,9 +32,9 @@ import java.util.Properties;
*/ */
@Component @Component
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
public class OfficeProcessManager { public class OfficePluginManager {
private final Logger logger = LoggerFactory.getLogger(OfficeProcessManager.class); private final Logger logger = LoggerFactory.getLogger(OfficePluginManager.class);
private OfficeManager officeManager; private OfficeManager officeManager;

View File

@ -10,10 +10,10 @@ import java.io.File;
*/ */
@Component @Component
public class OfficeToPdfService { public class OfficeToPdfService {
private final OfficeProcessManager officeProcessManager; private final OfficePluginManager officePluginManager;
public OfficeToPdfService(OfficeProcessManager officeProcessManager) { public OfficeToPdfService(OfficePluginManager officePluginManager) {
this.officeProcessManager = officeProcessManager; this.officePluginManager = officePluginManager;
} }
public void openOfficeToPDF(String inputFilePath, String outputFilePath) { public void openOfficeToPDF(String inputFilePath, String outputFilePath) {
@ -33,7 +33,7 @@ public class OfficeToPdfService {
public void office2pdf(String inputFilePath, String outputFilePath) { public void office2pdf(String inputFilePath, String outputFilePath) {
OfficeDocumentConverter converter = officeProcessManager.getDocumentConverter(); OfficeDocumentConverter converter = officePluginManager.getDocumentConverter();
if (null != inputFilePath) { if (null != inputFilePath) {
File inputFile = new File(inputFilePath); File inputFile = new File(inputFilePath);
// 判断目标文件路径是否为空 // 判断目标文件路径是否为空

View File

@ -6,7 +6,7 @@ import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.CadUtils; import cn.keking.utils.CadUtils;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FilePreviewCommonService; import cn.keking.service.FileHandlerService;
import cn.keking.utils.PdfUtils; import cn.keking.utils.PdfUtils;
import cn.keking.web.filter.BaseUrlFilter; import cn.keking.web.filter.BaseUrlFilter;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -22,7 +22,7 @@ import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
@Service @Service
public class CadFilePreviewImpl implements FilePreview { public class CadFilePreviewImpl implements FilePreview {
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
private final DownloadUtils downloadUtils; private final DownloadUtils downloadUtils;
@ -30,11 +30,11 @@ public class CadFilePreviewImpl implements FilePreview {
private final PdfUtils pdfUtils; private final PdfUtils pdfUtils;
public CadFilePreviewImpl(FilePreviewCommonService filePreviewCommonService, public CadFilePreviewImpl(FileHandlerService fileHandlerService,
DownloadUtils downloadUtils, DownloadUtils downloadUtils,
CadUtils cadUtils, CadUtils cadUtils,
PdfUtils pdfUtils) { PdfUtils pdfUtils) {
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
this.cadUtils = cadUtils; this.cadUtils = cadUtils;
this.pdfUtils = pdfUtils; this.pdfUtils = pdfUtils;
@ -56,7 +56,7 @@ public class CadFilePreviewImpl implements FilePreview {
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf"; String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
String outFilePath = FILE_DIR + pdfName; String outFilePath = FILE_DIR + pdfName;
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换 // 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
if (!filePreviewCommonService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) { if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath; String filePath;
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) { if (0 != response.getCode()) {
@ -74,7 +74,7 @@ public class CadFilePreviewImpl implements FilePreview {
} }
if (ConfigConstants.isCacheEnabled()) { if (ConfigConstants.isCacheEnabled()) {
// 加入缓存 // 加入缓存
filePreviewCommonService.addConvertedFile(pdfName, filePreviewCommonService.getRelativePath(outFilePath)); fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
} }
} }
} }

View File

@ -5,8 +5,8 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FilePreviewCommonService; import cn.keking.service.FileHandlerService;
import cn.keking.utils.ZipReader; import cn.keking.service.CompressFileReader;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -18,18 +18,14 @@ import org.springframework.util.StringUtils;
@Service @Service
public class CompressFilePreviewImpl implements FilePreview { public class CompressFilePreviewImpl implements FilePreview {
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
private final DownloadUtils downloadUtils; private final DownloadUtils downloadUtils;
private final CompressFileReader compressFileReader;
private final ZipReader zipReader; public CompressFilePreviewImpl(FileHandlerService fileHandlerService, DownloadUtils downloadUtils, CompressFileReader compressFileReader) {
this.fileHandlerService = fileHandlerService;
public CompressFilePreviewImpl(FilePreviewCommonService filePreviewCommonService,
DownloadUtils downloadUtils,
ZipReader zipReader) {
this.filePreviewCommonService = filePreviewCommonService;
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
this.zipReader = zipReader; this.compressFileReader = compressFileReader;
} }
@Override @Override
@ -38,7 +34,7 @@ public class CompressFilePreviewImpl implements FilePreview {
String suffix=fileAttribute.getSuffix(); String suffix=fileAttribute.getSuffix();
String fileTree = null; String fileTree = null;
// 判断文件名是否存在(redis缓存读取) // 判断文件名是否存在(redis缓存读取)
if (!StringUtils.hasText(filePreviewCommonService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) { if (!StringUtils.hasText(fileHandlerService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) { if (0 != response.getCode()) {
model.addAttribute("fileType", suffix); model.addAttribute("fileType", suffix);
@ -47,17 +43,17 @@ public class CompressFilePreviewImpl implements FilePreview {
} }
String filePath = response.getContent(); String filePath = response.getContent();
if ("zip".equalsIgnoreCase(suffix) || "jar".equalsIgnoreCase(suffix) || "gzip".equalsIgnoreCase(suffix)) { if ("zip".equalsIgnoreCase(suffix) || "jar".equalsIgnoreCase(suffix) || "gzip".equalsIgnoreCase(suffix)) {
fileTree = zipReader.readZipFile(filePath, fileName); fileTree = compressFileReader.readZipFile(filePath, fileName);
} else if ("rar".equalsIgnoreCase(suffix)) { } else if ("rar".equalsIgnoreCase(suffix)) {
fileTree = zipReader.unRar(filePath, fileName); fileTree = compressFileReader.unRar(filePath, fileName);
} else if ("7z".equalsIgnoreCase(suffix)) { } else if ("7z".equalsIgnoreCase(suffix)) {
fileTree = zipReader.read7zFile(filePath, fileName); fileTree = compressFileReader.read7zFile(filePath, fileName);
} }
if (fileTree != null && !"null".equals(fileTree) && ConfigConstants.isCacheEnabled()) { if (fileTree != null && !"null".equals(fileTree) && ConfigConstants.isCacheEnabled()) {
filePreviewCommonService.addConvertedFile(fileName, fileTree); fileHandlerService.addConvertedFile(fileName, fileTree);
} }
} else { } else {
fileTree = filePreviewCommonService.getConvertedFile(fileName); fileTree = fileHandlerService.getConvertedFile(fileName);
} }
if (fileTree != null && !"null".equals(fileTree)) { if (fileTree != null && !"null".equals(fileTree)) {
model.addAttribute("fileTree", fileTree); model.addAttribute("fileTree", fileTree);

View File

@ -5,6 +5,8 @@ import cn.keking.service.FilePreview;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import static com.sun.glass.ui.Clipboard.TEXT_TYPE;
/** /**
* @author kl (http://kailing.pub) * @author kl (http://kailing.pub)
* @since 2020/12/25 * @since 2020/12/25

View File

@ -4,7 +4,7 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FilePreviewCommonService; import cn.keking.service.FileHandlerService;
import cn.keking.web.filter.BaseUrlFilter; import cn.keking.web.filter.BaseUrlFilter;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -19,12 +19,12 @@ public class MediaFilePreviewImpl implements FilePreview {
private final DownloadUtils downloadUtils; private final DownloadUtils downloadUtils;
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
public MediaFilePreviewImpl(DownloadUtils downloadUtils, public MediaFilePreviewImpl(DownloadUtils downloadUtils,
FilePreviewCommonService filePreviewCommonService) { FileHandlerService fileHandlerService) {
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
} }
@Override @Override
@ -37,7 +37,7 @@ public class MediaFilePreviewImpl implements FilePreview {
model.addAttribute("msg", response.getMsg()); model.addAttribute("msg", response.getMsg());
return "fileNotSupported"; return "fileNotSupported";
} else { } else {
model.addAttribute("mediaUrl", BaseUrlFilter.getBaseUrl() + filePreviewCommonService.getRelativePath(response.getContent())); model.addAttribute("mediaUrl", BaseUrlFilter.getBaseUrl() + fileHandlerService.getRelativePath(response.getContent()));
} }
} else { } else {
model.addAttribute("mediaUrl", url); model.addAttribute("mediaUrl", url);

View File

@ -5,7 +5,7 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FilePreviewCommonService; import cn.keking.service.FileHandlerService;
import cn.keking.service.OfficeToPdfService; import cn.keking.service.OfficeToPdfService;
import cn.keking.utils.PdfUtils; import cn.keking.utils.PdfUtils;
import cn.keking.web.filter.BaseUrlFilter; import cn.keking.web.filter.BaseUrlFilter;
@ -22,13 +22,13 @@ import java.util.List;
@Service @Service
public class OfficeFilePreviewImpl implements FilePreview { public class OfficeFilePreviewImpl implements FilePreview {
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
private final PdfUtils pdfUtils; private final PdfUtils pdfUtils;
private final DownloadUtils downloadUtils; private final DownloadUtils downloadUtils;
private final OfficeToPdfService officeToPdfService; private final OfficeToPdfService officeToPdfService;
public OfficeFilePreviewImpl(FilePreviewCommonService filePreviewCommonService, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdfService officeToPdfService) { public OfficeFilePreviewImpl(FileHandlerService fileHandlerService, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdfService officeToPdfService) {
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
this.pdfUtils = pdfUtils; this.pdfUtils = pdfUtils;
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
this.officeToPdfService = officeToPdfService; this.officeToPdfService = officeToPdfService;
@ -49,7 +49,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf"); String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
String outFilePath = FILE_DIR + pdfName; String outFilePath = FILE_DIR + pdfName;
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换 // 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
if (!filePreviewCommonService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) { if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath; String filePath;
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) { if (0 != response.getCode()) {
@ -62,11 +62,11 @@ public class OfficeFilePreviewImpl implements FilePreview {
officeToPdfService.openOfficeToPDF(filePath, outFilePath); officeToPdfService.openOfficeToPDF(filePath, outFilePath);
if (isHtml) { if (isHtml) {
// 对转换后的文件进行操作(改变编码方式) // 对转换后的文件进行操作(改变编码方式)
filePreviewCommonService.doActionConvertedFile(outFilePath); fileHandlerService.doActionConvertedFile(outFilePath);
} }
if (ConfigConstants.isCacheEnabled()) { if (ConfigConstants.isCacheEnabled()) {
// 加入缓存 // 加入缓存
filePreviewCommonService.addConvertedFile(pdfName, filePreviewCommonService.getRelativePath(outFilePath)); fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
} }
} }
} }

View File

@ -5,7 +5,7 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FilePreviewCommonService; import cn.keking.service.FileHandlerService;
import cn.keking.utils.PdfUtils; import cn.keking.utils.PdfUtils;
import cn.keking.web.filter.BaseUrlFilter; import cn.keking.web.filter.BaseUrlFilter;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -20,7 +20,7 @@ import java.util.List;
@Service @Service
public class PdfFilePreviewImpl implements FilePreview { public class PdfFilePreviewImpl implements FilePreview {
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
private final PdfUtils pdfUtils; private final PdfUtils pdfUtils;
@ -28,10 +28,10 @@ public class PdfFilePreviewImpl implements FilePreview {
private static final String FILE_DIR = ConfigConstants.getFileDir(); private static final String FILE_DIR = ConfigConstants.getFileDir();
public PdfFilePreviewImpl(FilePreviewCommonService filePreviewCommonService, public PdfFilePreviewImpl(FileHandlerService fileHandlerService,
PdfUtils pdfUtils, PdfUtils pdfUtils,
DownloadUtils downloadUtils) { DownloadUtils downloadUtils) {
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
this.pdfUtils = pdfUtils; this.pdfUtils = pdfUtils;
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
} }
@ -46,7 +46,7 @@ public class PdfFilePreviewImpl implements FilePreview {
String outFilePath = FILE_DIR + pdfName; String outFilePath = FILE_DIR + pdfName;
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) { if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
//当文件不存在时,就去下载 //当文件不存在时,就去下载
if (!filePreviewCommonService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) { if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) { if (0 != response.getCode()) {
model.addAttribute("fileType", suffix); model.addAttribute("fileType", suffix);
@ -56,7 +56,7 @@ public class PdfFilePreviewImpl implements FilePreview {
outFilePath = response.getContent(); outFilePath = response.getContent();
if (ConfigConstants.isCacheEnabled()) { if (ConfigConstants.isCacheEnabled()) {
// 加入缓存 // 加入缓存
filePreviewCommonService.addConvertedFile(pdfName, filePreviewCommonService.getRelativePath(outFilePath)); fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
} }
} }
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl); List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl);
@ -75,17 +75,17 @@ public class PdfFilePreviewImpl implements FilePreview {
} else { } else {
// 不是http开头浏览器不能直接访问需下载到本地 // 不是http开头浏览器不能直接访问需下载到本地
if (url != null && !url.toLowerCase().startsWith("http")) { if (url != null && !url.toLowerCase().startsWith("http")) {
if (!filePreviewCommonService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) { if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, pdfName); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, pdfName);
if (0 != response.getCode()) { if (0 != response.getCode()) {
model.addAttribute("fileType", suffix); model.addAttribute("fileType", suffix);
model.addAttribute("msg", response.getMsg()); model.addAttribute("msg", response.getMsg());
return "fileNotSupported"; return "fileNotSupported";
} }
model.addAttribute("pdfUrl", filePreviewCommonService.getRelativePath(response.getContent())); model.addAttribute("pdfUrl", fileHandlerService.getRelativePath(response.getContent()));
if (ConfigConstants.isCacheEnabled()) { if (ConfigConstants.isCacheEnabled()) {
// 加入缓存 // 加入缓存
filePreviewCommonService.addConvertedFile(pdfName, filePreviewCommonService.getRelativePath(outFilePath)); fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
} }
} else { } else {
model.addAttribute("pdfUrl", pdfName); model.addAttribute("pdfUrl", pdfName);

View File

@ -4,7 +4,7 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FilePreviewCommonService; import cn.keking.service.FileHandlerService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -18,13 +18,13 @@ import java.util.List;
@Service @Service
public class PictureFilePreviewImpl implements FilePreview { public class PictureFilePreviewImpl implements FilePreview {
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
private final DownloadUtils downloadUtils; private final DownloadUtils downloadUtils;
public PictureFilePreviewImpl(FilePreviewCommonService filePreviewCommonService, public PictureFilePreviewImpl(FileHandlerService fileHandlerService,
DownloadUtils downloadUtils) { DownloadUtils downloadUtils) {
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
} }
@ -33,7 +33,7 @@ public class PictureFilePreviewImpl implements FilePreview {
List<String> imgUrls = new ArrayList<>(); List<String> imgUrls = new ArrayList<>();
imgUrls.add(url); imgUrls.add(url);
String fileKey = fileAttribute.getFileKey(); String fileKey = fileAttribute.getFileKey();
List<String> zipImgUrls = filePreviewCommonService.getImgCache(fileKey); List<String> zipImgUrls = fileHandlerService.getImgCache(fileKey);
if (!CollectionUtils.isEmpty(zipImgUrls)) { if (!CollectionUtils.isEmpty(zipImgUrls)) {
imgUrls.addAll(zipImgUrls); imgUrls.addAll(zipImgUrls);
} }
@ -45,7 +45,7 @@ public class PictureFilePreviewImpl implements FilePreview {
model.addAttribute("msg", response.getMsg()); model.addAttribute("msg", response.getMsg());
return "fileNotSupported"; return "fileNotSupported";
} else { } else {
String file = filePreviewCommonService.getRelativePath(response.getContent()); String file = fileHandlerService.getRelativePath(response.getContent());
imgUrls.clear(); imgUrls.clear();
imgUrls.add(file); imgUrls.add(file);
model.addAttribute("imgurls", imgUrls); model.addAttribute("imgurls", imgUrls);

View File

@ -1,7 +1,6 @@
package cn.keking.service.impl; package cn.keking.service.impl;
import cn.keking.model.FileAttribute; import cn.keking.model.FileAttribute;
import cn.keking.model.FileType;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
@ -13,7 +12,6 @@ import org.springframework.util.Base64Utils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
/** /**
* Created by kl on 2018/1/17. * Created by kl on 2018/1/17.
@ -22,8 +20,10 @@ import java.nio.file.Files;
@Service @Service
public class SimTextFilePreviewImpl implements FilePreview { public class SimTextFilePreviewImpl implements FilePreview {
private final DownloadUtils downloadUtils; public static final String TEXT_TYPE = "textType";
public static final String DEFAULT_TEXT_TYPE = "simText";
private final DownloadUtils downloadUtils;
public SimTextFilePreviewImpl(DownloadUtils downloadUtils) { public SimTextFilePreviewImpl(DownloadUtils downloadUtils) {
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;

View File

@ -5,6 +5,8 @@ import cn.keking.service.FilePreview;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import static com.sun.glass.ui.Clipboard.TEXT_TYPE;
/** /**
* @author kl (http://kailing.pub) * @author kl (http://kailing.pub)
* @since 2020/12/25 * @since 2020/12/25

View File

@ -5,7 +5,7 @@ import cn.keking.hutool.URLUtil;
import cn.keking.model.FileAttribute; import cn.keking.model.FileAttribute;
import cn.keking.model.FileType; import cn.keking.model.FileType;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreviewCommonService; import cn.keking.service.FileHandlerService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -21,20 +21,18 @@ import java.util.UUID;
@Component @Component
public class DownloadUtils { public class DownloadUtils {
private final Logger logger = LoggerFactory.getLogger(DownloadUtils.class); private final static Logger logger = LoggerFactory.getLogger(DownloadUtils.class);
private final String fileDir = ConfigConstants.getFileDir(); private final String fileDir = ConfigConstants.getFileDir();
private final FilePreviewCommonService filePreviewCommonService;
public DownloadUtils(FilePreviewCommonService filePreviewCommonService) {
this.filePreviewCommonService = filePreviewCommonService;
}
private static final String URL_PARAM_FTP_USERNAME = "ftp.username"; private static final String URL_PARAM_FTP_USERNAME = "ftp.username";
private static final String URL_PARAM_FTP_PASSWORD = "ftp.password"; private static final String URL_PARAM_FTP_PASSWORD = "ftp.password";
private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding"; private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding";
private final FileHandlerService fileHandlerService;
public DownloadUtils(FileHandlerService fileHandlerService) {
this.fileHandlerService = fileHandlerService;
}
/** /**
* @param fileAttribute fileAttribute * @param fileAttribute fileAttribute
* @param fileName * @param fileName
@ -46,25 +44,25 @@ public class DownloadUtils {
ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", ""); ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
if (null == fileName) { if (null == fileName) {
fileName = uuid+ "."+type; fileName = uuid + "." + type;
} else { // 文件后缀不一致时以type为准(针对simText【将类txt文件转为txt】) } else { // 文件后缀不一致时以type为准(针对simText【将类txt文件转为txt】)
fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type); fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
} }
String realPath = fileDir + fileName; String realPath = fileDir + fileName;
File dirFile = new File(fileDir); File dirFile = new File(fileDir);
if (!dirFile.exists()) { if (!dirFile.exists() && !dirFile.mkdirs()) {
dirFile.mkdirs(); logger.error("创建目录【{}】失败,可能是权限不够,请检查", fileDir);
} }
try { try {
URL url = new URL(urlStr); URL url = new URL(urlStr);
if (url.getProtocol() != null && (url.getProtocol().toLowerCase().startsWith("file")||url.getProtocol().toLowerCase().startsWith("http"))) { if (url.getProtocol() != null && (url.getProtocol().toLowerCase().startsWith("file") || url.getProtocol().toLowerCase().startsWith("http"))) {
byte[] bytes = getBytesFromUrl(urlStr); byte[] bytes = getBytesFromUrl(urlStr);
OutputStream os = new FileOutputStream(realPath); OutputStream os = new FileOutputStream(realPath);
saveBytesToOutStream(bytes, os); saveBytesToOutStream(bytes, os);
} else if (url.getProtocol() != null && "ftp".equalsIgnoreCase(url.getProtocol())) { } else if (url.getProtocol() != null && "ftp".equalsIgnoreCase(url.getProtocol())) {
String ftpUsername = filePreviewCommonService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); String ftpUsername = fileHandlerService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = filePreviewCommonService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD); String ftpPassword = fileHandlerService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
String ftpControlEncoding = filePreviewCommonService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING); String ftpControlEncoding = fileHandlerService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding); FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding);
} else { } else {
response.setCode(1); response.setCode(1);
@ -73,8 +71,8 @@ public class DownloadUtils {
} }
response.setContent(realPath); response.setContent(realPath);
response.setMsg(fileName); response.setMsg(fileName);
if(FileType.simText.equals(fileAttribute.getType())){ if (FileType.simText.equals(fileAttribute.getType())) {
convertTextPlainFileCharsetToUtf8(realPath); this.convertTextPlainFileCharsetToUtf8(realPath);
} }
return response; return response;
} catch (IOException e) { } catch (IOException e) {
@ -92,17 +90,15 @@ public class DownloadUtils {
public byte[] getBytesFromUrl(String urlStr) throws IOException { public byte[] getBytesFromUrl(String urlStr) throws IOException {
InputStream is = getInputStreamFromUrl(urlStr); InputStream is = getInputStreamFromUrl(urlStr);
if (is != null) { if (is == null) {
return getBytesFromStream(is);
} else {
urlStr = URLUtil.normalize(urlStr, true, true); urlStr = URLUtil.normalize(urlStr, true, true);
is = getInputStreamFromUrl(urlStr); is = getInputStreamFromUrl(urlStr);
if (is == null) { if (is == null) {
logger.error("文件下载异常url{}", urlStr); logger.error("文件下载异常url{}", urlStr);
throw new IOException("文件下载异常url" + urlStr); throw new IOException("文件下载异常url" + urlStr);
} }
return getBytesFromStream(is);
} }
return getBytesFromStream(is);
} }
public void saveBytesToOutStream(byte[] b, OutputStream os) throws IOException { public void saveBytesToOutStream(byte[] b, OutputStream os) throws IOException {
@ -127,7 +123,7 @@ public class DownloadUtils {
private byte[] getBytesFromStream(InputStream is) throws IOException { private byte[] getBytesFromStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int len = 0; int len;
while ((len = is.read(buffer)) != -1) { while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len); baos.write(buffer, 0, len);
} }
@ -140,37 +136,33 @@ public class DownloadUtils {
/** /**
* utf8 * utf8
* ,utf8 * ,utf8
*
* @param filePath * @param filePath
*/ */
private static void convertTextPlainFileCharsetToUtf8(String filePath) throws IOException { private void convertTextPlainFileCharsetToUtf8(String filePath) throws IOException {
File sourceFile = new File(filePath); File sourceFile = new File(filePath);
if(sourceFile.exists() && sourceFile.isFile() && sourceFile.canRead()) { if (sourceFile.exists() && sourceFile.isFile() && sourceFile.canRead()) {
String encoding = null; String encoding = FileUtils.getFileEncode(filePath);
try { if (!FileUtils.DEFAULT_FILE_ENCODING.equals(encoding)) {
FileCharsetDetector.Observer observer = FileCharsetDetector.guessFileEncoding(sourceFile);
// 为准确探测到编码,不适用猜测的编码
encoding = observer.isFound()?observer.getEncoding():null;
// 为准确探测到编码,可以考虑使用GBK 大部分文件都是windows系统产生的
} catch (IOException e) {
// 编码探测失败,
e.printStackTrace();
}
if(encoding != null && !"UTF-8".equals(encoding)){
// 不为utf8,进行转码 // 不为utf8,进行转码
File tmpUtf8File = new File(filePath+".utf8"); File tmpUtf8File = new File(filePath + ".utf8");
Writer writer = new OutputStreamWriter(new FileOutputStream(tmpUtf8File), StandardCharsets.UTF_8); Writer writer = new OutputStreamWriter(new FileOutputStream(tmpUtf8File), StandardCharsets.UTF_8);
Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile),encoding)); Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile), encoding));
char[] buf = new char[1024]; char[] buf = new char[1024];
int read; int read;
while ((read = reader.read(buf)) > 0){ while ((read = reader.read(buf)) > 0) {
writer.write(buf, 0, read); writer.write(buf, 0, read);
} }
reader.close(); reader.close();
writer.close(); writer.close();
// 删除源文件 // 删除源文件
sourceFile.delete(); if (!sourceFile.delete()) {
logger.error("源文件【{}】删除失败,请检查文件目录权限!", filePath);
}
// 重命名 // 重命名
tmpUtf8File.renameTo(sourceFile); if (tmpUtf8File.renameTo(sourceFile)) {
logger.error("临时文件【{}】重命名失败,请检查文件路径权限!", tmpUtf8File.getPath());
}
} }
} }
} }

View File

@ -1,157 +0,0 @@
package cn.keking.utils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
/**
*
*
* @author HWliao
* @date 2017-12-24
*/
public class FileCharsetDetector {
/**
* (File)
*
* @param file File
* @return null
* @throws FileNotFoundException
* @throws IOException
*/
public static Observer guessFileEncoding(File file)
throws FileNotFoundException, IOException {
return guessFileEncoding(file, new nsDetector());
}
/**
* <pre>
*
* @param file
* File
* @param languageHint
* @see #nsPSMDetector ,
* 1 : Japanese
* 2 : Chinese
* 3 : Simplified Chinese
* 4 : Traditional Chinese
* 5 : Korean
* 6 : Dont know(default)
* </pre>
*
* @return egUTF-8,GBK,GB2312()null
* @throws FileNotFoundException
* @throws IOException
*/
public static Observer guessFileEncoding(File file, int languageHint)
throws FileNotFoundException, IOException {
return guessFileEncoding(file, new nsDetector(languageHint));
}
/**
*
*
* @param file
* @param det
* @return
* @throws FileNotFoundException
* @throws IOException
*/
private static Observer guessFileEncoding(File file, nsDetector det)
throws FileNotFoundException, IOException {
// new Observer
Observer observer = new Observer();
// set Observer
// The Notify() will be called when a matching charset is found.
det.Init(observer);
BufferedInputStream imp = new BufferedInputStream(new FileInputStream(
file));
byte[] buf = new byte[1024];
int len;
boolean done = false;
boolean isAscii = false;
while ((len = imp.read(buf, 0, buf.length)) != -1) {
// Check if the stream is only ascii.
isAscii = det.isAscii(buf, len);
if (isAscii) {
break;
}
// DoIt if non-ascii and not done yet.
done = det.DoIt(buf, len, false);
if (done) {
break;
}
}
imp.close();
det.DataEnd();
if (isAscii) {
observer.encoding = "ASCII";
observer.found = true;
}
if (!observer.isFound()) {
String[] prob = det.getProbableCharsets();
// // 这里将可能的字符集组合起来返回
// for (int i = 0; i < prob.length; i++) {
// if (i == 0) {
// encoding = prob[i];
// } else {
// encoding += "," + prob[i];
// }
// }
if (prob.length > 0) {
// 在没有发现情况下,去第一个可能的编码
observer.encoding = prob[0];
} else {
observer.encoding = null;
}
}
return observer;
}
/**
* @author liaohongwei
* @Description: ,
* @date 2016620 2:27:06
*/
public static class Observer implements nsICharsetDetectionObserver {
/**
* @Fields encoding :
*/
private String encoding = null;
/**
* @Fields found :
*/
private boolean found = false;
@Override
public void Notify(String charset) {
this.encoding = charset;
this.found = true;
}
public String getEncoding() {
return encoding;
}
public boolean isFound() {
return found;
}
@Override
public String toString() {
return "Observer [encoding=" + encoding + ", found=" + found + "]";
}
}
}

View File

@ -1,14 +1,18 @@
package cn.keking.utils; package cn.keking.utils;
import cpdetector.CharsetPrinter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Objects; import java.util.Objects;
public class DeleteFileUtil { public class FileUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(DeleteFileUtil.class); private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
public static final String DEFAULT_FILE_ENCODING = "UTF-8";
/** /**
* *
@ -17,7 +21,7 @@ public class DeleteFileUtil {
* *
* @return truefalse * @return truefalse
*/ */
public static boolean deleteFile(String fileName) { public static boolean deleteFileByName(String fileName) {
File file = new File(fileName); File file = new File(fileName);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除 // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if (file.exists() && file.isFile()) { if (file.exists() && file.isFile()) {
@ -34,6 +38,36 @@ public class DeleteFileUtil {
} }
} }
/**
*
*
* @param filePath
* @return
*/
public static String getFileEncode(String filePath) {
File file = new File(filePath);
CharsetPrinter cp = new CharsetPrinter();
try {
String encoding = cp.guessEncoding(file);
LOGGER.info("检测到文件【{}】编码: {}", filePath, encoding);
return encoding;
} catch (IOException e) {
LOGGER.warn("文件编码获取失败采用默认的编码格式UTF-8", e);
return DEFAULT_FILE_ENCODING;
}
}
/**
*
*
* @param filePath
*/
public static void deleteFileByPath(String filePath) {
File file = new File(filePath);
if (file.exists() && !file.delete()) {
LOGGER.warn("压缩包源文件删除失败:{}", filePath);
}
}
/** /**
* *
@ -59,20 +93,20 @@ public class DeleteFileUtil {
for (int i = 0; i < Objects.requireNonNull(files).length; i++) { for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
// 删除子文件 // 删除子文件
if (files[i].isFile()) { if (files[i].isFile()) {
flag = DeleteFileUtil.deleteFile(files[i].getAbsolutePath()); flag = FileUtils.deleteFileByName(files[i].getAbsolutePath());
if (!flag) { if (!flag) {
break; break;
} }
} else if (files[i].isDirectory()) { } else if (files[i].isDirectory()) {
// 删除子目录 // 删除子目录
flag = DeleteFileUtil.deleteDirectory(files[i].getAbsolutePath()); flag = FileUtils.deleteDirectory(files[i].getAbsolutePath());
if (!flag) { if (!flag) {
break; break;
} }
} }
} }
dirFile.delete();
if (!flag) { if (!dirFile.delete() || !flag) {
LOGGER.info("删除目录失败!"); LOGGER.info("删除目录失败!");
return false; return false;
} }

View File

@ -1,6 +1,6 @@
package cn.keking.utils; package cn.keking.utils;
import cn.keking.service.FilePreviewCommonService; import cn.keking.service.FileHandlerService;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.rendering.PDFRenderer;
@ -23,18 +23,18 @@ public class PdfUtils {
private final Logger logger = LoggerFactory.getLogger(PdfUtils.class); private final Logger logger = LoggerFactory.getLogger(PdfUtils.class);
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
@Value("${server.tomcat.uri-encoding:UTF-8}") @Value("${server.tomcat.uri-encoding:UTF-8}")
private String uriEncoding; private String uriEncoding;
public PdfUtils(FilePreviewCommonService filePreviewCommonService) { public PdfUtils(FileHandlerService fileHandlerService) {
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
} }
public List<String> pdf2jpg(String pdfFilePath, String pdfName, String baseUrl) { public List<String> pdf2jpg(String pdfFilePath, String pdfName, String baseUrl) {
List<String> imageUrls = new ArrayList<>(); List<String> imageUrls = new ArrayList<>();
Integer imageCount = filePreviewCommonService.getConvertedPdfImage(pdfFilePath); Integer imageCount = fileHandlerService.getConvertedPdfImage(pdfFilePath);
String imageFileSuffix = ".jpg"; String imageFileSuffix = ".jpg";
String pdfFolder = pdfName.substring(0, pdfName.length() - 4); String pdfFolder = pdfName.substring(0, pdfName.length() - 4);
String urlPrefix = null; String urlPrefix = null;
@ -70,7 +70,7 @@ public class PdfUtils {
imageUrls.add(urlPrefix + "/" + pageIndex + imageFileSuffix); imageUrls.add(urlPrefix + "/" + pageIndex + imageFileSuffix);
} }
doc.close(); doc.close();
filePreviewCommonService.addConvertedPdfImage(pdfFilePath, pageCount); fileHandlerService.addConvertedPdfImage(pdfFilePath, pageCount);
} catch (IOException e) { } catch (IOException e) {
logger.error("Convert pdf to jpg exception, pdfFilePath{}", pdfFilePath, e); logger.error("Convert pdf to jpg exception, pdfFilePath{}", pdfFilePath, e);
} }

View File

@ -31,7 +31,7 @@ public class ShedulerClean {
public void clean() { public void clean() {
logger.info("Cache clean start"); logger.info("Cache clean start");
cacheService.cleanCache(); cacheService.cleanCache();
DeleteFileUtil.deleteDirectory(fileDir); FileUtils.deleteDirectory(fileDir);
logger.info("Cache clean end"); logger.info("Cache clean end");
} }
} }

View File

@ -6,7 +6,7 @@ import cn.keking.service.FilePreviewFactory;
import cn.keking.service.cache.CacheService; import cn.keking.service.cache.CacheService;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FilePreviewCommonService; import cn.keking.service.FileHandlerService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -32,12 +32,12 @@ public class OnlinePreviewController {
private final FilePreviewFactory previewFactory; private final FilePreviewFactory previewFactory;
private final CacheService cacheService; private final CacheService cacheService;
private final FilePreviewCommonService filePreviewCommonService; private final FileHandlerService fileHandlerService;
private final DownloadUtils downloadUtils; private final DownloadUtils downloadUtils;
public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FilePreviewCommonService filePreviewCommonService, CacheService cacheService, DownloadUtils downloadUtils) { public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService, DownloadUtils downloadUtils) {
this.previewFactory = filePreviewFactory; this.previewFactory = filePreviewFactory;
this.filePreviewCommonService = filePreviewCommonService; this.fileHandlerService = fileHandlerService;
this.cacheService = cacheService; this.cacheService = cacheService;
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
} }
@ -45,7 +45,7 @@ public class OnlinePreviewController {
@RequestMapping(value = "/onlinePreview") @RequestMapping(value = "/onlinePreview")
public String onlinePreview(String url, Model model, HttpServletRequest req) { public String onlinePreview(String url, Model model, HttpServletRequest req) {
FileAttribute fileAttribute = filePreviewCommonService.getFileAttribute(url,req); FileAttribute fileAttribute = fileHandlerService.getFileAttribute(url,req);
FilePreview filePreview = previewFactory.get(fileAttribute); FilePreview filePreview = previewFactory.get(fileAttribute);
logger.info("预览文件url{}previewType{}", url, fileAttribute.getType()); logger.info("预览文件url{}previewType{}", url, fileAttribute.getType());
return filePreview.filePreviewHandle(url, model, fileAttribute); return filePreview.filePreviewHandle(url, model, fileAttribute);