From 692bb8f9641fdd742277eeac65452754a44864b0 Mon Sep 17 00:00:00 2001 From: kl <632104866@QQ.com> Date: Thu, 31 Aug 2023 10:47:41 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=B8=8B=E8=BD=BD=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=87=8D=E6=96=B0=E4=B8=8B=E8=BD=BD=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#485)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 重构下载文件的代码,修复文件重新下载的问题 * fix: 修复可能得空指针问题 --- .../java/cn/keking/utils/DownloadUtils.java | 73 ++----------------- .../java/cn/keking/utils/KkFileUtils.java | 36 ++++++--- 2 files changed, 33 insertions(+), 76 deletions(-) diff --git a/server/src/main/java/cn/keking/utils/DownloadUtils.java b/server/src/main/java/cn/keking/utils/DownloadUtils.java index 58f2da7d..a5d93f75 100644 --- a/server/src/main/java/cn/keking/utils/DownloadUtils.java +++ b/server/src/main/java/cn/keking/utils/DownloadUtils.java @@ -7,15 +7,11 @@ 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 java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLDecoder; import java.util.UUID; import static cn.keking.utils.KkFileUtils.isFtpUrl; @@ -40,7 +36,6 @@ public class DownloadUtils { public static ReturnResponse downLoad(FileAttribute fileAttribute, String fileName) { // 忽略ssl证书 String urlStr = null; - HttpURLConnection urlcon; try { SslUtils.ignoreSsl(); urlStr = fileAttribute.getUrl().replaceAll("\\+", "%20"); @@ -49,7 +44,9 @@ public class DownloadUtils { } ReturnResponse response = new ReturnResponse<>(0, "下载成功!!!", ""); String realPath = getRelFilePath(fileName, fileAttribute); - if(!StringUtils.hasText(realPath)){ + + // 判断是否非法地址 + if (KkFileUtils.isIllegalFileName(realPath)) { response.setCode(1); response.setContent(null); response.setMsg("下载失败:文件名不合法!" + urlStr); @@ -67,57 +64,14 @@ public class DownloadUtils { response.setMsg(fileName); return response; } - if(realPath.equals("cunzhai")){ - response.setContent(fileDir + fileName); + // 如果文件是否已经存在、且不强制更新,则直接返回文件路径 + if (KkFileUtils.isExist(realPath) && !fileAttribute.forceUpdatedCache()) { + response.setContent(realPath); response.setMsg(fileName); return response; } try { URL url = WebUtils.normalizedURL(urlStr); - if (!urlStr.toLowerCase().startsWith("ftp:")&& !urlStr.toLowerCase().startsWith("file")){ - urlcon=(HttpURLConnection)url.openConnection(); - urlcon.setConnectTimeout(30000); - urlcon.setReadTimeout(30000); - urlcon.setInstanceFollowRedirects(false); - int responseCode = urlcon.getResponseCode(); - if(responseCode != 200){ - if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { //301 302 - url =new URL(urlcon.getHeaderField("Location")); - } - if (responseCode == 403|| responseCode == 500) { //301 302 - response.setCode(1); - response.setContent(null); - response.setMsg("下载失败:地址错误!" + urlStr); - return response; - } - if (responseCode == 404 ) { //404 - try { - urlStr = URLDecoder.decode(urlStr, "UTF-8"); - urlStr = URLDecoder.decode(urlStr, "UTF-8"); - url = WebUtils.normalizedURL(urlStr); - urlcon=(HttpURLConnection)url.openConnection(); - urlcon.setConnectTimeout(30000); - urlcon.setReadTimeout(30000); - urlcon.setInstanceFollowRedirects(false); - responseCode = urlcon.getResponseCode(); - if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { //301 302 - url =new URL(urlcon.getHeaderField("Location")); - } - if(responseCode == 404 ||responseCode == 403|| responseCode == 500 ){ - response.setCode(1); - response.setContent(null); - response.setMsg("下载失败:地址错误!" + urlStr); - return response; - } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - }finally { - assert urlcon != null; - urlcon.disconnect(); - } - } - } - } if (!fileAttribute.getSkipDownLoad()) { if (isHttpUrl(url)) { File realFile = new File(realPath); @@ -163,25 +117,12 @@ 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); } - Boolean forceUpdatedCache = fileAttribute.forceUpdatedCache(); - //判断是否启用强制更新功能如果启用 文件必须重新下载 - if (null == forceUpdatedCache || !forceUpdatedCache) { - // 文件已在本地存在,跳过文件下载 - File realFile = new File(realPath); - if (realFile.exists()) { - fileAttribute.setSkipDownLoad(true); - return "cunzhai"; //这里给的值是不能修改的 对应的是下载方法里面有个强制输出地址的 - } - } return realPath; } diff --git a/server/src/main/java/cn/keking/utils/KkFileUtils.java b/server/src/main/java/cn/keking/utils/KkFileUtils.java index 8db41c9c..48ae8928 100644 --- a/server/src/main/java/cn/keking/utils/KkFileUtils.java +++ b/server/src/main/java/cn/keking/utils/KkFileUtils.java @@ -34,29 +34,33 @@ public class KkFileUtils { /** * 检查文件名是否合规 + * * @param fileName 文件名 - * @return 合规结果,true:不合规,false:合规 + * @return 合规结果, true:不合规,false:合规 */ - public static boolean isIllegalFileName(String fileName){ - for (String str: illegalFileStrList){ - if(fileName.contains(str)){ + public static boolean isIllegalFileName(String fileName) { + for (String str : illegalFileStrList) { + if (fileName.contains(str)) { return true; } } return false; } + /** * 检查是否是数字 + * * @param str 文件名 - * @return 合规结果,true:不合规,false:合规 + * @return 合规结果, true:不合规,false:合规 */ public static boolean isInteger(String str) { - if(StringUtils.hasText(str)){ + if (StringUtils.hasText(str)) { boolean strResult = str.matches("-?[0-9]+.?[0-9]*"); - return strResult ; + return strResult; } return false; } + /** * 判断url是否是http资源 * @@ -102,7 +106,7 @@ public class KkFileUtils { public static String htmlEscape(String input) { - if(StringUtils.hasText(input)){ + if (StringUtils.hasText(input)) { //input = input.replaceAll("\\{", "%7B").replaceAll("}", "%7D").replaceAll("\\\\", "%5C"); String htmlStr = HtmlUtils.htmlEscape(input, "UTF-8"); //& -> & @@ -186,11 +190,23 @@ public class KkFileUtils { */ public static boolean isAllowedUpload(String file) { String fileType = suffixFromFileName(file); - for (String type : ConfigConstants.getProhibit()) { - if (type.equals(fileType)) + for (String type : ConfigConstants.getProhibit()) { + if (type.equals(fileType)){ return false; + } } return !ObjectUtils.isEmpty(fileType); } + /** + * 判断文件是否存在 + * + * @param filePath 文件路径 + * @return 是否存在 true:存在,false:不存在 + */ + public static boolean isExist(String filePath) { + File file = new File(filePath); + return file.exists(); + } + }