2018-01-17 14:10:40 +08:00
|
|
|
|
package cn.keking.utils;
|
|
|
|
|
|
2019-04-11 14:45:22 +08:00
|
|
|
|
import cn.keking.config.ConfigConstants;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import cn.keking.model.FileAttribute;
|
|
|
|
|
import cn.keking.model.FileType;
|
2019-04-08 17:50:13 +08:00
|
|
|
|
import cn.keking.service.cache.CacheService;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
2019-06-17 14:21:16 +08:00
|
|
|
|
import org.springframework.util.StringUtils;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.nio.charset.Charset;
|
2020-05-15 18:09:19 +08:00
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
import java.util.Arrays;
|
2019-06-17 14:21:16 +08:00
|
|
|
|
import java.util.HashMap;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author yudian-it
|
|
|
|
|
* @date 2017/11/13
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class FileUtils {
|
2019-10-25 13:59:29 +08:00
|
|
|
|
|
2020-05-19 11:04:53 +08:00
|
|
|
|
private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
|
|
|
|
|
|
|
|
|
|
private final String fileDir = ConfigConstants.getFileDir();
|
2019-10-25 13:59:29 +08:00
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
private final CacheService cacheService;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
public FileUtils(CacheService cacheService) {
|
|
|
|
|
this.cacheService = cacheService;
|
|
|
|
|
}
|
2019-04-08 17:50:13 +08:00
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
|
/**
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @return 已转换过的文件集合(缓存)
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public Map<String, String> listConvertedFiles() {
|
2019-04-08 17:50:13 +08:00
|
|
|
|
return cacheService.getPDFCache();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @return 已转换过的文件,根据文件名获取
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public String getConvertedFile(String key) {
|
2019-04-08 17:50:13 +08:00
|
|
|
|
return cacheService.getPDFCache(key);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 18:39:58 +08:00
|
|
|
|
/**
|
|
|
|
|
* @param key pdf本地路径
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @return 已将pdf转换成图片的图片本地相对路径
|
2019-04-25 18:39:58 +08:00
|
|
|
|
*/
|
|
|
|
|
public Integer getConvertedPdfImage(String key) {
|
|
|
|
|
return cacheService.getPdfImageCache(key);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* 查看文件类型(防止参数中存在.点号或者其他特殊字符,所以先抽取文件名,然后再获取文件类型)
|
|
|
|
|
*
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param url url
|
|
|
|
|
* @return 文件类型
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
2018-01-17 17:51:53 +08:00
|
|
|
|
public FileType typeFromUrl(String url) {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
String nonPramStr = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
|
2018-01-17 14:10:40 +08:00
|
|
|
|
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
|
2019-06-17 14:21:16 +08:00
|
|
|
|
return typeFromFileName(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private FileType typeFromFileName(String fileName) {
|
|
|
|
|
String[] simText = ConfigConstants.getSimText();
|
|
|
|
|
String[] media = ConfigConstants.getMedia();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
|
|
|
if (listPictureTypes().contains(fileType.toLowerCase())) {
|
2019-06-17 14:21:16 +08:00
|
|
|
|
return FileType.picture;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
if (listArchiveTypes().contains(fileType.toLowerCase())) {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
return FileType.compress;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
if (listOfficeTypes().contains(fileType.toLowerCase())) {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
return FileType.office;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
if (Arrays.asList(simText).contains(fileType.toLowerCase())) {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
return FileType.simText;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
2018-03-25 13:26:51 +08:00
|
|
|
|
if (Arrays.asList(media).contains(fileType.toLowerCase())) {
|
|
|
|
|
return FileType.media;
|
|
|
|
|
}
|
2019-11-21 17:01:14 +08:00
|
|
|
|
if ("pdf".equalsIgnoreCase(fileType)) {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
return FileType.pdf;
|
|
|
|
|
}
|
2019-11-21 17:01:14 +08:00
|
|
|
|
if ("dwg".equalsIgnoreCase(fileType)) {
|
|
|
|
|
return FileType.cad;
|
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
return FileType.other;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 从url中剥离出文件名
|
|
|
|
|
* @param url
|
|
|
|
|
* 格式如:http://keking.ufile.ucloud.com.cn/20171113164107_月度绩效表模板(新).xls?UCloudPublicKey=ucloudtangshd@weifenf.com14355492830001993909323&Expires=&Signature=I D1NOFtAJSPT16E6imv6JWuq0k=
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @return 文件名
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public String getFileNameFromURL(String url) {
|
|
|
|
|
// 因为url的参数中可能会存在/的情况,所以直接url.lastIndexOf("/")会有问题
|
|
|
|
|
// 所以先从?处将url截断,然后运用url.lastIndexOf("/")获取文件名
|
2020-05-15 18:09:19 +08:00
|
|
|
|
String noQueryUrl = url.substring(0, url.contains("?") ? url.indexOf("?"): url.length());
|
|
|
|
|
return noQueryUrl.substring(noQueryUrl.lastIndexOf("/") + 1);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* 从路径中获取文件负
|
2018-01-17 14:10:40 +08:00
|
|
|
|
* @param path
|
|
|
|
|
* 类似这种:C:\Users\yudian-it\Downloads
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @return 文件名
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public String getFileNameFromPath(String path) {
|
|
|
|
|
return path.substring(path.lastIndexOf(File.separator) + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<String> listPictureTypes(){
|
|
|
|
|
List<String> list = Lists.newArrayList();
|
|
|
|
|
list.add("jpg");
|
|
|
|
|
list.add("jpeg");
|
|
|
|
|
list.add("png");
|
|
|
|
|
list.add("gif");
|
|
|
|
|
list.add("bmp");
|
2018-01-17 17:51:53 +08:00
|
|
|
|
list.add("ico");
|
|
|
|
|
list.add("RAW");
|
2018-01-17 14:10:40 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<String> listArchiveTypes(){
|
|
|
|
|
List<String> list = Lists.newArrayList();
|
|
|
|
|
list.add("rar");
|
|
|
|
|
list.add("zip");
|
|
|
|
|
list.add("jar");
|
|
|
|
|
list.add("7-zip");
|
|
|
|
|
list.add("tar");
|
|
|
|
|
list.add("gzip");
|
|
|
|
|
list.add("7z");
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<String> listOfficeTypes() {
|
|
|
|
|
List<String> list = Lists.newArrayList();
|
|
|
|
|
list.add("docx");
|
|
|
|
|
list.add("doc");
|
|
|
|
|
list.add("xls");
|
|
|
|
|
list.add("xlsx");
|
|
|
|
|
list.add("ppt");
|
|
|
|
|
list.add("pptx");
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取相对路径
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param absolutePath 绝对路径
|
|
|
|
|
* @return 相对路径
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public String getRelativePath(String absolutePath) {
|
|
|
|
|
return absolutePath.substring(fileDir.length());
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
/**
|
|
|
|
|
* 添加转换后PDF缓存
|
|
|
|
|
* @param fileName pdf文件名
|
|
|
|
|
* @param value 缓存相对路径
|
|
|
|
|
*/
|
2018-01-17 14:10:40 +08:00
|
|
|
|
public void addConvertedFile(String fileName, String value){
|
2019-04-08 17:50:13 +08:00
|
|
|
|
cacheService.putPDFCache(fileName, value);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 18:39:58 +08:00
|
|
|
|
/**
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* 添加转换后图片组缓存
|
|
|
|
|
* @param pdfFilePath pdf文件绝对路径
|
|
|
|
|
* @param num 图片张数
|
2019-04-25 18:39:58 +08:00
|
|
|
|
*/
|
|
|
|
|
public void addConvertedPdfImage(String pdfFilePath, int num){
|
|
|
|
|
cacheService.putPdfImageCache(pdfFilePath, num);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取redis中压缩包内图片文件
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param fileKey fileKey
|
|
|
|
|
* @return 图片文件访问url列表
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
2020-05-15 18:09:19 +08:00
|
|
|
|
public List<String> getImgCache(String fileKey){
|
2019-04-08 17:50:13 +08:00
|
|
|
|
return cacheService.getImgCache(fileKey);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置redis中压缩包内图片文件
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param fileKey fileKey
|
|
|
|
|
* @param imgs 图片文件访问url列表
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
2020-05-15 18:09:19 +08:00
|
|
|
|
public void putImgCache(String fileKey,List<String> imgs){
|
2019-04-08 17:50:13 +08:00
|
|
|
|
cacheService.putImgCache(fileKey, imgs);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 判断文件编码格式
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param path 绝对路径
|
|
|
|
|
* @return 编码格式
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public String getFileEncodeUTFGBK(String path){
|
|
|
|
|
String enc = Charset.forName("GBK").name();
|
|
|
|
|
File file = new File(path);
|
2020-05-15 18:09:19 +08:00
|
|
|
|
InputStream in;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
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) {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
enc = StandardCharsets.UTF_8.name();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
System.out.println("文件编码格式为:" + enc);
|
|
|
|
|
return enc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 对转换后的文件进行操作(改变编码方式)
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param outFilePath 文件绝对路径
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public void doActionConvertedFile(String outFilePath) {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
try (InputStream inputStream = new FileInputStream(outFilePath);
|
2019-10-25 13:59:29 +08:00
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_CONVERTER_CHARSET))){
|
2018-01-17 14:10:40 +08:00
|
|
|
|
String line;
|
|
|
|
|
while(null != (line = reader.readLine())){
|
|
|
|
|
if (line.contains("charset=gb2312")) {
|
|
|
|
|
line = line.replace("charset=gb2312", "charset=utf-8");
|
|
|
|
|
}
|
|
|
|
|
sb.append(line);
|
|
|
|
|
}
|
|
|
|
|
// 添加sheet控制头
|
|
|
|
|
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>");
|
2020-05-13 19:40:31 +08:00
|
|
|
|
sb.append("<link rel=\"stylesheet\" href=\"bootstrap/css/bootstrap.min.css\">");
|
2018-01-17 14:10:40 +08:00
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
// 重新写入文件
|
|
|
|
|
try(FileOutputStream fos = new FileOutputStream(outFilePath);
|
2020-05-15 18:09:19 +08:00
|
|
|
|
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
|
2018-01-17 14:10:40 +08:00
|
|
|
|
writer.write(sb.toString());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取文件后缀
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param url url
|
|
|
|
|
* @return 文件后缀
|
2018-01-17 17:51:53 +08:00
|
|
|
|
*/
|
|
|
|
|
private String suffixFromUrl(String url) {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
String nonPramStr = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
|
2018-01-17 17:51:53 +08:00
|
|
|
|
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
|
2019-06-17 14:21:16 +08:00
|
|
|
|
return suffixFromFileName(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String suffixFromFileName(String fileName) {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 14:21:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取url中的参数
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param url url
|
|
|
|
|
* @param name 参数名
|
|
|
|
|
* @return 参数值
|
2019-06-17 14:21:16 +08:00
|
|
|
|
*/
|
2019-06-19 14:18:09 +08:00
|
|
|
|
public String getUrlParameterReg(String url, String name) {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
Map<String, String> mapRequest = new HashMap<>();
|
2019-06-19 14:18:09 +08:00
|
|
|
|
String strUrlParam = truncateUrlPage(url);
|
2019-06-17 14:21:16 +08:00
|
|
|
|
if (strUrlParam == null) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
//每个键值为一组
|
2020-05-15 18:09:19 +08:00
|
|
|
|
String[] arrSplit = strUrlParam.split("[&]");
|
|
|
|
|
for(String strSplit : arrSplit) {
|
|
|
|
|
String[] arrSplitEqual = strSplit.split("[=]");
|
2019-06-17 14:21:16 +08:00
|
|
|
|
//解析出键值
|
|
|
|
|
if(arrSplitEqual.length > 1) {
|
|
|
|
|
//正确解析
|
|
|
|
|
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
|
|
|
|
|
} else if (!arrSplitEqual[0].equals("")) {
|
|
|
|
|
//只有参数没有值,不加入
|
|
|
|
|
mapRequest.put(arrSplitEqual[0], "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mapRequest.get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 去掉url中的路径,留下请求参数部分
|
|
|
|
|
* @param strURL url地址
|
|
|
|
|
* @return url请求参数部分
|
|
|
|
|
*/
|
2019-06-19 14:18:09 +08:00
|
|
|
|
private String truncateUrlPage(String strURL) {
|
2019-06-17 14:21:16 +08:00
|
|
|
|
String strAllParam = null;
|
|
|
|
|
strURL = strURL.trim();
|
|
|
|
|
String[] arrSplit = strURL.split("[?]");
|
|
|
|
|
if(strURL.length() > 1) {
|
|
|
|
|
if(arrSplit.length > 1) {
|
|
|
|
|
if(arrSplit[1] != null) {
|
|
|
|
|
strAllParam=arrSplit[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return strAllParam;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取文件属性
|
|
|
|
|
* @param url url
|
|
|
|
|
* @return 文件属性
|
|
|
|
|
*/
|
2018-01-17 17:51:53 +08:00
|
|
|
|
public FileAttribute getFileAttribute(String url) {
|
2019-06-17 14:21:16 +08:00
|
|
|
|
String fileName;
|
|
|
|
|
FileType type;
|
|
|
|
|
String suffix;
|
2019-10-24 16:23:14 +08:00
|
|
|
|
String fullFileName = getUrlParameterReg(url, "fullfilename");
|
2019-06-17 14:21:16 +08:00
|
|
|
|
if (!StringUtils.isEmpty(fullFileName)) {
|
|
|
|
|
fileName = fullFileName;
|
|
|
|
|
type = typeFromFileName(fileName);
|
|
|
|
|
suffix = suffixFromFileName(fileName);
|
|
|
|
|
} else {
|
2019-10-24 16:23:14 +08:00
|
|
|
|
fileName = getFileNameFromURL(url);
|
2019-06-17 14:21:16 +08:00
|
|
|
|
type = typeFromUrl(url);
|
|
|
|
|
suffix = suffixFromUrl(url);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2020-05-15 18:09:19 +08:00
|
|
|
|
return new FileAttribute(type,suffix,fileName,url);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|