mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-03-14 13:13:47 +08:00
优化:重构大量代码,修复异常
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user