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