1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错 2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式 Co-authored-by: gaoxiongzaq <admin@cxcp.com>
This commit is contained in:
@ -3,10 +3,13 @@ package cn.keking.utils;
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.model.ReturnResponse;
|
||||
import cn.keking.service.FileHandlerService;
|
||||
import io.mola.galimatias.GalimatiasParseException;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
@ -35,6 +38,12 @@ public class DownloadUtils {
|
||||
String urlStr = fileAttribute.getUrl().replaceAll("\\+", "%20");
|
||||
ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
|
||||
String realPath = DownloadUtils.getRelFilePath(fileName, fileAttribute);
|
||||
if(!StringUtils.hasText(realPath)){
|
||||
response.setCode(1);
|
||||
response.setContent(null);
|
||||
response.setMsg("下载失败:文件名不合法!" + urlStr);
|
||||
return response;
|
||||
}
|
||||
try {
|
||||
URL url = WebUtils.normalizedURL(urlStr);
|
||||
if (!fileAttribute.getSkipDownLoad()) {
|
||||
@ -82,18 +91,20 @@ public class DownloadUtils {
|
||||
} else { // 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】)
|
||||
fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
|
||||
}
|
||||
// 判断是否非法地址
|
||||
if (KkFileUtils.isIllegalFileName(fileName)) {
|
||||
return null;
|
||||
}
|
||||
String realPath = fileDir + fileName;
|
||||
File dirFile = new File(fileDir);
|
||||
if (!dirFile.exists() && !dirFile.mkdirs()) {
|
||||
logger.error("创建目录【{}】失败,可能是权限不够,请检查", fileDir);
|
||||
}
|
||||
|
||||
// 文件已在本地存在,跳过文件下载
|
||||
File realFile = new File(realPath);
|
||||
if (realFile.exists()) {
|
||||
fileAttribute.setSkipDownLoad(true);
|
||||
}
|
||||
|
||||
return realPath;
|
||||
}
|
||||
|
||||
|
||||
@ -1,13 +1,11 @@
|
||||
package cn.keking.utils;
|
||||
|
||||
import cpdetector.CharsetPrinter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -45,7 +43,18 @@ public class KkFileUtils {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否是数字
|
||||
* @param str 文件名
|
||||
* @return 合规结果,true:不合规,false:合规
|
||||
*/
|
||||
public static boolean isInteger(String str) {
|
||||
if(StringUtils.hasText(str)){
|
||||
boolean strResult = str.matches("-?[0-9]+.?[0-9]*");
|
||||
return strResult ;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* 判断url是否是http资源
|
||||
*
|
||||
@ -89,41 +98,16 @@ public class KkFileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测文件编码格式
|
||||
*
|
||||
* @param filePath 绝对路径
|
||||
* @return 编码格式
|
||||
*/
|
||||
public static String getFileEncode(String filePath) {
|
||||
return getFileEncode(new File(filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测文件编码格式
|
||||
*
|
||||
* @param file 检测的文件
|
||||
* @return 编码格式
|
||||
*/
|
||||
public static String getFileEncode(File file) {
|
||||
CharsetPrinter cp = new CharsetPrinter();
|
||||
try {
|
||||
String encoding = cp.guessEncoding(file);
|
||||
LOGGER.info("检测到文件【{}】编码: {}", file.getAbsolutePath(), encoding);
|
||||
return encoding;
|
||||
} catch (IOException e) {
|
||||
LOGGER.warn("文件编码获取失败,采用默认的编码格式:UTF-8", e);
|
||||
return DEFAULT_FILE_ENCODING;
|
||||
}
|
||||
}
|
||||
|
||||
public static String htmlEscape(String input) {
|
||||
if(StringUtils.hasText(input)){
|
||||
//input = input.replaceAll("\\{", "%7B").replaceAll("}", "%7D").replaceAll("\\\\", "%5C");
|
||||
return HtmlUtils.htmlEscape(input);
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过文件名获取文件后缀
|
||||
*
|
||||
|
||||
@ -8,12 +8,13 @@ import javax.servlet.ServletRequest;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author : kl
|
||||
@ -173,7 +174,23 @@ public class WebUtils {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断地址是否正确
|
||||
* 高 2022/12/17
|
||||
*/
|
||||
public static boolean hefaurl (String url) {
|
||||
String regStr = "^((https|http|ftp|rtsp|mms|file)://)";//[.?*]表示匹配的就是本身
|
||||
Pattern pattern = Pattern.compile(regStr);
|
||||
Matcher matcher = pattern.matcher(url);
|
||||
return matcher.find();
|
||||
}
|
||||
public static boolean kuayu(String host, String wjl) { //查询域名是否相同
|
||||
if (wjl.contains(host)) {
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 将 Base64 字符串解码,再解码URL参数, 默认使用 UTF-8
|
||||
* @param source 原始 Base64 字符串
|
||||
@ -183,11 +200,10 @@ public class WebUtils {
|
||||
*/
|
||||
public static String decodeUrl(String source) {
|
||||
String url = decodeBase64String(source, StandardCharsets.UTF_8);
|
||||
try {
|
||||
url = URLDecoder.decode(url, StandardCharsets.UTF_8.name());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
if (! StringUtils.isNotBlank(url)){
|
||||
return null;
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
@ -203,9 +219,13 @@ public class WebUtils {
|
||||
* 有些 Base64 实现可能每 76 个字符插入换行符,也一并去掉
|
||||
* https://github.com/kekingcn/kkFileView/pull/340
|
||||
*/
|
||||
return new String(Base64Utils.decodeFromString(
|
||||
source.replaceAll(" ", "+").replaceAll("\n", "")
|
||||
), charsets);
|
||||
try {
|
||||
return new String(Base64Utils.decodeFromString(source.replaceAll(" ", "+").replaceAll("\n", "")), charsets);
|
||||
} catch (Exception e) {
|
||||
System.out.println("接入方法错误,或者未使用BASE64");
|
||||
// e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user