Commit 180e7bcb authored by 陈精华's avatar 陈精华 Committed by kl

优化:重构大量代码,修复异常

parent 8a524506
...@@ -48,7 +48,7 @@ trust.host = ${KK_TRUST_HOST:default} ...@@ -48,7 +48,7 @@ trust.host = ${KK_TRUST_HOST:default}
cache.enabled = ${KK_CACHE_ENABLED:true} cache.enabled = ${KK_CACHE_ENABLED:true}
#文本类型,默认如下,可自定义添加 #文本类型,默认如下,可自定义添加
simText = ${KK_SIMTEXT:txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd} simText = ${KK_SIMTEXT:txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd}
#多媒体类型,默认如下,可自定义添加 #多媒体类型,默认如下,可自定义添加
media = ${KK_MEDIA:mp3,wav,mp4,flv} media = ${KK_MEDIA:mp3,wav,mp4,flv}
#office类型文档(word ppt)样式,默认为图片(image),可配置为pdf(预览时也有按钮切换) #office类型文档(word ppt)样式,默认为图片(image),可配置为pdf(预览时也有按钮切换)
......
...@@ -10,9 +10,8 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -10,9 +10,8 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.Set; import java.util.Set;
/** /**
* @auther: chenjh * @author: chenjh
* @time: 2019/4/10 17:22 * @since: 2019/4/10 17:22
* @description
*/ */
@Component @Component
public class ConfigConstants { public class ConfigConstants {
...@@ -30,7 +29,7 @@ public class ConfigConstants { ...@@ -30,7 +29,7 @@ public class ConfigConstants {
private static String PDF_DOWNLOAD_DISABLE; private static String PDF_DOWNLOAD_DISABLE;
public static final String DEFAULT_CACHE_ENABLED = "true"; public static final String DEFAULT_CACHE_ENABLED = "true";
public static final String DEFAULT_TXT_TYPE = "txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd"; public static final String DEFAULT_TXT_TYPE = "txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd";
public static final String DEFAULT_MEDIA_TYPE = "mp3,wav,mp4,flv"; public static final String DEFAULT_MEDIA_TYPE = "mp3,wav,mp4,flv";
public static final String DEFAULT_OFFICE_PREVIEW_TYPE = "image"; public static final String DEFAULT_OFFICE_PREVIEW_TYPE = "image";
public static final String DEFAULT_FTP_USERNAME = null; public static final String DEFAULT_FTP_USERNAME = null;
...@@ -45,7 +44,12 @@ public class ConfigConstants { ...@@ -45,7 +44,12 @@ public class ConfigConstants {
return CACHE_ENABLED; return CACHE_ENABLED;
} }
public static void setCacheEnabled(Boolean cacheEnabled) { @Value("${cache.enabled:true}")
public static void setCacheEnabled(String cacheEnabled) {
setCacheEnabledValueValue(Boolean.parseBoolean(cacheEnabled));
}
public static void setCacheEnabledValueValue(Boolean cacheEnabled) {
CACHE_ENABLED = cacheEnabled; CACHE_ENABLED = cacheEnabled;
} }
...@@ -53,7 +57,13 @@ public class ConfigConstants { ...@@ -53,7 +57,13 @@ public class ConfigConstants {
return SIM_TEXT; return SIM_TEXT;
} }
public static void setSimText(String[] simText) { @Value("${simText:txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd}")
public void setSimText(String simText) {
String[] simTextArr = simText.split(",");
setSimTextValue(simTextArr);
}
public static void setSimTextValue(String[] simText) {
SIM_TEXT = simText; SIM_TEXT = simText;
} }
...@@ -61,15 +71,26 @@ public class ConfigConstants { ...@@ -61,15 +71,26 @@ public class ConfigConstants {
return MEDIA; return MEDIA;
} }
public static void setMedia(String[] Media) { @Value("${media:mp3,wav,mp4,flv}")
ConfigConstants.MEDIA = Media; public void setMedia(String media) {
String[] mediaArr = media.split(",");
setMediaValue(mediaArr);
}
public static void setMediaValue(String[] Media) {
MEDIA = Media;
} }
public static String getOfficePreviewType() { public static String getOfficePreviewType() {
return OFFICE_PREVIEW_TYPE; return OFFICE_PREVIEW_TYPE;
} }
public static void setOfficePreviewType(String officePreviewType) { @Value("${office.preview.type:image}")
public void setOfficePreviewType(String officePreviewType) {
setOfficePreviewTypeValue(officePreviewType);
}
public static void setOfficePreviewTypeValue(String officePreviewType) {
OFFICE_PREVIEW_TYPE = officePreviewType; OFFICE_PREVIEW_TYPE = officePreviewType;
} }
...@@ -77,7 +98,12 @@ public class ConfigConstants { ...@@ -77,7 +98,12 @@ public class ConfigConstants {
return FTP_USERNAME; return FTP_USERNAME;
} }
public static void setFtpUsername(String ftpUsername) { @Value("${ftp.username:}")
public void setFtpUsername(String ftpUsername) {
setFtpUsernameValue(ftpUsername);
}
public static void setFtpUsernameValue(String ftpUsername) {
FTP_USERNAME = ftpUsername; FTP_USERNAME = ftpUsername;
} }
...@@ -85,7 +111,12 @@ public class ConfigConstants { ...@@ -85,7 +111,12 @@ public class ConfigConstants {
return FTP_PASSWORD; return FTP_PASSWORD;
} }
public static void setFtpPassword(String ftpPassword) { @Value("${ftp.password:}")
public void setFtpPassword(String ftpPassword) {
setFtpPasswordValue(ftpPassword);
}
public static void setFtpPasswordValue(String ftpPassword) {
FTP_PASSWORD = ftpPassword; FTP_PASSWORD = ftpPassword;
} }
...@@ -93,7 +124,12 @@ public class ConfigConstants { ...@@ -93,7 +124,12 @@ public class ConfigConstants {
return FTP_CONTROL_ENCODING; return FTP_CONTROL_ENCODING;
} }
public static void setFtpControlEncoding(String ftpControlEncoding) { @Value("${ftp.control.encoding:UTF-8}")
public void setFtpControlEncoding(String ftpControlEncoding) {
setFtpControlEncodingValue(ftpControlEncoding);
}
public static void setFtpControlEncodingValue(String ftpControlEncoding) {
FTP_CONTROL_ENCODING = ftpControlEncoding; FTP_CONTROL_ENCODING = ftpControlEncoding;
} }
...@@ -101,7 +137,12 @@ public class ConfigConstants { ...@@ -101,7 +137,12 @@ public class ConfigConstants {
return BASE_URL; return BASE_URL;
} }
public static void setBaseUrl(String baseUrl) { @Value("${base.url:default}")
public void setBaseUrl(String baseUrl) {
setBaseUrlValue(baseUrl);
}
public static void setBaseUrlValue(String baseUrl) {
BASE_URL = baseUrl; BASE_URL = baseUrl;
} }
...@@ -111,6 +152,10 @@ public class ConfigConstants { ...@@ -111,6 +152,10 @@ public class ConfigConstants {
@Value("${file.dir:default}") @Value("${file.dir:default}")
public void setFileDir(String fileDir) { public void setFileDir(String fileDir) {
setFileDirValue(fileDir);
}
public static void setFileDirValue(String fileDir) {
if (!DEFAULT_FILE_DIR_VALUE.equals(fileDir.toLowerCase())) { if (!DEFAULT_FILE_DIR_VALUE.equals(fileDir.toLowerCase())) {
if (!fileDir.endsWith(File.separator)) { if (!fileDir.endsWith(File.separator)) {
fileDir = fileDir + File.separator; fileDir = fileDir + File.separator;
...@@ -120,11 +165,11 @@ public class ConfigConstants { ...@@ -120,11 +165,11 @@ public class ConfigConstants {
} }
@Value("${trust.host:default}") @Value("${trust.host:default}")
public void setTrustHostStr(String trustHost) { public void setTrustHost(String trustHost) {
setTrustHost(trustHost); setTrustHostValue(trustHost);
} }
public static void setTrustHost(String trustHost) { public static void setTrustHostValue(String trustHost) {
CopyOnWriteArraySet<String> trustHostSet; CopyOnWriteArraySet<String> trustHostSet;
if (DEFAULT_TRUST_HOST.equals(trustHost.toLowerCase())) { if (DEFAULT_TRUST_HOST.equals(trustHost.toLowerCase())) {
trustHostSet = new CopyOnWriteArraySet<>(); trustHostSet = new CopyOnWriteArraySet<>();
...@@ -144,17 +189,17 @@ public class ConfigConstants { ...@@ -144,17 +189,17 @@ public class ConfigConstants {
ConfigConstants.TRUST_HOST_SET = trustHostSet; ConfigConstants.TRUST_HOST_SET = trustHostSet;
} }
public static String getPdfDownloadDisable() { public static String getPdfDownloadDisable() {
return PDF_DOWNLOAD_DISABLE; return PDF_DOWNLOAD_DISABLE;
} }
public static void setPdfDownloadDisableValue(String pdfDownloadDisable) {
PDF_DOWNLOAD_DISABLE = pdfDownloadDisable;
}
@Value("${pdf.download.disable:true}") @Value("${pdf.download.disable:true}")
public void setPdfDownloadDisable(String pdfDownloadDisable) { public void setPdfDownloadDisable(String pdfDownloadDisable) {
setPdfDownloadDisableValue(pdfDownloadDisable);
}
public static void setPdfDownloadDisableValue(String pdfDownloadDisable) {
PDF_DOWNLOAD_DISABLE = pdfDownloadDisable; PDF_DOWNLOAD_DISABLE = pdfDownloadDisable;
} }
} }
...@@ -27,14 +27,14 @@ public class ConfigRefreshComponent { ...@@ -27,14 +27,14 @@ public class ConfigRefreshComponent {
configRefreshThread.start(); configRefreshThread.start();
} }
class ConfigRefreshThread implements Runnable { static class ConfigRefreshThread implements Runnable {
@Override @Override
public void run() { public void run() {
try { try {
Properties properties = new Properties(); Properties properties = new Properties();
String text; String text;
String media; String media;
Boolean cacheEnabled; boolean cacheEnabled;
String[] textArray; String[] textArray;
String[] mediaArray; String[] mediaArray;
String officePreviewType; String officePreviewType;
...@@ -50,7 +50,7 @@ public class ConfigRefreshComponent { ...@@ -50,7 +50,7 @@ public class ConfigRefreshComponent {
BufferedReader bufferedReader = new BufferedReader(fileReader); BufferedReader bufferedReader = new BufferedReader(fileReader);
properties.load(bufferedReader); properties.load(bufferedReader);
OfficeUtils.restorePropertiesFromEnvFormat(properties); OfficeUtils.restorePropertiesFromEnvFormat(properties);
cacheEnabled = new Boolean(properties.getProperty("cache.enabled", ConfigConstants.DEFAULT_CACHE_ENABLED)); cacheEnabled = Boolean.parseBoolean(properties.getProperty("cache.enabled", ConfigConstants.DEFAULT_CACHE_ENABLED));
text = properties.getProperty("simText", ConfigConstants.DEFAULT_TXT_TYPE); text = properties.getProperty("simText", ConfigConstants.DEFAULT_TXT_TYPE);
media = properties.getProperty("media", ConfigConstants.DEFAULT_MEDIA_TYPE); media = properties.getProperty("media", ConfigConstants.DEFAULT_MEDIA_TYPE);
officePreviewType = properties.getProperty("office.preview.type", ConfigConstants.DEFAULT_OFFICE_PREVIEW_TYPE); officePreviewType = properties.getProperty("office.preview.type", ConfigConstants.DEFAULT_OFFICE_PREVIEW_TYPE);
...@@ -62,15 +62,15 @@ public class ConfigRefreshComponent { ...@@ -62,15 +62,15 @@ public class ConfigRefreshComponent {
baseUrl = properties.getProperty("base.url", ConfigConstants.DEFAULT_BASE_URL); baseUrl = properties.getProperty("base.url", ConfigConstants.DEFAULT_BASE_URL);
trustHost = properties.getProperty("trust.host", ConfigConstants.DEFAULT_TRUST_HOST); trustHost = properties.getProperty("trust.host", ConfigConstants.DEFAULT_TRUST_HOST);
pdfDownloadDisable = properties.getProperty("pdf.download.disable", ConfigConstants.DEFAULT_PDF_DOWNLOAD_DISABLE); pdfDownloadDisable = properties.getProperty("pdf.download.disable", ConfigConstants.DEFAULT_PDF_DOWNLOAD_DISABLE);
ConfigConstants.setCacheEnabled(cacheEnabled); ConfigConstants.setCacheEnabledValueValue(cacheEnabled);
ConfigConstants.setSimText(textArray); ConfigConstants.setSimTextValue(textArray);
ConfigConstants.setMedia(mediaArray); ConfigConstants.setMediaValue(mediaArray);
ConfigConstants.setOfficePreviewType(officePreviewType); ConfigConstants.setOfficePreviewTypeValue(officePreviewType);
ConfigConstants.setFtpUsername(ftpUsername); ConfigConstants.setFtpUsernameValue(ftpUsername);
ConfigConstants.setFtpPassword(ftpPassword); ConfigConstants.setFtpPasswordValue(ftpPassword);
ConfigConstants.setFtpControlEncoding(ftpControlEncoding); ConfigConstants.setFtpControlEncodingValue(ftpControlEncoding);
ConfigConstants.setBaseUrl(baseUrl); ConfigConstants.setBaseUrlValue(baseUrl);
ConfigConstants.setTrustHost(trustHost); ConfigConstants.setTrustHostValue(trustHost);
ConfigConstants.setPdfDownloadDisableValue(pdfDownloadDisable); ConfigConstants.setPdfDownloadDisableValue(pdfDownloadDisable);
setWatermarkConfig(properties); setWatermarkConfig(properties);
bufferedReader.close(); bufferedReader.close();
......
...@@ -7,9 +7,8 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry ...@@ -7,9 +7,8 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/** /**
* @auther: chenjh * @author: chenjh
* @time: 2019/4/16 20:04 * @since: 2019/4/16 20:04
* @description
*/ */
@Configuration @Configuration
public class WebConfig extends WebMvcConfigurerAdapter { public class WebConfig extends WebMvcConfigurerAdapter {
......
package cn.keking.extend; package cn.keking.extend;
import com.google.common.collect.Maps;
import com.sun.star.beans.PropertyValue;
import org.artofsolving.jodconverter.document.DocumentFamily; import org.artofsolving.jodconverter.document.DocumentFamily;
import org.artofsolving.jodconverter.document.DocumentFormat; import org.artofsolving.jodconverter.document.DocumentFormat;
import org.artofsolving.jodconverter.document.SimpleDocumentFormatRegistry; import org.artofsolving.jodconverter.document.SimpleDocumentFormatRegistry;
...@@ -42,7 +40,7 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry ...@@ -42,7 +40,7 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry
// available for exporting Spreadsheet and Presentation formats // available for exporting Spreadsheet and Presentation formats
html.setInputFamily(DocumentFamily.TEXT); html.setInputFamily(DocumentFamily.TEXT);
html.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "HTML (StarWriter)")); html.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "HTML (StarWriter)"));
Map<String,Object> htmlLoadAndStoreProperties = new LinkedHashMap<String,Object>(); Map<String,Object> htmlLoadAndStoreProperties = new LinkedHashMap<>();
htmlLoadAndStoreProperties.put("FilterName", "HTML (StarCalc)"); htmlLoadAndStoreProperties.put("FilterName", "HTML (StarCalc)");
htmlLoadAndStoreProperties.put("FilterOptions", "utf8"); htmlLoadAndStoreProperties.put("FilterOptions", "utf8");
html.setStoreProperties(DocumentFamily.SPREADSHEET, htmlLoadAndStoreProperties); html.setStoreProperties(DocumentFamily.SPREADSHEET, htmlLoadAndStoreProperties);
...@@ -79,7 +77,7 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry ...@@ -79,7 +77,7 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry
DocumentFormat txt = new DocumentFormat("Plain Text", "txt", "text/plain"); DocumentFormat txt = new DocumentFormat("Plain Text", "txt", "text/plain");
txt.setInputFamily(DocumentFamily.TEXT); txt.setInputFamily(DocumentFamily.TEXT);
Map<String,Object> txtLoadAndStoreProperties = new LinkedHashMap<String,Object>(); Map<String,Object> txtLoadAndStoreProperties = new LinkedHashMap<>();
txtLoadAndStoreProperties.put("FilterName", "Text (encoded)"); txtLoadAndStoreProperties.put("FilterName", "Text (encoded)");
txtLoadAndStoreProperties.put("FilterOptions", "utf8"); txtLoadAndStoreProperties.put("FilterOptions", "utf8");
txt.setLoadProperties(txtLoadAndStoreProperties); txt.setLoadProperties(txtLoadAndStoreProperties);
...@@ -111,7 +109,7 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry ...@@ -111,7 +109,7 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry
DocumentFormat csv = new DocumentFormat("Comma Separated Values", "csv", "text/csv"); DocumentFormat csv = new DocumentFormat("Comma Separated Values", "csv", "text/csv");
csv.setInputFamily(DocumentFamily.SPREADSHEET); csv.setInputFamily(DocumentFamily.SPREADSHEET);
Map<String,Object> csvLoadAndStoreProperties = new LinkedHashMap<String,Object>(); Map<String,Object> csvLoadAndStoreProperties = new LinkedHashMap<>();
csvLoadAndStoreProperties.put("FilterName", "Text - txt - csv (StarCalc)"); csvLoadAndStoreProperties.put("FilterName", "Text - txt - csv (StarCalc)");
csvLoadAndStoreProperties.put("FilterOptions", "44,34,0"); // Field Separator: ','; Text Delimiter: '"' csvLoadAndStoreProperties.put("FilterOptions", "44,34,0"); // Field Separator: ','; Text Delimiter: '"'
csv.setLoadProperties(csvLoadAndStoreProperties); csv.setLoadProperties(csvLoadAndStoreProperties);
...@@ -120,7 +118,7 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry ...@@ -120,7 +118,7 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry
DocumentFormat tsv = new DocumentFormat("Tab Separated Values", "tsv", "text/tab-separated-values"); DocumentFormat tsv = new DocumentFormat("Tab Separated Values", "tsv", "text/tab-separated-values");
tsv.setInputFamily(DocumentFamily.SPREADSHEET); tsv.setInputFamily(DocumentFamily.SPREADSHEET);
Map<String,Object> tsvLoadAndStoreProperties = new LinkedHashMap<String,Object>(); Map<String,Object> tsvLoadAndStoreProperties = new LinkedHashMap<>();
tsvLoadAndStoreProperties.put("FilterName", "Text - txt - csv (StarCalc)"); tsvLoadAndStoreProperties.put("FilterName", "Text - txt - csv (StarCalc)");
tsvLoadAndStoreProperties.put("FilterOptions", "9,34,0"); // Field Separator: '\t'; Text Delimiter: '"' tsvLoadAndStoreProperties.put("FilterOptions", "9,34,0"); // Field Separator: '\t'; Text Delimiter: '"'
tsv.setLoadProperties(tsvLoadAndStoreProperties); tsv.setLoadProperties(tsvLoadAndStoreProperties);
...@@ -156,56 +154,4 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry ...@@ -156,56 +154,4 @@ public class ControlDocumentFormatRegistry extends SimpleDocumentFormatRegistry
addFormat(svg); addFormat(svg);
} }
/**
* 创建默认的导出属性
* @return
*/
private PropertyValue[] getCommonPropertyValue() {
PropertyValue[] aFilterData = new PropertyValue[11];
// 不显示文档标题
aFilterData[0] = new PropertyValue();
aFilterData[0].Name = "DisplayPDFDocumentTitle";
aFilterData[0].Value= true;
// 导出文件编码方式
aFilterData[1] = new PropertyValue();
aFilterData[1].Name = "Encoding";
aFilterData[1].Value= "UTF-8";
// 隐藏工具条
aFilterData[2] = new PropertyValue();
aFilterData[2].Name = "HideViewerToolbar";
aFilterData[2].Value= false;
// 隐藏窗口控制条
aFilterData[3] = new PropertyValue();
aFilterData[3].Name = "HideViewerWindowControls";
aFilterData[3].Value= true;
// 全屏展示
aFilterData[4] = new PropertyValue();
aFilterData[4].Name = "OpenInFullScreenMode";
aFilterData[4].Value= false;
// 第一页左边展示
aFilterData[5] = new PropertyValue();
aFilterData[5].Name = "MathToMathType";
aFilterData[5].Value= true;
// 文档标题内容
aFilterData[6] = new PropertyValue();
aFilterData[6].Name = "Watermark";
aFilterData[6].Value= "KEKING.CN";
// 导出文件编码方式
aFilterData[7] = new PropertyValue();
aFilterData[7].Name = "CharacterSet";
aFilterData[7].Value= "UTF-8";
// 导出文件编码方式
aFilterData[8] = new PropertyValue();
aFilterData[8].Name = "Encoding";
aFilterData[8].Value= "UTF-8";
// 导出文件编码方式
aFilterData[9] = new PropertyValue();
aFilterData[9].Name = "CharSet";
aFilterData[9].Value= "UTF-8";
// 导出文件编码方式
aFilterData[10] = new PropertyValue();
aFilterData[10].Name = "charset";
aFilterData[10].Value= "UTF-8";
return aFilterData;
}
} }
...@@ -14,17 +14,14 @@ public class FileAttribute { ...@@ -14,17 +14,14 @@ public class FileAttribute {
private String url; private String url;
private String decodedUrl;
public FileAttribute() { public FileAttribute() {
} }
public FileAttribute(FileType type, String suffix, String name, String url, String decodedUrl) { public FileAttribute(FileType type, String suffix, String name, String url) {
this.type = type; this.type = type;
this.suffix = suffix; this.suffix = suffix;
this.name = name; this.name = name;
this.url = url; this.url = url;
this.decodedUrl = decodedUrl;
} }
public FileType getType() { public FileType getType() {
...@@ -58,12 +55,4 @@ public class FileAttribute { ...@@ -58,12 +55,4 @@ public class FileAttribute {
public void setUrl(String url) { public void setUrl(String url) {
this.url = url; this.url = url;
} }
public String getDecodedUrl() {
return decodedUrl;
}
public void setDecodedUrl(String decodedUrl) {
this.decodedUrl = decodedUrl;
}
} }
...@@ -14,7 +14,8 @@ public enum FileType { ...@@ -14,7 +14,8 @@ public enum FileType {
media("mediaFilePreviewImpl"), media("mediaFilePreviewImpl"),
cad("cadFilePreviewImpl"); cad("cadFilePreviewImpl");
private String instanceName; private final String instanceName;
FileType(String instanceName){ FileType(String instanceName){
this.instanceName=instanceName; this.instanceName=instanceName;
} }
...@@ -23,7 +24,4 @@ public enum FileType { ...@@ -23,7 +24,4 @@ public enum FileType {
return instanceName; return instanceName;
} }
public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}
} }
...@@ -6,7 +6,6 @@ import cn.keking.service.cache.CacheService; ...@@ -6,7 +6,6 @@ import cn.keking.service.cache.CacheService;
import cn.keking.utils.FileUtils; import cn.keking.utils.FileUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.ExtendedModelMap;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
...@@ -18,36 +17,44 @@ import java.util.concurrent.Executors; ...@@ -18,36 +17,44 @@ import java.util.concurrent.Executors;
* Content :消费队列中的转换文件 * Content :消费队列中的转换文件
*/ */
@Service @Service
public class FileConverQueueTask { public class FileConvertQueueTask {
Logger logger= LoggerFactory.getLogger(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass());
public static final String queueTaskName="FileConverQueueTask";
@Autowired private final FilePreviewFactory previewFactory;
FilePreviewFactory previewFactory;
@Autowired private final CacheService cacheService;
CacheService cacheService;
@Autowired private final FileUtils fileUtils;
FileUtils fileUtils;
public FileConvertQueueTask(FilePreviewFactory previewFactory,
CacheService cacheService,
FileUtils fileUtils) {
this.previewFactory = previewFactory;
this.cacheService = cacheService;
this.fileUtils=fileUtils;
}
@PostConstruct @PostConstruct
public void startTask(){ public void startTask(){
ExecutorService executorService = Executors.newFixedThreadPool(3); ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new ConverTask(previewFactory, cacheService, fileUtils)); executorService.submit(new ConvertTask(previewFactory, cacheService, fileUtils));
logger.info("队列处理文件转换任务启动完成 "); logger.info("队列处理文件转换任务启动完成 ");
} }
class ConverTask implements Runnable{ class ConvertTask implements Runnable {
private final Logger logger = LoggerFactory.getLogger(ConvertTask.class);
FilePreviewFactory previewFactory; private final FilePreviewFactory previewFactory;
CacheService cacheService; private final CacheService cacheService;
FileUtils fileUtils; private final FileUtils fileUtils;
public ConverTask(FilePreviewFactory previewFactory, CacheService cacheService, FileUtils fileUtils) { public ConvertTask(FilePreviewFactory previewFactory,
CacheService cacheService,
FileUtils fileUtils) {
this.previewFactory = previewFactory; this.previewFactory = previewFactory;
this.cacheService = cacheService; this.cacheService = cacheService;
this.fileUtils=fileUtils; this.fileUtils=fileUtils;
......
package cn.keking.service; package cn.keking.service;
import cn.keking.model.FileAttribute; import cn.keking.model.FileAttribute;
import cn.keking.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -15,11 +13,11 @@ import java.util.Map; ...@@ -15,11 +13,11 @@ import java.util.Map;
@Service @Service
public class FilePreviewFactory { public class FilePreviewFactory {
@Autowired private final ApplicationContext context;
FileUtils fileUtils;
@Autowired public FilePreviewFactory(ApplicationContext context) {
ApplicationContext context; this.context = context;
}
public FilePreview get(FileAttribute fileAttribute) { public FilePreview get(FileAttribute fileAttribute) {
Map<String, FilePreview> filePreviewMap = context.getBeansOfType(FilePreview.class); Map<String, FilePreview> filePreviewMap = context.getBeansOfType(FilePreview.class);
......
...@@ -4,19 +4,19 @@ import java.util.List; ...@@ -4,19 +4,19 @@ import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* @auther: chenjh * @author: chenjh
* @time: 2019/4/2 16:45 * @since: 2019/4/2 16:45
* @description
*/ */
public interface CacheService { 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";
String FILE_PREVIEW_PDF_KEY = "converted-preview-pdf-file";
String FILE_PREVIEW_IMGS_KEY = "converted-preview-imgs-file";//压缩包内图片文件集合
String FILE_PREVIEW_PDF_IMGS_KEY = "converted-preview-pdfimgs-file";
String TASK_QUEUE_NAME = "convert-task";
final Integer DEFAULT_PDF_CAPACITY = 500000; Integer DEFAULT_PDF_CAPACITY = 500000;
final Integer DEFAULT_IMG_CAPACITY = 500000; Integer DEFAULT_IMG_CAPACITY = 500000;
final Integer DEFAULT_PDFIMG_CAPACITY = 500000; Integer DEFAULT_PDFIMG_CAPACITY = 500000;
void initPDFCachePool(Integer capacity); void initPDFCachePool(Integer capacity);
void initIMGCachePool(Integer capacity); void initIMGCachePool(Integer capacity);
...@@ -29,12 +29,8 @@ public interface CacheService { ...@@ -29,12 +29,8 @@ public interface CacheService {
List<String> getImgCache(String key); List<String> getImgCache(String key);
Integer getPdfImageCache(String key); Integer getPdfImageCache(String key);
void putPdfImageCache(String pdfFilePath, int num); void putPdfImageCache(String pdfFilePath, int num);
void cleanCache(); void cleanCache();
void addQueueTask(String url); void addQueueTask(String url);
String takeQueueTask() throws InterruptedException; String takeQueueTask() throws InterruptedException;
} }
...@@ -3,7 +3,6 @@ package cn.keking.service.cache.impl; ...@@ -3,7 +3,6 @@ package cn.keking.service.cache.impl;
import cn.keking.service.cache.CacheService; import cn.keking.service.cache.CacheService;
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.googlecode.concurrentlinkedhashmap.Weighers; import com.googlecode.concurrentlinkedhashmap.Weighers;
import org.rocksdb.RocksDB;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -29,7 +28,7 @@ public class CacheServiceJDKImpl implements CacheService { ...@@ -29,7 +28,7 @@ public class CacheServiceJDKImpl implements CacheService {
private static final int QUEUE_SIZE = 500000; private static final int QUEUE_SIZE = 500000;
private BlockingQueue blockingQueue = new ArrayBlockingQueue(QUEUE_SIZE); private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(QUEUE_SIZE);
@Override @Override
public void initPDFCachePool(Integer capacity) { public void initPDFCachePool(Integer capacity) {
...@@ -130,6 +129,6 @@ public class CacheServiceJDKImpl implements CacheService { ...@@ -130,6 +129,6 @@ public class CacheServiceJDKImpl implements CacheService {
@Override @Override
public String takeQueueTask() throws InterruptedException { public String takeQueueTask() throws InterruptedException {
return String.valueOf(blockingQueue.take()); return blockingQueue.take();
} }
} }
package cn.keking.service.cache.impl; package cn.keking.service.cache.impl;
import cn.keking.service.FileConverQueueTask;
import cn.keking.service.cache.CacheService; import cn.keking.service.cache.CacheService;
import org.redisson.Redisson; import org.redisson.Redisson;
import org.redisson.api.RBlockingQueue; import org.redisson.api.RBlockingQueue;
import org.redisson.api.RMapCache; import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.redisson.config.Config; import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -23,16 +21,12 @@ import java.util.Map; ...@@ -23,16 +21,12 @@ import java.util.Map;
@Service @Service
public class CacheServiceRedisImpl implements CacheService { public class CacheServiceRedisImpl implements CacheService {
private Config config; private final RedissonClient redissonClient;
@Autowired
public CacheServiceRedisImpl(Config config) { public CacheServiceRedisImpl(Config config) {
this.config = config;
this.redissonClient = Redisson.create(config); this.redissonClient = Redisson.create(config);
} }
private RedissonClient redissonClient;
@Override @Override
public void initPDFCachePool(Integer capacity) { public void initPDFCachePool(Integer capacity) {
...@@ -50,47 +44,47 @@ public class CacheServiceRedisImpl implements CacheService { ...@@ -50,47 +44,47 @@ public class CacheServiceRedisImpl implements CacheService {
@Override @Override
public void putPDFCache(String key, String value) { public void putPDFCache(String key, String value) {
RMapCache<String, String> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY); RMapCache<String, String> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_KEY);
convertedList.fastPut(key, value); convertedList.fastPut(key, value);
} }
@Override @Override
public void putImgCache(String key, List<String> value) { public void putImgCache(String key, List<String> value) {
RMapCache<String, List<String>> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_IMGS_KEY); RMapCache<String, List<String>> convertedList = redissonClient.getMapCache(FILE_PREVIEW_IMGS_KEY);
convertedList.fastPut(key, value); convertedList.fastPut(key, value);
} }
@Override @Override
public Map<String, String> getPDFCache() { public Map<String, String> getPDFCache() {
return redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY); return redissonClient.getMapCache(FILE_PREVIEW_PDF_KEY);
} }
@Override @Override
public String getPDFCache(String key) { public String getPDFCache(String key) {
RMapCache<String, String> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY); RMapCache<String, String> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_KEY);
return convertedList.get(key); return convertedList.get(key);
} }
@Override @Override
public Map<String, List<String>> getImgCache() { public Map<String, List<String>> getImgCache() {
return redissonClient.getMapCache(REDIS_FILE_PREVIEW_IMGS_KEY); return redissonClient.getMapCache(FILE_PREVIEW_IMGS_KEY);
} }
@Override @Override
public List<String> getImgCache(String key) { public List<String> getImgCache(String key) {
RMapCache<String, List<String>> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_IMGS_KEY); RMapCache<String, List<String>> convertedList = redissonClient.getMapCache(FILE_PREVIEW_IMGS_KEY);
return convertedList.get(key); return convertedList.get(key);
} }
@Override @Override
public Integer getPdfImageCache(String key) { public Integer getPdfImageCache(String key) {
RMapCache<String, Integer> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_IMGS_KEY); RMapCache<String, Integer> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_IMGS_KEY);
return convertedList.get(key); return convertedList.get(key);
} }
@Override @Override
public void putPdfImageCache(String pdfFilePath, int num) { public void putPdfImageCache(String pdfFilePath, int num) {
RMapCache<String, Integer> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_IMGS_KEY); RMapCache<String, Integer> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_IMGS_KEY);
convertedList.fastPut(pdfFilePath, num); convertedList.fastPut(pdfFilePath, num);
} }
...@@ -103,28 +97,28 @@ public class CacheServiceRedisImpl implements CacheService { ...@@ -103,28 +97,28 @@ public class CacheServiceRedisImpl implements CacheService {
@Override @Override
public void addQueueTask(String url) { public void addQueueTask(String url) {
RBlockingQueue<String> queue = redissonClient.getBlockingQueue(FileConverQueueTask.queueTaskName); RBlockingQueue<String> queue = redissonClient.getBlockingQueue(TASK_QUEUE_NAME);
queue.addAsync(url); queue.addAsync(url);
} }
@Override @Override
public String takeQueueTask() throws InterruptedException { public String takeQueueTask() throws InterruptedException {
RBlockingQueue<String> queue = redissonClient.getBlockingQueue(FileConverQueueTask.queueTaskName); RBlockingQueue<String> queue = redissonClient.getBlockingQueue(TASK_QUEUE_NAME);
return queue.take(); return queue.take();
} }
private void cleanPdfCache() { private void cleanPdfCache() {
RMapCache<String, String> pdfCache = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY); RMapCache<String, String> pdfCache = redissonClient.getMapCache(FILE_PREVIEW_PDF_KEY);
pdfCache.clear(); pdfCache.clear();
} }
private void cleanImgCache() { private void cleanImgCache() {
RMapCache<String, List<String>> imgCache = redissonClient.getMapCache(REDIS_FILE_PREVIEW_IMGS_KEY); RMapCache<String, List<String>> imgCache = redissonClient.getMapCache(FILE_PREVIEW_IMGS_KEY);
imgCache.clear(); imgCache.clear();
} }
private void cleanPdfImgCache() { private void cleanPdfImgCache() {
RMapCache<String, Integer> pdfImg = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_IMGS_KEY); RMapCache<String, Integer> pdfImg = redissonClient.getMapCache(FILE_PREVIEW_PDF_IMGS_KEY);
pdfImg.clear(); pdfImg.clear();
} }
} }
...@@ -36,24 +36,24 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -36,24 +36,24 @@ public class CacheServiceRocksDBImpl implements CacheService {
private static final Logger LOGGER = LoggerFactory.getLogger(CacheServiceRocksDBImpl.class); private static final Logger LOGGER = LoggerFactory.getLogger(CacheServiceRocksDBImpl.class);
private BlockingQueue blockingQueue = new ArrayBlockingQueue(QUEUE_SIZE); private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(QUEUE_SIZE);
private RocksDB db; private RocksDB db;
{ {
try { try {
db = RocksDB.open(DB_PATH); db = RocksDB.open(DB_PATH);
if (db.get(REDIS_FILE_PREVIEW_PDF_KEY.getBytes()) == null) { if (db.get(FILE_PREVIEW_PDF_KEY.getBytes()) == null) {
Map<String, String> initPDFCache = new HashMap<>(); Map<String, String> initPDFCache = new HashMap<>();
db.put(REDIS_FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(initPDFCache)); db.put(FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(initPDFCache));
} }
if (db.get(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes()) == null) { if (db.get(FILE_PREVIEW_IMGS_KEY.getBytes()) == null) {
Map<String, List<String>> initIMGCache = new HashMap<>(); Map<String, List<String>> initIMGCache = new HashMap<>();
db.put(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(initIMGCache)); db.put(FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(initIMGCache));
} }
if (db.get(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes()) == null) { if (db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes()) == null) {
Map<String, Integer> initPDFIMGCache = new HashMap<>(); Map<String, Integer> initPDFIMGCache = new HashMap<>();
db.put(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache)); db.put(FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache));
} }
} catch (RocksDBException | IOException e) { } catch (RocksDBException | IOException e) {
LOGGER.error("Uable to init RocksDB" + e); LOGGER.error("Uable to init RocksDB" + e);
...@@ -81,7 +81,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -81,7 +81,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
try { try {
Map<String, String> pdfCacheItem = getPDFCache(); Map<String, String> pdfCacheItem = getPDFCache();
pdfCacheItem.put(key, value); pdfCacheItem.put(key, value);
db.put(REDIS_FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(pdfCacheItem)); db.put(FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(pdfCacheItem));
} catch (RocksDBException | IOException e) { } catch (RocksDBException | IOException e) {
LOGGER.error("Put into RocksDB Exception" + e); LOGGER.error("Put into RocksDB Exception" + e);
} }
...@@ -92,7 +92,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -92,7 +92,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
try { try {
Map<String, List<String>> imgCacheItem = getImgCache(); Map<String, List<String>> imgCacheItem = getImgCache();
imgCacheItem.put(key, value); imgCacheItem.put(key, value);
db.put(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(imgCacheItem)); db.put(FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(imgCacheItem));
} catch (RocksDBException | IOException e) { } catch (RocksDBException | IOException e) {
LOGGER.error("Put into RocksDB Exception" + e); LOGGER.error("Put into RocksDB Exception" + e);
} }
...@@ -102,7 +102,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -102,7 +102,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
public Map<String, String> getPDFCache() { public Map<String, String> getPDFCache() {
Map<String, String> result = new HashMap<>(); Map<String, String> result = new HashMap<>();
try{ try{
result = (Map<String, String>) toObject(db.get(REDIS_FILE_PREVIEW_PDF_KEY.getBytes())); result = (Map<String, String>) toObject(db.get(FILE_PREVIEW_PDF_KEY.getBytes()));
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
} }
...@@ -113,7 +113,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -113,7 +113,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
public String getPDFCache(String key) { public String getPDFCache(String key) {
String result = ""; String result = "";
try{ try{
Map<String, String> map = (Map<String, String>) toObject(db.get(REDIS_FILE_PREVIEW_PDF_KEY.getBytes())); Map<String, String> map = (Map<String, String>) toObject(db.get(FILE_PREVIEW_PDF_KEY.getBytes()));
result = map.get(key); result = map.get(key);
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
...@@ -125,7 +125,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -125,7 +125,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
public Map<String, List<String>> getImgCache() { public Map<String, List<String>> getImgCache() {
Map<String, List<String>> result = new HashMap<>(); Map<String, List<String>> result = new HashMap<>();
try{ try{
result = (Map<String, List<String>>) toObject(db.get(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes())); result = (Map<String, List<String>>) toObject(db.get(FILE_PREVIEW_IMGS_KEY.getBytes()));
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
} }
...@@ -137,7 +137,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -137,7 +137,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
Map<String, List<String>> map; Map<String, List<String>> map;
try{ try{
map = (Map<String, List<String>>) toObject(db.get(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes())); map = (Map<String, List<String>>) toObject(db.get(FILE_PREVIEW_IMGS_KEY.getBytes()));
result = map.get(key); result = map.get(key);
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
...@@ -148,7 +148,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -148,7 +148,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
public Map<String, Integer> getPdfImageCaches() { public Map<String, Integer> getPdfImageCaches() {
Map<String, Integer> map = new HashMap<>(); Map<String, Integer> map = new HashMap<>();
try{ try{
map = (Map<String, Integer>) toObject(db.get(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes())); map = (Map<String, Integer>) toObject(db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
} }
...@@ -160,7 +160,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -160,7 +160,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
Integer result = 0; Integer result = 0;
Map<String, Integer> map; Map<String, Integer> map;
try{ try{
map = (Map<String, Integer>) toObject(db.get(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes())); map = (Map<String, Integer>) toObject(db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
result = map.get(key); result = map.get(key);
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
...@@ -173,7 +173,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -173,7 +173,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
try { try {
Map<String, Integer> pdfImageCacheItem = getPdfImageCaches(); Map<String, Integer> pdfImageCacheItem = getPdfImageCaches();
pdfImageCacheItem.put(pdfFilePath, num); pdfImageCacheItem.put(pdfFilePath, num);
db.put(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(pdfImageCacheItem)); db.put(FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(pdfImageCacheItem));
} catch (RocksDBException | IOException e) { } catch (RocksDBException | IOException e) {
LOGGER.error("Put into RocksDB Exception" + e); LOGGER.error("Put into RocksDB Exception" + e);
} }
...@@ -197,11 +197,11 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -197,11 +197,11 @@ public class CacheServiceRocksDBImpl implements CacheService {
@Override @Override
public String takeQueueTask() throws InterruptedException { public String takeQueueTask() throws InterruptedException {
return String.valueOf(blockingQueue.take()); return blockingQueue.take();
} }
private byte[] toByteArray (Object obj) throws IOException { private byte[] toByteArray (Object obj) throws IOException {
byte[] bytes = null; byte[] bytes;
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos); ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj); oos.writeObject(obj);
...@@ -213,7 +213,7 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -213,7 +213,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
} }
private Object toObject (byte[] bytes) throws IOException, ClassNotFoundException { private Object toObject (byte[] bytes) throws IOException, ClassNotFoundException {
Object obj = null; Object obj;
ByteArrayInputStream bis = new ByteArrayInputStream (bytes); ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
ObjectInputStream ois = new ObjectInputStream (bis); ObjectInputStream ois = new ObjectInputStream (bis);
obj = ois.readObject(); obj = ois.readObject();
...@@ -224,16 +224,16 @@ public class CacheServiceRocksDBImpl implements CacheService { ...@@ -224,16 +224,16 @@ public class CacheServiceRocksDBImpl implements CacheService {
private void cleanPdfCache() throws IOException, RocksDBException { private void cleanPdfCache() throws IOException, RocksDBException {
Map<String, String> initPDFCache = new HashMap<>(); Map<String, String> initPDFCache = new HashMap<>();
db.put(REDIS_FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(initPDFCache)); db.put(FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(initPDFCache));
} }
private void cleanImgCache() throws IOException, RocksDBException { private void cleanImgCache() throws IOException, RocksDBException {
Map<String, List<String>> initIMGCache = new HashMap<>(); Map<String, List<String>> initIMGCache = new HashMap<>();
db.put(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(initIMGCache)); db.put(FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(initIMGCache));
} }
private void cleanPdfImgCache() throws IOException, RocksDBException { private void cleanPdfImgCache() throws IOException, RocksDBException {
Map<String, Integer> initPDFIMGCache = new HashMap<>(); Map<String, Integer> initPDFIMGCache = new HashMap<>();
db.put(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache)); db.put(FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache));
} }
} }
...@@ -14,7 +14,7 @@ import org.springframework.stereotype.Service; ...@@ -14,7 +14,7 @@ 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;
import java.util.List; import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
/** /**
* @author chenjh * @author chenjh
...@@ -52,7 +52,7 @@ public class CadFilePreviewImpl implements FilePreview { ...@@ -52,7 +52,7 @@ public class CadFilePreviewImpl implements FilePreview {
String outFilePath = fileDir + pdfName; String outFilePath = fileDir + pdfName;
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换 // 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) { if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath = fileDir + fileName; String filePath;
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) { if (0 != response.getCode()) {
model.addAttribute("fileType", suffix); model.addAttribute("fileType", suffix);
...@@ -74,19 +74,7 @@ public class CadFilePreviewImpl implements FilePreview { ...@@ -74,19 +74,7 @@ public class CadFilePreviewImpl implements FilePreview {
} }
} }
if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType))) { if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType))) {
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl); return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, pdfUtils, OFFICE_PREVIEW_TYPE_IMAGE);
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));
if (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
return "officePicture";
} else {
return "picture";
}
} }
model.addAttribute("pdfUrl", pdfName); model.addAttribute("pdfUrl", pdfName);
return "pdf"; return "pdf";
......
...@@ -74,6 +74,13 @@ public class OfficeFilePreviewImpl implements FilePreview { ...@@ -74,6 +74,13 @@ public class OfficeFilePreviewImpl implements FilePreview {
} }
} }
if (!isHtml && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType))) { if (!isHtml && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType))) {
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, pdfUtils, OFFICE_PREVIEW_TYPE_IMAGE);
}
model.addAttribute("pdfUrl", pdfName);
return isHtml ? "html" : "pdf";
}
static String getPreviewType(Model model, FileAttribute fileAttribute, String officePreviewType, String baseUrl, String pdfName, String outFilePath, PdfUtils pdfUtils, String officePreviewTypeImage) {
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl); List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl);
if (imageUrls == null || imageUrls.size() < 1) { if (imageUrls == null || imageUrls.size() < 1) {
model.addAttribute("msg", "office转图片异常,请联系管理员"); model.addAttribute("msg", "office转图片异常,请联系管理员");
...@@ -82,13 +89,10 @@ public class OfficeFilePreviewImpl implements FilePreview { ...@@ -82,13 +89,10 @@ public class OfficeFilePreviewImpl implements FilePreview {
} }
model.addAttribute("imgurls", imageUrls); model.addAttribute("imgurls", imageUrls);
model.addAttribute("currentUrl", imageUrls.get(0)); model.addAttribute("currentUrl", imageUrls.get(0));
if (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) { if (officePreviewTypeImage.equals(officePreviewType)) {
return "officePicture"; return "officePicture";
} else { } else {
return "picture"; return "picture";
} }
} }
model.addAttribute("pdfUrl", pdfName);
return isHtml ? "html" : "pdf";
}
} }
package cn.keking.service.impl; package cn.keking.service.impl;
import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute; 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;
...@@ -30,7 +29,7 @@ public class PictureFilePreviewImpl implements FilePreview { ...@@ -30,7 +29,7 @@ public class PictureFilePreviewImpl implements FilePreview {
@Override @Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
String fileKey = (String) RequestContextHolder.currentRequestAttributes().getAttribute("fileKey",0); String fileKey = (String) RequestContextHolder.currentRequestAttributes().getAttribute("fileKey",0);
List imgUrls = Lists.newArrayList(url); List<String> imgUrls = Lists.newArrayList(url);
try { try {
imgUrls.clear(); imgUrls.clear();
imgUrls.addAll(fileUtils.getImgCache(fileKey)); imgUrls.addAll(fileUtils.getImgCache(fileKey));
......
...@@ -3,9 +3,7 @@ package cn.keking.service.impl; ...@@ -3,9 +3,7 @@ package cn.keking.service.impl;
import cn.keking.model.FileAttribute; 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.FileUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.utils.SimTextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
...@@ -18,19 +16,18 @@ import java.nio.file.Files; ...@@ -18,19 +16,18 @@ import java.nio.file.Files;
* Content :处理文本文件 * Content :处理文本文件
*/ */
@Service @Service
public class SimTextFilePreviewImpl implements FilePreview{ public class SimTextFilePreviewImpl implements FilePreview {
@Autowired private final DownloadUtils downloadUtils;
SimTextUtil simTextUtil;
@Autowired public SimTextFilePreviewImpl(DownloadUtils downloadUtils) {
FileUtils fileUtils; this.downloadUtils = downloadUtils;
}
@Override @Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute){ public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute){
String decodedUrl=fileAttribute.getDecodedUrl(); String fileName = fileAttribute.getName();
String fileName=fileAttribute.getName(); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
ReturnResponse<String> response = simTextUtil.readSimText(decodedUrl, fileName);
if (0 != response.getCode()) { if (0 != response.getCode()) {
model.addAttribute("msg", response.getMsg()); model.addAttribute("msg", response.getMsg());
model.addAttribute("fileType",fileAttribute.getSuffix()); model.addAttribute("fileType",fileAttribute.getSuffix());
......
...@@ -36,7 +36,7 @@ public class CadToPdf { ...@@ -36,7 +36,7 @@ public class CadToPdf {
PdfOptions pdfOptions = new PdfOptions(); PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions); pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
File outputFile = new File(outputFilePath); File outputFile = new File(outputFilePath);
OutputStream stream = null; OutputStream stream;
try { try {
stream = new FileOutputStream(outputFile); stream = new FileOutputStream(outputFile);
cadImage.save(stream, pdfOptions); cadImage.save(stream, pdfOptions);
......
...@@ -9,15 +9,15 @@ import org.artofsolving.jodconverter.office.OfficeManager; ...@@ -9,15 +9,15 @@ import org.artofsolving.jodconverter.office.OfficeManager;
import org.artofsolving.jodconverter.office.OfficeUtils; import org.artofsolving.jodconverter.office.OfficeUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
...@@ -37,11 +37,12 @@ public class ConverterUtils { ...@@ -37,11 +37,12 @@ public class ConverterUtils {
@PostConstruct @PostConstruct
public void initOfficeManager() { public void initOfficeManager() {
String officeHome = OfficeUtils.getDefaultOfficeHome().getAbsolutePath(); File officeHome;
officeHome = OfficeUtils.getDefaultOfficeHome();
if (officeHome == null) { if (officeHome == null) {
throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误"); throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误");
} }
boolean killOffice = killProcess("soffice.bin"); boolean killOffice = killProcess();
if (killOffice) { if (killOffice) {
logger.warn("检测到有正在运行的office进程,已自动结束该进程"); logger.warn("检测到有正在运行的office进程,已自动结束该进程");
} }
...@@ -72,11 +73,11 @@ public class ConverterUtils { ...@@ -72,11 +73,11 @@ public class ConverterUtils {
loadProperties.put("Hidden", true); loadProperties.put("Hidden", true);
loadProperties.put("ReadOnly", true); loadProperties.put("ReadOnly", true);
loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE); loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE);
loadProperties.put("CharacterSet", Charset.forName("UTF-8").name()); loadProperties.put("CharacterSet", StandardCharsets.UTF_8.name());
return loadProperties; return loadProperties;
} }
private boolean killProcess(String processName) { private boolean killProcess() {
boolean flag = false; boolean flag = false;
Properties props = System.getProperties(); Properties props = System.getProperties();
try { try {
...@@ -84,32 +85,28 @@ public class ConverterUtils { ...@@ -84,32 +85,28 @@ public class ConverterUtils {
Process p = Runtime.getRuntime().exec("cmd /c tasklist "); Process p = Runtime.getRuntime().exec("cmd /c tasklist ");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream os = p.getInputStream(); InputStream os = p.getInputStream();
byte b[] = new byte[256]; byte[] b = new byte[256];
while (os.read(b) > 0) { while (os.read(b) > 0) {
baos.write(b); baos.write(b);
} }
String s = baos.toString(); String s = baos.toString();
if (s.indexOf(processName) >= 0) { if (s.contains("soffice.bin")) {
Runtime.getRuntime().exec("taskkill /im " + processName + " /f"); Runtime.getRuntime().exec("taskkill /im " + "soffice.bin" + " /f");
flag = true; flag = true;
} else {
flag = false;
} }
} else { } else {
Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","ps -ef | grep " + processName}); Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","ps -ef | grep " + "soffice.bin"});
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream os = p.getInputStream(); InputStream os = p.getInputStream();
byte b[] = new byte[256]; byte[] b = new byte[256];
while (os.read(b) > 0) { while (os.read(b) > 0) {
baos.write(b); baos.write(b);
} }
String s = baos.toString(); String s = baos.toString();
if (StringUtils.ordinalIndexOf(s, processName, 3) > 0) { if (StringUtils.ordinalIndexOf(s, "soffice.bin", 3) > 0) {
String[] cmd ={"sh","-c","kill -15 `ps -ef|grep " + processName + "|awk 'NR==1{print $2}'`"}; String[] cmd ={"sh","-c","kill -15 `ps -ef|grep " + "soffice.bin" + "|awk 'NR==1{print $2}'`"};
Runtime.getRuntime().exec(cmd); Runtime.getRuntime().exec(cmd);
flag = true; flag = true;
} else {
flag = false;
} }
} }
} catch (IOException e) { } catch (IOException e) {
......
...@@ -4,6 +4,7 @@ import org.slf4j.Logger; ...@@ -4,6 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.util.Objects;
public class DeleteFileUtil { public class DeleteFileUtil {
...@@ -55,21 +56,21 @@ public class DeleteFileUtil { ...@@ -55,21 +56,21 @@ public class DeleteFileUtil {
boolean flag = true; boolean flag = true;
// 删除文件夹中的所有文件包括子目录 // 删除文件夹中的所有文件包括子目录
File[] files = dirFile.listFiles(); File[] files = dirFile.listFiles();
for (int i = 0; i < 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 = DeleteFileUtil.deleteFile(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 = DeleteFileUtil.deleteDirectory(files[i] if (!flag) {
.getAbsolutePath());
if (!flag)
break; break;
} }
} }
}
dirFile.delete(); dirFile.delete();
if (!flag) { if (!flag) {
LOGGER.info("删除目录失败!"); LOGGER.info("删除目录失败!");
......
...@@ -3,13 +3,15 @@ package cn.keking.utils; ...@@ -3,13 +3,15 @@ package cn.keking.utils;
import cn.keking.config.ConfigConstants; import cn.keking.config.ConfigConstants;
import cn.keking.hutool.URLUtil; import cn.keking.hutool.URLUtil;
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 org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.UUID; import java.util.UUID;
/** /**
...@@ -18,32 +20,29 @@ import java.util.UUID; ...@@ -18,32 +20,29 @@ import java.util.UUID;
@Component @Component
public class DownloadUtils { public class DownloadUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(DownloadUtils.class); private final Logger logger = LoggerFactory.getLogger(DownloadUtils.class);
private final String fileDir = ConfigConstants.getFileDir();
private String fileDir = ConfigConstants.getFileDir(); private final FileUtils fileUtils;
@Autowired public DownloadUtils(FileUtils fileUtils) {
private FileUtils fileUtils; this.fileUtils = fileUtils;
}
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";
/** /**
* @param fileAttribute * @param fileAttribute fileAttribute
* @return * @param fileName 文件名
* @return 本地文件绝对路径
*/ */
public ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) { public ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) {
String urlAddress = fileAttribute.getDecodedUrl(); String urlStr = fileAttribute.getUrl();
String type = fileAttribute.getSuffix(); String type = fileAttribute.getSuffix();
ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", ""); ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
URL url = null;
try {
urlAddress = URLUtil.normalize(urlAddress, true);
url = new URL(urlAddress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
if (null == fileName) { if (null == fileName) {
fileName = uuid+ "."+type; fileName = uuid+ "."+type;
...@@ -56,35 +55,28 @@ public class DownloadUtils { ...@@ -56,35 +55,28 @@ public class DownloadUtils {
dirFile.mkdirs(); dirFile.mkdirs();
} }
try { try {
if ("ftp".equals(url.getProtocol())) { URL url = new URL(urlStr);
OutputStream os = new FileOutputStream(new File(realPath));
if (url.getProtocol() != null && url.getProtocol().toLowerCase().startsWith("http")) {
saveToOutputStreamFormUrl(urlStr, os);
} else if (url.getProtocol() != null && "ftp".equals(url.getProtocol().toLowerCase())) {
String ftpUsername = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); String ftpUsername = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD); String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING); String ftpControlEncoding = fileUtils.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 {
URLConnection connection = url.openConnection(); response.setCode(1);
InputStream in = connection.getInputStream(); response.setContent(null);
response.setMsg("url不能识别url" + urlStr);
FileOutputStream os = new FileOutputStream(realPath);
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = in.read(buffer)) > 0) {
os.write(buffer, 0, read);
}
os.close();
in.close();
} }
response.setContent(realPath); response.setContent(realPath);
// 同样针对类txt文件,如果成功msg包含的是转换后的文件名
response.setMsg(fileName); response.setMsg(fileName);
if(FileType.simText.equals(fileAttribute.getType())){
// txt转换文件编码为utf8
if("txt".equals(type)){
convertTextPlainFileCharsetToUtf8(realPath); convertTextPlainFileCharsetToUtf8(realPath);
} }
return response; return response;
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("文件下载失败,url:{}", urlAddress, e); logger.error("文件下载失败,url:{}", urlStr, e);
response.setCode(1); response.setCode(1);
response.setContent(null); response.setContent(null);
if (e instanceof FileNotFoundException) { if (e instanceof FileNotFoundException) {
...@@ -96,6 +88,47 @@ public class DownloadUtils { ...@@ -96,6 +88,47 @@ public class DownloadUtils {
} }
} }
public boolean saveToOutputStreamFormUrl(String urlStr, OutputStream os) throws IOException {
InputStream is = getInputStreamFromUrl(urlStr);
if (is != null) {
copyStream(is, os);
} else {
urlStr = URLUtil.normalize(urlStr, true);
is = getInputStreamFromUrl(urlStr);
if (is != null) {
copyStream(is, os);
} else {
os.close();
return false;
}
}
return true;
}
private InputStream getInputStreamFromUrl(String urlStr) {
try {
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
if (connection instanceof HttpURLConnection) {
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
}
return connection.getInputStream();
} catch (IOException e) {
logger.warn("连接url异常:url:{}", urlStr);
return null;
}
}
private void copyStream(InputStream is, OutputStream os) throws IOException {
byte[] bs = new byte[1024];
int len;
while (-1 != (len = is.read(bs))) {
os.write(bs, 0, len);
}
is.close();
os.close();
}
/** /**
* 转换文本文件编码为utf8 * 转换文本文件编码为utf8
* 探测源文件编码,探测到编码切不为utf8则进行转码 * 探测源文件编码,探测到编码切不为utf8则进行转码
...@@ -117,7 +150,7 @@ public class DownloadUtils { ...@@ -117,7 +150,7 @@ public class DownloadUtils {
if(encoding != null && !"UTF-8".equals(encoding)){ 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),"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;
......
...@@ -5,14 +5,12 @@ import cn.keking.model.FileAttribute; ...@@ -5,14 +5,12 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.FileType; import cn.keking.model.FileType;
import cn.keking.service.cache.CacheService; import cn.keking.service.cache.CacheService;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.io.*; import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -28,33 +26,31 @@ public class FileUtils { ...@@ -28,33 +26,31 @@ public class FileUtils {
public static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding"); public static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
Logger log= LoggerFactory.getLogger(getClass()); private final CacheService cacheService;
@Autowired public FileUtils(CacheService cacheService) {
private CacheService cacheService; this.cacheService = cacheService;
}
String fileDir = ConfigConstants.getFileDir(); String fileDir = ConfigConstants.getFileDir();
/** /**
* 已转换过的文件集合(缓存) * @return 已转换过的文件集合(缓存)
* @return
*/ */
public Map<String, String> listConvertedFiles() { public Map<String, String> listConvertedFiles() {
return cacheService.getPDFCache(); return cacheService.getPDFCache();
} }
/** /**
* 已转换过的文件,根据文件名获取 * @return 已转换过的文件,根据文件名获取
* @return
*/ */
public String getConvertedFile(String key) { public String getConvertedFile(String key) {
return cacheService.getPDFCache(key); return cacheService.getPDFCache(key);
} }
/** /**
* 已将pdf转换成图片的图片本地路径
* @param key pdf本地路径 * @param key pdf本地路径
* @return * @return 已将pdf转换成图片的图片本地相对路径
*/ */
public Integer getConvertedPdfImage(String key) { public Integer getConvertedPdfImage(String key) {
return cacheService.getPdfImageCache(key); return cacheService.getPdfImageCache(key);
...@@ -63,11 +59,11 @@ public class FileUtils { ...@@ -63,11 +59,11 @@ public class FileUtils {
/** /**
* 查看文件类型(防止参数中存在.点号或者其他特殊字符,所以先抽取文件名,然后再获取文件类型) * 查看文件类型(防止参数中存在.点号或者其他特殊字符,所以先抽取文件名,然后再获取文件类型)
* *
* @param url * @param url url
* @return * @return 文件类型
*/ */
public FileType typeFromUrl(String url) { public FileType typeFromUrl(String url) {
String nonPramStr = url.substring(0, url.indexOf("?") != -1 ? url.indexOf("?") : url.length()); String nonPramStr = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1); String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
return typeFromFileName(fileName); return typeFromFileName(fileName);
} }
...@@ -103,31 +99,20 @@ public class FileUtils { ...@@ -103,31 +99,20 @@ public class FileUtils {
* 从url中剥离出文件名 * 从url中剥离出文件名
* @param url * @param url
* 格式如:http://keking.ufile.ucloud.com.cn/20171113164107_月度绩效表模板(新).xls?UCloudPublicKey=ucloudtangshd@weifenf.com14355492830001993909323&Expires=&Signature=I D1NOFtAJSPT16E6imv6JWuq0k= * 格式如:http://keking.ufile.ucloud.com.cn/20171113164107_月度绩效表模板(新).xls?UCloudPublicKey=ucloudtangshd@weifenf.com14355492830001993909323&Expires=&Signature=I D1NOFtAJSPT16E6imv6JWuq0k=
* @return * @return 文件名
*/ */
public String getFileNameFromURL(String url) { public String getFileNameFromURL(String url) {
// 因为url的参数中可能会存在/的情况,所以直接url.lastIndexOf("/")会有问题 // 因为url的参数中可能会存在/的情况,所以直接url.lastIndexOf("/")会有问题
// 所以先从?处将url截断,然后运用url.lastIndexOf("/")获取文件名 // 所以先从?处将url截断,然后运用url.lastIndexOf("/")获取文件名
String noQueryUrl = url.substring(0, url.indexOf("?") != -1 ? url.indexOf("?"): url.length()); String noQueryUrl = url.substring(0, url.contains("?") ? url.indexOf("?"): url.length());
String fileName = noQueryUrl.substring(noQueryUrl.lastIndexOf("/") + 1); return noQueryUrl.substring(noQueryUrl.lastIndexOf("/") + 1);
return fileName;
}
/**
* 获取文件后缀
* @param fileName
* @return
*/
public String getSuffixFromFileName(String fileName) {
String suffix = fileName.substring(fileName.lastIndexOf("."));
return suffix;
} }
/** /**
* 从路径中获取 * 从路径中获取文件负
* @param path * @param path
* 类似这种:C:\Users\yudian-it\Downloads * 类似这种:C:\Users\yudian-it\Downloads
* @return * @return 文件名
*/ */
public String getFileNameFromPath(String path) { public String getFileNameFromPath(String path) {
return path.substring(path.lastIndexOf(File.separator) + 1); return path.substring(path.lastIndexOf(File.separator) + 1);
...@@ -170,21 +155,26 @@ public class FileUtils { ...@@ -170,21 +155,26 @@ public class FileUtils {
/** /**
* 获取相对路径 * 获取相对路径
* @param absolutePath * @param absolutePath 绝对路径
* @return * @return 相对路径
*/ */
public String getRelativePath(String absolutePath) { public String getRelativePath(String absolutePath) {
return absolutePath.substring(fileDir.length()); return absolutePath.substring(fileDir.length());
} }
/**
* 添加转换后PDF缓存
* @param fileName pdf文件名
* @param value 缓存相对路径
*/
public void addConvertedFile(String fileName, String value){ public void addConvertedFile(String fileName, String value){
cacheService.putPDFCache(fileName, value); cacheService.putPDFCache(fileName, value);
} }
/** /**
* * 添加转换后图片组缓存
* @param pdfFilePath * @param pdfFilePath pdf文件绝对路径
* @param num * @param num 图片张数
*/ */
public void addConvertedPdfImage(String pdfFilePath, int num){ public void addConvertedPdfImage(String pdfFilePath, int num){
cacheService.putPdfImageCache(pdfFilePath, num); cacheService.putPdfImageCache(pdfFilePath, num);
...@@ -192,40 +182,38 @@ public class FileUtils { ...@@ -192,40 +182,38 @@ public class FileUtils {
/** /**
* 获取redis中压缩包内图片文件 * 获取redis中压缩包内图片文件
* @param fileKey * @param fileKey fileKey
* @return * @return 图片文件访问url列表
*/ */
public List getImgCache(String fileKey){ public List<String> getImgCache(String fileKey){
return cacheService.getImgCache(fileKey); return cacheService.getImgCache(fileKey);
} }
/** /**
* 设置redis中压缩包内图片文件 * 设置redis中压缩包内图片文件
* @param fileKey * @param fileKey fileKey
* @param imgs * @param imgs 图片文件访问url列表
*/ */
public void putImgCache(String fileKey,List imgs){ public void putImgCache(String fileKey,List<String> imgs){
cacheService.putImgCache(fileKey, imgs); cacheService.putImgCache(fileKey, imgs);
} }
/** /**
* 判断文件编码格式 * 判断文件编码格式
* @param path * @param path 绝对路径
* @return * @return 编码格式
*/ */
public String getFileEncodeUTFGBK(String path){ public String getFileEncodeUTFGBK(String path){
String enc = Charset.forName("GBK").name(); String enc = Charset.forName("GBK").name();
File file = new File(path); File file = new File(path);
InputStream in= null; InputStream in;
try { try {
in = new FileInputStream(file); in = new FileInputStream(file);
byte[] b = new byte[3]; byte[] b = new byte[3];
in.read(b); in.read(b);
in.close(); in.close();
if (b[0] == -17 && b[1] == -69 && b[2] == -65) { if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
enc = Charset.forName("UTF-8").name(); enc = StandardCharsets.UTF_8.name();
} }
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -235,10 +223,10 @@ public class FileUtils { ...@@ -235,10 +223,10 @@ public class FileUtils {
/** /**
* 对转换后的文件进行操作(改变编码方式) * 对转换后的文件进行操作(改变编码方式)
* @param outFilePath * @param outFilePath 文件绝对路径
*/ */
public void doActionConvertedFile(String outFilePath) { public void doActionConvertedFile(String outFilePath) {
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
try (InputStream inputStream = new FileInputStream(outFilePath); try (InputStream inputStream = new FileInputStream(outFilePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_CONVERTER_CHARSET))){ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_CONVERTER_CHARSET))){
String line; String line;
...@@ -252,53 +240,48 @@ public class FileUtils { ...@@ -252,53 +240,48 @@ public class FileUtils {
sb.append("<script src=\"js/jquery-3.0.0.min.js\" type=\"text/javascript\"></script>"); sb.append("<script src=\"js/jquery-3.0.0.min.js\" type=\"text/javascript\"></script>");
sb.append("<script src=\"js/excel.header.js\" type=\"text/javascript\"></script>"); sb.append("<script src=\"js/excel.header.js\" type=\"text/javascript\"></script>");
sb.append("<link rel=\"stylesheet\" href=\"bootstrap/css/bootstrap.min.css\">"); sb.append("<link rel=\"stylesheet\" href=\"bootstrap/css/bootstrap.min.css\">");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// 重新写入文件 // 重新写入文件
try(FileOutputStream fos = new FileOutputStream(outFilePath); try(FileOutputStream fos = new FileOutputStream(outFilePath);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"))) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
writer.write(sb.toString()); writer.write(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
* 获取文件后缀 * 获取文件后缀
* @param url * @param url url
* @return * @return 文件后缀
*/ */
private String suffixFromUrl(String url) { private String suffixFromUrl(String url) {
String nonPramStr = url.substring(0, url.indexOf("?") != -1 ? url.indexOf("?") : url.length()); String nonPramStr = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1); String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
return suffixFromFileName(fileName); return suffixFromFileName(fileName);
} }
private String suffixFromFileName(String fileName) { private String suffixFromFileName(String fileName) {
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1); return fileName.substring(fileName.lastIndexOf(".") + 1);
return fileType;
} }
/** /**
* 获取url中的参数 * 获取url中的参数
* @param url * @param url url
* @param name * @param name 参数名
* @return * @return 参数值
*/ */
public String getUrlParameterReg(String url, String name) { public String getUrlParameterReg(String url, String name) {
Map<String, String> mapRequest = new HashMap(); Map<String, String> mapRequest = new HashMap<>();
String strUrlParam = truncateUrlPage(url); String strUrlParam = truncateUrlPage(url);
if (strUrlParam == null) { if (strUrlParam == null) {
return ""; return "";
} }
//每个键值为一组 //每个键值为一组
String[] arrSplit=strUrlParam.split("[&]"); String[] arrSplit = strUrlParam.split("[&]");
for(String strSplit:arrSplit) { for(String strSplit : arrSplit) {
String[] arrSplitEqual= strSplit.split("[=]"); String[] arrSplitEqual = strSplit.split("[=]");
//解析出键值 //解析出键值
if(arrSplitEqual.length > 1) { if(arrSplitEqual.length > 1) {
//正确解析 //正确解析
...@@ -330,7 +313,11 @@ public class FileUtils { ...@@ -330,7 +313,11 @@ public class FileUtils {
return strAllParam; return strAllParam;
} }
/**
* 获取文件属性
* @param url url
* @return 文件属性
*/
public FileAttribute getFileAttribute(String url) { public FileAttribute getFileAttribute(String url) {
String fileName; String fileName;
FileType type; FileType type;
...@@ -345,6 +332,6 @@ public class FileUtils { ...@@ -345,6 +332,6 @@ public class FileUtils {
type = typeFromUrl(url); type = typeFromUrl(url);
suffix = suffixFromUrl(url); suffix = suffixFromUrl(url);
} }
return new FileAttribute(type,suffix,fileName,url,url); return new FileAttribute(type,suffix,fileName,url);
} }
} }
...@@ -11,6 +11,7 @@ import java.io.FileOutputStream; ...@@ -11,6 +11,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
/** /**
* @auther: chenjh * @auther: chenjh
...@@ -47,7 +48,7 @@ public class FtpUtils { ...@@ -47,7 +48,7 @@ public class FtpUtils {
FTPClient ftpClient = connect(host, port, username, password, controlEncoding); FTPClient ftpClient = connect(host, port, username, password, controlEncoding);
OutputStream outputStream = new FileOutputStream(localFilePath); OutputStream outputStream = new FileOutputStream(localFilePath);
ftpClient.enterLocalPassiveMode(); ftpClient.enterLocalPassiveMode();
boolean downloadResult = ftpClient.retrieveFile(new String(remoteFilePath.getBytes(controlEncoding), "ISO-8859-1"), outputStream); boolean downloadResult = ftpClient.retrieveFile(new String(remoteFilePath.getBytes(controlEncoding), StandardCharsets.ISO_8859_1), outputStream);
LOGGER.debug("FTP download result {}", downloadResult); LOGGER.debug("FTP download result {}", downloadResult);
outputStream.flush(); outputStream.flush();
outputStream.close(); outputStream.close();
......
package cn.keking.utils; package cn.keking.utils;
import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.File; import java.io.File;
/** /**
...@@ -9,38 +10,18 @@ import java.io.File; ...@@ -9,38 +10,18 @@ import java.io.File;
*/ */
@Component @Component
public class OfficeToPdf { public class OfficeToPdf {
/** private final ConverterUtils converterUtils;
* 获取OpenOffice.org 3的安装目录
* public OfficeToPdf(ConverterUtils converterUtils) {
* @return OpenOffice.org 3的安装目录 this.converterUtils = converterUtils;
*/ }
@Autowired
ConverterUtils converterUtils; public void openOfficeToPDF(String inputFilePath, String outputFilePath) {
/** office2pdf(inputFilePath, outputFilePath);
* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
*
* @param inputFilePath
* 源文件路径,如:"e:/test.docx"
* @param outputFilePath
* 目标文件路径,如:"e:/test_docx.pdf"
* @return
*/
public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
return office2pdf(inputFilePath, outputFilePath);
} }
/**
* 转换文件
*
* @param inputFile
* @param outputFilePath_end
* @param inputFilePath
* @param outputFilePath
* @param converter
*/
public static void converterFile(File inputFile, String outputFilePath_end, public static void converterFile(File inputFile, String outputFilePath_end,
String inputFilePath, String outputFilePath,
OfficeDocumentConverter converter) { OfficeDocumentConverter converter) {
File outputFile = new File(outputFilePath_end); File outputFile = new File(outputFilePath_end);
// 假如目标路径不存在,则新建该路径 // 假如目标路径不存在,则新建该路径
...@@ -50,17 +31,8 @@ public class OfficeToPdf { ...@@ -50,17 +31,8 @@ public class OfficeToPdf {
converter.convert(inputFile, outputFile); converter.convert(inputFile, outputFile);
} }
/**
* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件 public void office2pdf(String inputFilePath, String outputFilePath) {
*
* @param inputFilePath
* 源文件路径,如:"e:/test.docx"
* @param outputFilePath
* 目标文件路径,如:"e:/test_docx.pdf"
* @return
*/
public boolean office2pdf(String inputFilePath, String outputFilePath) {
boolean flag = false;
OfficeDocumentConverter converter = converterUtils.getDocumentConverter(); OfficeDocumentConverter converter = converterUtils.getDocumentConverter();
if (null != inputFilePath) { if (null != inputFilePath) {
File inputFile = new File(inputFilePath); File inputFile = new File(inputFilePath);
...@@ -68,43 +40,23 @@ public class OfficeToPdf { ...@@ -68,43 +40,23 @@ public class OfficeToPdf {
if (null == outputFilePath) { if (null == outputFilePath) {
// 转换后的文件路径 // 转换后的文件路径
String outputFilePath_end = getOutputFilePath(inputFilePath); String outputFilePath_end = getOutputFilePath(inputFilePath);
if (inputFile.exists()) {// 找不到源文件, 则返回 if (inputFile.exists()) {
converterFile(inputFile, outputFilePath_end, inputFilePath, // 找不到源文件, 则返回
outputFilePath, converter); converterFile(inputFile, outputFilePath_end,converter);
flag = true;
} }
} else { } else {
if (inputFile.exists()) {// 找不到源文件, 则返回 if (inputFile.exists()) {
converterFile(inputFile, outputFilePath, inputFilePath, // 找不到源文件, 则返回
outputFilePath, converter); converterFile(inputFile, outputFilePath, converter);
flag = true;
} }
} }
// officeManager.stop();
} else {
flag = false;
} }
return flag;
} }
/**
* 获取输出文件
*
* @param inputFilePath
* @return
*/
public static String getOutputFilePath(String inputFilePath) { public static String getOutputFilePath(String inputFilePath) {
String outputFilePath = inputFilePath.replaceAll("." return inputFilePath.replaceAll("."+ getPostfix(inputFilePath), ".pdf");
+ getPostfix(inputFilePath), ".pdf");
return outputFilePath;
} }
/**
* 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"
*
* @param inputFilePath
* @return
*/
public static String getPostfix(String inputFilePath) { public static String getPostfix(String inputFilePath) {
return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1); return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
} }
......
...@@ -6,12 +6,14 @@ import org.apache.pdfbox.rendering.PDFRenderer; ...@@ -6,12 +6,14 @@ import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil; import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -20,16 +22,28 @@ public class PdfUtils { ...@@ -20,16 +22,28 @@ public class PdfUtils {
private final Logger logger = LoggerFactory.getLogger(PdfUtils.class); private final Logger logger = LoggerFactory.getLogger(PdfUtils.class);
@Autowired private final FileUtils fileUtils;
FileUtils fileUtils;
@Value("${server.tomcat.uri-encoding:UTF-8}")
private String uriEncoding;
public PdfUtils(FileUtils fileUtils) {
this.fileUtils = fileUtils;
}
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 = fileUtils.getConvertedPdfImage(pdfFilePath); Integer imageCount = fileUtils.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 = baseUrl + pdfFolder; String urlPrefix = null;
if (imageCount != null && imageCount.intValue() > 0) { try {
urlPrefix = baseUrl + URLEncoder.encode(URLEncoder.encode(pdfFolder, uriEncoding), uriEncoding);
} catch (UnsupportedEncodingException e) {
logger.error("UnsupportedEncodingException", e);
urlPrefix = baseUrl + pdfFolder;
}
if (imageCount != null && imageCount > 0) {
for (int i = 0; i < imageCount ; i++) for (int i = 0; i < imageCount ; i++)
imageUrls.add(urlPrefix + "/" + i + imageFileSuffix); imageUrls.add(urlPrefix + "/" + i + imageFileSuffix);
return imageUrls; return imageUrls;
......
...@@ -4,7 +4,6 @@ import cn.keking.config.ConfigConstants; ...@@ -4,7 +4,6 @@ import cn.keking.config.ConfigConstants;
import cn.keking.service.cache.CacheService; import cn.keking.service.cache.CacheService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -19,10 +18,13 @@ public class ShedulerClean { ...@@ -19,10 +18,13 @@ public class ShedulerClean {
private final Logger logger = LoggerFactory.getLogger(ShedulerClean.class); private final Logger logger = LoggerFactory.getLogger(ShedulerClean.class);
@Autowired private final CacheService cacheService;
private CacheService cacheService;
private String fileDir = ConfigConstants.getFileDir(); public ShedulerClean(CacheService cacheService) {
this.cacheService = cacheService;
}
private final String fileDir = ConfigConstants.getFileDir();
//默认每晚3点执行一次 //默认每晚3点执行一次
@Scheduled(cron = "${cache.clean.cron:0 0 3 * * ?}") @Scheduled(cron = "${cache.clean.cron:0 0 3 * * ?}")
......
package cn.keking.utils;
import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 读取类文本文件
* @author yudian-it
* @date 2017/12/13
*/
@Component
public class SimTextUtil {
@Autowired
private FileUtils fileUtils;
@Autowired
private DownloadUtils downloadUtils;
public ReturnResponse<String> readSimText(String url, String fileName){
FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
return response;
}
}
...@@ -3,7 +3,6 @@ package cn.keking.utils; ...@@ -3,7 +3,6 @@ package cn.keking.utils;
import cn.keking.config.ConfigConstants; import cn.keking.config.ConfigConstants;
import cn.keking.model.FileType; import cn.keking.model.FileType;
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;
import com.github.junrar.Archive; import com.github.junrar.Archive;
...@@ -15,7 +14,6 @@ import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry; ...@@ -15,7 +14,6 @@ import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile; import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile; import org.apache.commons.compress.archivers.zip.ZipFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.*; import java.io.*;
...@@ -37,32 +35,20 @@ import java.util.regex.Pattern; ...@@ -37,32 +35,20 @@ import java.util.regex.Pattern;
public class ZipReader { public class ZipReader {
static Pattern pattern = Pattern.compile("^\\d+"); static Pattern pattern = Pattern.compile("^\\d+");
@Autowired private final FileUtils fileUtils;
FileUtils fileUtils;
String fileDir = ConfigConstants.getFileDir();
ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); private final String fileDir = ConfigConstants.getFileDir();
private final ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public ZipReader(FileUtils fileUtils) {
this.fileUtils = fileUtils;
}
/**
* 读取压缩文件
* 文件压缩到统一目录fileDir下,并且命名使用压缩文件名+文件名因为文件名
* 可能会重复(在系统中对于同一种类型的材料压缩文件内的文件是一样的,如果文件名
* 重复,那么这里会被覆盖[同一个压缩文件中的不同目录中的相同文件名暂时不考虑])
* <b>注:</b>
* <p>
* 文件名命名中的参数的说明:
* 1.archiveName,为避免解压的文件中有重名的文件会彼此覆盖,所以加上了archiveName,因为在ufile中archiveName
* 是不会重复的。
* 2.level,这里层级结构的列表我是通过一个map来构造的,map的key是文件的名字,值是对应的文件,这样每次向map中
* 加入节点的时候都会获取父节点是否存在,存在则会获取父节点的value并将当前节点加入到父节点的childList中(这里利用
* 的是java语言的引用的特性)。
* </p>
* @param filePath
*/
public String readZipFile(String filePath,String fileKey) { public String readZipFile(String filePath,String fileKey) {
String archiveSeparator = "/"; String archiveSeparator = "/";
Map<String, FileNode> appender = Maps.newHashMap(); Map<String, FileNode> appender = Maps.newHashMap();
List imgUrls=Lists.newArrayList(); List<String> imgUrls = Lists.newArrayList();
String baseUrl = BaseUrlFilter.getBaseUrl(); String baseUrl = BaseUrlFilter.getBaseUrl();
String archiveFileName = fileUtils.getFileNameFromPath(filePath); String archiveFileName = fileUtils.getFileNameFromPath(filePath);
try { try {
...@@ -89,7 +75,7 @@ public class ZipReader { ...@@ -89,7 +75,7 @@ public class ZipReader {
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);
} }
...@@ -103,24 +89,18 @@ public class ZipReader { ...@@ -103,24 +89,18 @@ public class ZipReader {
} }
} }
/**
* 排序zipEntries(对原来列表倒序)
* @param entries
*/
private Enumeration<ZipArchiveEntry> sortZipEntries(Enumeration<ZipArchiveEntry> entries) { private Enumeration<ZipArchiveEntry> sortZipEntries(Enumeration<ZipArchiveEntry> entries) {
List<ZipArchiveEntry> sortedEntries = Lists.newArrayList(); List<ZipArchiveEntry> sortedEntries = Lists.newArrayList();
while(entries.hasMoreElements()){ while(entries.hasMoreElements()){
sortedEntries.add(entries.nextElement()); sortedEntries.add(entries.nextElement());
} }
Collections.sort(sortedEntries, 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 = Maps.newHashMap(); Map<String, FileNode> appender = Maps.newHashMap();
List imgUrls=Lists.newArrayList(); List<String> imgUrls = Lists.newArrayList();
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(new File(filePath)));
...@@ -144,35 +124,27 @@ public class ZipReader { ...@@ -144,35 +124,27 @@ 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=fileUtils.typeFromUrl(childName); FileType type = fileUtils.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));
fileUtils.putImgCache(fileKey,imgUrls); fileUtils.putImgCache(fileKey,imgUrls);
return new ObjectMapper().writeValueAsString(appender.get("")); return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (RarException e) { } catch (RarException | IOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
/**
* 解压7z文件
* @param filePath
* @param fileKey
* @return
*/
public String read7zFile(String filePath,String fileKey) { public String read7zFile(String filePath,String fileKey) {
String archiveSeparator = "/"; String archiveSeparator = "/";
Map<String, FileNode> appender = Maps.newHashMap(); Map<String, FileNode> appender = Maps.newHashMap();
List imgUrls=Lists.newArrayList(); List<String> imgUrls = Lists.newArrayList();
String baseUrl= BaseUrlFilter.getBaseUrl(); String baseUrl= BaseUrlFilter.getBaseUrl();
String archiveFileName = fileUtils.getFileNameFromPath(filePath); String archiveFileName = fileUtils.getFileNameFromPath(filePath);
try { try {
...@@ -199,7 +171,7 @@ public class ZipReader { ...@@ -199,7 +171,7 @@ public class ZipReader {
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);
} }
...@@ -211,29 +183,23 @@ public class ZipReader { ...@@ -211,29 +183,23 @@ public class ZipReader {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
/**
* 排序7ZEntries(对原来列表倒序)
* @param entries
*/
private Enumeration<SevenZArchiveEntry> sortSevenZEntries(Iterable<SevenZArchiveEntry> entries) { private Enumeration<SevenZArchiveEntry> sortSevenZEntries(Iterable<SevenZArchiveEntry> entries) {
List<SevenZArchiveEntry> sortedEntries = Lists.newArrayList(); List<SevenZArchiveEntry> sortedEntries = Lists.newArrayList();
Iterator<SevenZArchiveEntry> iterator = entries.iterator(); for (SevenZArchiveEntry entry : entries) {
while(iterator.hasNext()){ sortedEntries.add(entry);
sortedEntries.add(iterator.next());
} }
// Collections.sort(sortedEntries, Comparator.comparingInt(o -> o.getName().length()));
return Collections.enumeration(sortedEntries); return Collections.enumeration(sortedEntries);
} }
private void addNodes(Map<String, FileNode> appender, String parentName, FileNode node) { private void addNodes(Map<String, FileNode> appender, String parentName, FileNode node) {
if (appender.containsKey(parentName)) { if (appender.containsKey(parentName)) {
appender.get(parentName).getChildList().add(node); appender.get(parentName).getChildList().add(node);
Collections.sort(appender.get(parentName).getChildList(), sortComparator); appender.get(parentName).getChildList().sort(sortComparator);
// appender.get(parentName).getChildList().sort((final FileNode h1, final FileNode h2) -> h1.getOriginName().compareTo(h2.getOriginName()));//排序 } else {
}else { // 根节点 // 根节点
FileNode nodeRoot = new FileNode(parentName, parentName, "", new ArrayList<>(), true); FileNode nodeRoot = new FileNode(parentName, parentName, "", new ArrayList<>(), true);
nodeRoot.getChildList().add(node); nodeRoot.getChildList().add(node);
appender.put("", nodeRoot); appender.put("", nodeRoot);
...@@ -255,15 +221,6 @@ public class ZipReader { ...@@ -255,15 +221,6 @@ public class ZipReader {
return sortedHeaders; return sortedHeaders;
} }
/**
* 获取倒数第二个文件(夹)名
* @param fullName
* @param seperator
* 压缩文件解压后,不同的压缩格式分隔符不一样zip是/,而rar是\
* @param rootName
* 根目录名:如果倒数第二个路径为空,那么赋值为rootName
* @return
*/
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);
...@@ -271,34 +228,27 @@ public class ZipReader { ...@@ -271,34 +228,27 @@ public class ZipReader {
// 1.获取剩余部分 // 1.获取剩余部分
int endIndex = fullName.lastIndexOf(seperator); int endIndex = fullName.lastIndexOf(seperator);
String leftPath = fullName.substring(0, endIndex == -1 ? 0 : endIndex); String leftPath = fullName.substring(0, endIndex == -1 ? 0 : endIndex);
if (null != leftPath && leftPath.length() > 1) { if (leftPath.length() > 1) {
// 2.获取倒数第二个 // 2.获取倒数第二个
return getLastFileName(leftPath, seperator); return getLastFileName(leftPath, seperator);
}else { } else {
return rootName; return rootName;
} }
} }
/**
* 获取最后一个文件(夹)的名字
* @param fullName
* @param seperator
* 压缩文件解压后,不同的压缩格式分隔符不一样zip是/,而rar是\
* @return
*/
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 (null != fullName && fullName.contains(seperator)) { if (fullName.contains(seperator)) {
newName = fullName.substring(fullName.lastIndexOf(seperator) + 1); newName = fullName.substring(fullName.lastIndexOf(seperator) + 1);
} }
return newName; return newName;
} }
public static Comparator<FileNode> sortComparator = new Comparator<FileNode>() { public static Comparator<FileNode> sortComparator = new Comparator<FileNode>() {
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) {
// 判断两个对比对象是否是开头包含数字,如果包含数字则获取数字并按数字真正大小进行排序 // 判断两个对比对象是否是开头包含数字,如果包含数字则获取数字并按数字真正大小进行排序
...@@ -321,21 +271,16 @@ public class ZipReader { ...@@ -321,21 +271,16 @@ public class ZipReader {
return null; return null;
} }
/** public static class FileNode {
* 文件节点(区分文件上下级)
*/
public class FileNode{
private String originName; private String originName;
private String fileName; private String fileName;
private String parentFileName; private String parentFileName;
private boolean directory; private boolean directory;
private String fileKey;//用于图片预览时寻址 //用于图片预览时寻址
private String fileKey;
private List<FileNode> childList; private List<FileNode> childList;
public FileNode() {
}
public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory) { public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory) {
this.originName = originName; this.originName = originName;
this.fileName = fileName; this.fileName = fileName;
...@@ -410,14 +355,11 @@ public class ZipReader { ...@@ -410,14 +355,11 @@ public class ZipReader {
} }
} }
/**
* Zip文件抽取线程
*/
class ZipExtractorWorker implements Runnable { class ZipExtractorWorker implements Runnable {
private List<Map<String, ZipArchiveEntry>> entriesToBeExtracted; private final List<Map<String, ZipArchiveEntry>> entriesToBeExtracted;
private ZipFile zipFile; private final ZipFile zipFile;
private String filePath; private final String filePath;
public ZipExtractorWorker(List<Map<String, ZipArchiveEntry>> entriesToBeExtracted, ZipFile zipFile, String filePath) { public ZipExtractorWorker(List<Map<String, ZipArchiveEntry>> entriesToBeExtracted, ZipFile zipFile, String filePath) {
this.entriesToBeExtracted = entriesToBeExtracted; this.entriesToBeExtracted = entriesToBeExtracted;
...@@ -446,12 +388,6 @@ public class ZipReader { ...@@ -446,12 +388,6 @@ public class ZipReader {
} }
} }
/**
* 读取压缩文件并写入到fileDir文件夹下
* @param childName
* @param zipFile
*/
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)){
...@@ -460,21 +396,16 @@ public class ZipReader { ...@@ -460,21 +396,16 @@ public class ZipReader {
while ((-1 != (len = zipFile.read(inByte)))){ while ((-1 != (len = zipFile.read(inByte)))){
ot.write(inByte, 0, len); ot.write(inByte, 0, len);
} }
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
/**
* 7z文件抽取线程
*/
class SevenZExtractorWorker implements Runnable { class SevenZExtractorWorker implements Runnable {
private List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted; private final List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted;
private String filePath; private final String filePath;
public SevenZExtractorWorker(List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted, String filePath) { public SevenZExtractorWorker(List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted, String filePath) {
this.entriesToBeExtracted = entriesToBeExtracted; this.entriesToBeExtracted = entriesToBeExtracted;
...@@ -483,7 +414,6 @@ public class ZipReader { ...@@ -483,7 +414,6 @@ public class ZipReader {
@Override @Override
public void run() { public void run() {
System.out.println("解析压缩文件开始《《《《《《《《《《《《《《《《《《《《《《《");
try { try {
SevenZFile sevenZFile = new SevenZFile(new File(filePath)); SevenZFile sevenZFile = new SevenZFile(new File(filePath));
SevenZArchiveEntry entry = sevenZFile.getNextEntry(); SevenZArchiveEntry entry = sevenZFile.getNextEntry();
...@@ -493,7 +423,7 @@ public class ZipReader { ...@@ -493,7 +423,7 @@ public class ZipReader {
continue; continue;
} }
String childName = "default_file"; String childName = "default_file";
SevenZArchiveEntry entry1 = null; SevenZArchiveEntry entry1;
for (Map<String, SevenZArchiveEntry> entryMap : entriesToBeExtracted) { for (Map<String, SevenZArchiveEntry> entryMap : entriesToBeExtracted) {
childName = entryMap.keySet().iterator().next(); childName = entryMap.keySet().iterator().next();
entry1 = entryMap.values().iterator().next(); entry1 = entryMap.values().iterator().next();
...@@ -509,8 +439,6 @@ public class ZipReader { ...@@ -509,8 +439,6 @@ public class ZipReader {
entry = sevenZFile.getNextEntry(); entry = sevenZFile.getNextEntry();
} }
sevenZFile.close(); sevenZFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -518,20 +446,16 @@ public class ZipReader { ...@@ -518,20 +446,16 @@ public class ZipReader {
if (new File(filePath).exists()) { if (new File(filePath).exists()) {
new File(filePath).delete(); new File(filePath).delete();
} }
System.out.println("解析压缩文件结束《《《《《《《《《《《《《《《《《《《《《《《");
} }
} }
/**
* Rar文件抽取
*/
class RarExtractorWorker implements Runnable { class RarExtractorWorker implements Runnable {
private List<Map<String, FileHeader>> headersToBeExtracted; private final List<Map<String, FileHeader>> headersToBeExtracted;
private Archive archive; private final Archive archive;
/** /**
* 用以删除源文件 * 用以删除源文件
*/ */
private String filePath; private final String filePath;
public RarExtractorWorker(List<Map<String, FileHeader>> headersToBeExtracted, Archive archive, String filePath) { public RarExtractorWorker(List<Map<String, FileHeader>> headersToBeExtracted, Archive archive, String filePath) {
this.headersToBeExtracted = headersToBeExtracted; this.headersToBeExtracted = headersToBeExtracted;
...@@ -541,7 +465,6 @@ public class ZipReader { ...@@ -541,7 +465,6 @@ public class ZipReader {
@Override @Override
public void run() { public void run() {
System.out.println("解析压缩文件开始《《《《《《《《《《《《《《《《《《《《《《《");
for (Map<String, FileHeader> entryMap : headersToBeExtracted) { for (Map<String, FileHeader> entryMap : headersToBeExtracted) {
String childName = entryMap.keySet().iterator().next(); String childName = entryMap.keySet().iterator().next();
extractRarFile(childName, entryMap.values().iterator().next(), archive); extractRarFile(childName, entryMap.values().iterator().next(), archive);
...@@ -554,24 +477,13 @@ public class ZipReader { ...@@ -554,24 +477,13 @@ public class ZipReader {
if (new File(filePath).exists()) { if (new File(filePath).exists()) {
new File(filePath).delete(); new File(filePath).delete();
} }
System.out.println("解析压缩文件结束《《《《《《《《《《《《《《《《《《《《《《《");
} }
/**
* 抽取rar文件到指定目录下
* @param childName
* @param header
* @param archive
*/
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 (FileNotFoundException e) { } catch (IOException | RarException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RarException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
......
...@@ -6,22 +6,20 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -6,22 +6,20 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.utils.FileUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*; import java.io.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.Objects;
/** /**
* *
...@@ -33,11 +31,11 @@ public class FileController { ...@@ -33,11 +31,11 @@ public class FileController {
private final Logger logger = LoggerFactory.getLogger(FileController.class); private final Logger logger = LoggerFactory.getLogger(FileController.class);
private String fileDir = ConfigConstants.getFileDir(); private final String fileDir = ConfigConstants.getFileDir();
@Autowired
private FileUtils fileUtils; private final String demoDir = "demo";
private String demoDir = "demo";
private String demoPath = demoDir + File.separator; private final String demoPath = demoDir + File.separator;
@RequestMapping(value = "fileUpload", method = RequestMethod.POST) @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
public String fileUpload(@RequestParam("file") MultipartFile file) throws JsonProcessingException { public String fileUpload(@RequestParam("file") MultipartFile file) throws JsonProcessingException {
...@@ -49,7 +47,7 @@ public class FileController { ...@@ -49,7 +47,7 @@ public class FileController {
// Check for Windows-style path // Check for Windows-style path
int winSep = fileName.lastIndexOf('\\'); int winSep = fileName.lastIndexOf('\\');
// Cut off at latest possible point // Cut off at latest possible point
int pos = (winSep > unixSep ? winSep : unixSep); int pos = (Math.max(winSep, unixSep));
if (pos != -1) { if (pos != -1) {
fileName = fileName.substring(pos + 1); fileName = fileName.substring(pos + 1);
} }
...@@ -62,13 +60,8 @@ public class FileController { ...@@ -62,13 +60,8 @@ public class FileController {
outFile.mkdirs(); outFile.mkdirs();
} }
logger.info("上传文件:{}", outFile.getAbsolutePath()); logger.info("上传文件:{}", outFile.getAbsolutePath());
try(InputStream in = file.getInputStream(); try(InputStream in = file.getInputStream(); OutputStream out = new FileOutputStream(fileDir + demoPath + fileName)) {
OutputStream ot = new FileOutputStream(fileDir + demoPath + fileName)){ StreamUtils.copy(in, out);
byte[] buffer = new byte[1024];
int len;
while ((-1 != (len = in.read(buffer)))) {
ot.write(buffer, 0, len);
}
return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(0, "SUCCESS", null)); return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(0, "SUCCESS", null));
} catch (IOException e) { } catch (IOException e) {
logger.error("文件上传失败", e); logger.error("文件上传失败", e);
...@@ -94,29 +87,13 @@ public class FileController { ...@@ -94,29 +87,13 @@ public class FileController {
List<Map<String, String>> list = Lists.newArrayList(); List<Map<String, String>> list = Lists.newArrayList();
File file = new File(fileDir + demoPath); File file = new File(fileDir + demoPath);
if (file.exists()) { if (file.exists()) {
Arrays.stream(file.listFiles()).forEach(file1 -> list.add(ImmutableMap.of("fileName", demoDir + "/" + file1.getName()))); Arrays.stream(Objects.requireNonNull(file.listFiles())).forEach(file1 -> list.add(ImmutableMap.of("fileName", demoDir + "/" + file1.getName())));
} }
return new ObjectMapper().writeValueAsString(list); return new ObjectMapper().writeValueAsString(list);
} }
private String getFileName(String name) {
String suffix = name.substring(name.lastIndexOf("."));
String nameNoSuffix = name.substring(0, name.lastIndexOf("."));
String uuid = UUID.randomUUID().toString();
return uuid + "-" + nameNoSuffix + suffix;
}
/**
* 是否存在该类型的文件
* @return
* @param fileName
*/
private boolean existsFile(String fileName) { private boolean existsFile(String fileName) {
boolean result = false;
File file = new File(fileDir + demoPath + fileName); File file = new File(fileDir + demoPath + fileName);
if (file.exists()) { return file.exists();
return true;
}
return result;
} }
} }
...@@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestMethod; ...@@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
@Controller @Controller
public class IndexController { public class IndexController {
@RequestMapping(value = "index", method = RequestMethod.GET) @RequestMapping(value = "/index", method = RequestMethod.GET)
public String go2Index(){ public String go2Index(){
return "index"; return "index";
} }
......
package cn.keking.web.controller; package cn.keking.web.controller;
import cn.keking.config.ConfigConstants; import cn.keking.config.ConfigConstants;
import cn.keking.hutool.URLUtil;
import cn.keking.model.FileAttribute; import cn.keking.model.FileAttribute;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.service.FilePreviewFactory; 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.FileUtils; import cn.keking.utils.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -22,7 +20,6 @@ import org.springframework.web.bind.annotation.ResponseBody; ...@@ -22,7 +20,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -34,20 +31,25 @@ public class OnlinePreviewController { ...@@ -34,20 +31,25 @@ public class OnlinePreviewController {
private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class); private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class);
@Autowired private final FilePreviewFactory previewFactory;
private FilePreviewFactory previewFactory;
@Autowired private final CacheService cacheService;
private CacheService cacheService;
private final FileUtils fileUtils;
private final DownloadUtils downloadUtils;
public OnlinePreviewController(FilePreviewFactory filePreviewFactory,
FileUtils fileUtils,
CacheService cacheService,
DownloadUtils downloadUtils) {
this.previewFactory = filePreviewFactory;
this.fileUtils = fileUtils;
this.cacheService = cacheService;
this.downloadUtils = downloadUtils;
}
@Autowired
private FileUtils fileUtils;
/**
* @param url
* @param model
* @return
*/
@RequestMapping(value = "/onlinePreview", method = RequestMethod.GET) @RequestMapping(value = "/onlinePreview", method = RequestMethod.GET)
public String onlinePreview(String url, Model model, HttpServletRequest req) { public String onlinePreview(String url, Model model, HttpServletRequest req) {
FileAttribute fileAttribute = fileUtils.getFileAttribute(url); FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
...@@ -66,7 +68,7 @@ public class OnlinePreviewController { ...@@ -66,7 +68,7 @@ public class OnlinePreviewController {
String currentUrl = req.getParameter("currentUrl"); String currentUrl = req.getParameter("currentUrl");
logger.info("预览文件url:{},urls:{}", currentUrl, urls); logger.info("预览文件url:{},urls:{}", currentUrl, urls);
String[] imgs = urls.split("\\|"); String[] imgs = urls.split("\\|");
List imgurls = Arrays.asList(imgs); List<String> imgurls = Arrays.asList(imgs);
model.addAttribute("imgurls", imgurls); model.addAttribute("imgurls", imgurls);
model.addAttribute("currentUrl", currentUrl); model.addAttribute("currentUrl", currentUrl);
return "picture"; return "picture";
...@@ -75,31 +77,16 @@ public class OnlinePreviewController { ...@@ -75,31 +77,16 @@ public class OnlinePreviewController {
* 根据url获取文件内容 * 根据url获取文件内容
* 当pdfjs读取存在跨域问题的文件时将通过此接口读取 * 当pdfjs读取存在跨域问题的文件时将通过此接口读取
* *
* @param urlPath * @param urlPath url
* @param resp * @param response response
*/ */
@RequestMapping(value = "/getCorsFile", method = RequestMethod.GET) @RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
public void getCorsFile(String urlPath, HttpServletResponse resp) { public void getCorsFile(String urlPath, HttpServletResponse response) {
InputStream inputStream = null;
logger.info("下载跨域pdf文件url:{}", urlPath); logger.info("下载跨域pdf文件url:{}", urlPath);
try { try {
URL url = new URL(URLUtil.normalize(urlPath, true)); downloadUtils.saveToOutputStreamFormUrl(urlPath, response.getOutputStream());
//打开请求连接
URLConnection connection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
inputStream = httpURLConnection.getInputStream();
byte[] bs = new byte[1024];
int len;
while (-1 != (len = inputStream.read(bs))) {
resp.getOutputStream().write(bs, 0, len);
}
} catch (IOException e) { } catch (IOException e) {
logger.error("下载跨域pdf文件异常,url:{}", urlPath, e); logger.error("下载跨域pdf文件异常,url:{}", urlPath, e);
} finally {
if (inputStream != null) {
IOUtils.closeQuietly(inputStream);
}
} }
} }
......
...@@ -27,7 +27,7 @@ public class BaseUrlFilter implements Filter { ...@@ -27,7 +27,7 @@ public class BaseUrlFilter implements Filter {
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) {
} }
......
package cn.keking.web.filter; package cn.keking.web.filter;
import cn.keking.config.ConfigConstants;
import org.springframework.web.context.request.RequestContextHolder;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
/** /**
...@@ -15,40 +12,12 @@ import java.io.IOException; ...@@ -15,40 +12,12 @@ import java.io.IOException;
public class ChinesePathFilter implements Filter { public class ChinesePathFilter implements Filter {
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) {
} }
private static String BASE_URL;
public static String getBaseUrl() {
String baseUrl;
try {
baseUrl = (String) RequestContextHolder.currentRequestAttributes().getAttribute("baseUrl",0);
} catch (Exception e) {
baseUrl = BASE_URL;
}
return baseUrl;
}
@Override @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
String baseUrl;
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(request.getScheme()).append("://").append(request.getServerName()).append(":")
.append(request.getServerPort()).append(((HttpServletRequest) request).getContextPath()).append("/");
String baseUrlTmp = ConfigConstants.getBaseUrl();
if (baseUrlTmp != null && !ConfigConstants.DEFAULT_BASE_URL.equals(baseUrlTmp.toLowerCase())) {
if (!baseUrlTmp.endsWith("/")) {
baseUrlTmp = baseUrlTmp.concat("/");
}
baseUrl = baseUrlTmp;
} else {
baseUrl = pathBuilder.toString();
}
BASE_URL = baseUrl;
request.setAttribute("baseUrl", baseUrl);
chain.doFilter(request, response); chain.doFilter(request, response);
} }
......
...@@ -20,7 +20,7 @@ public class TrustHostFilter implements Filter { ...@@ -20,7 +20,7 @@ public class TrustHostFilter implements Filter {
private String notTrustHost; private String notTrustHost;
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) {
ClassPathResource classPathResource = new ClassPathResource("web/notTrustHost.html"); ClassPathResource classPathResource = new ClassPathResource("web/notTrustHost.html");
try { try {
classPathResource.getInputStream(); classPathResource.getInputStream();
...@@ -35,7 +35,7 @@ public class TrustHostFilter implements Filter { ...@@ -35,7 +35,7 @@ public class TrustHostFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String url = getSourceUrl(request); String url = getSourceUrl(request);
String host = getHost(url); String host = getHost(url);
if (!ConfigConstants.getTrustHostSet().isEmpty() && !ConfigConstants.getTrustHostSet().contains(host)) { if (host != null &&!ConfigConstants.getTrustHostSet().isEmpty() && !ConfigConstants.getTrustHostSet().contains(host)) {
String html = this.notTrustHost.replace("${current_host}", host); String html = this.notTrustHost.replace("${current_host}", host);
response.getWriter().write(html); response.getWriter().write(html);
response.getWriter().close(); response.getWriter().close();
...@@ -68,7 +68,7 @@ public class TrustHostFilter implements Filter { ...@@ -68,7 +68,7 @@ public class TrustHostFilter implements Filter {
try { try {
URL url = new URL(urlStr); URL url = new URL(urlStr);
return url.getHost().toLowerCase(); return url.getHost().toLowerCase();
} catch (MalformedURLException e) { } catch (MalformedURLException ignored) {
} }
return null; return null;
} }
......
...@@ -12,7 +12,7 @@ import java.io.IOException; ...@@ -12,7 +12,7 @@ import java.io.IOException;
public class WatermarkConfigFilter implements Filter { public class WatermarkConfigFilter implements Filter {
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) {
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment