From 75fe6824336cda7d4db6d05efd2feb0bcedb73ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=9B=84?= Date: Fri, 28 Jul 2023 06:36:23 +0000 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=96=B9=E6=B3=95=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=87=8D=E5=AE=9A=E5=90=91=20=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=96=B9=E6=B3=95=20=E6=94=AF=E6=8C=81=E9=87=8D?= =?UTF-8?q?=E5=AE=9A=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 高雄 --- .../java/cn/keking/utils/DownloadUtils.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/server/src/main/java/cn/keking/utils/DownloadUtils.java b/server/src/main/java/cn/keking/utils/DownloadUtils.java index 3d67b07f..15af9ee8 100644 --- a/server/src/main/java/cn/keking/utils/DownloadUtils.java +++ b/server/src/main/java/cn/keking/utils/DownloadUtils.java @@ -12,7 +12,10 @@ 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; @@ -37,6 +40,7 @@ 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"); @@ -70,6 +74,50 @@ public class DownloadUtils { } 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);