From 36139a0c275e87182ee80de5836f8e45fed6c5d6 Mon Sep 17 00:00:00 2001 From: ltai701 Date: Sun, 8 Jun 2025 20:50:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E5=BC=80=E5=A7=8B=E5=B0=B1=E5=AE=8C?= =?UTF-8?q?=E6=88=90factory=E7=9A=84=E5=9F=BA=E6=9C=AC=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=EF=BC=8C=E9=81=BF=E5=85=8D=E6=AF=8F=E6=AC=A1?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E9=87=8D=E5=A4=8D=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/keking/utils/DownloadUtils.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/cn/keking/utils/DownloadUtils.java b/server/src/main/java/cn/keking/utils/DownloadUtils.java index 5f8914bfb..0befc61ce 100644 --- a/server/src/main/java/cn/keking/utils/DownloadUtils.java +++ b/server/src/main/java/cn/keking/utils/DownloadUtils.java @@ -39,9 +39,18 @@ public class DownloadUtils { private static final String URL_PARAM_FTP_USERNAME = "ftp.username"; private static final String URL_PARAM_FTP_PASSWORD = "ftp.password"; private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding"; - private static final RestTemplate restTemplate = new RestTemplate(); + // private static final RestTemplate restTemplate = new RestTemplate(); private static final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); private static final ObjectMapper mapper = new ObjectMapper(); + + /** + * 一开始就完成factory的基本参数设置,避免每次调用重复设置 + */ + static { + factory.setConnectionRequestTimeout(2000); //设置超时时间 + factory.setConnectTimeout(10000); + factory.setReadTimeout(72000); + } /** @@ -49,7 +58,8 @@ public class DownloadUtils { * @param fileName 文件名 * @return 本地文件绝对路径 */ - public static ReturnResponse downLoad(FileAttribute fileAttribute, String fileName) { + @SuppressWarnings("unchecked") + public static ReturnResponse downLoad(FileAttribute fileAttribute, String fileName) { // 忽略ssl证书 String urlStr = null; try { @@ -90,11 +100,9 @@ public static ReturnResponse downLoad(FileAttribute fileAttribute, Strin if (!fileAttribute.getSkipDownLoad()) { if (isHttpUrl(url)) { File realFile = new File(realPath); - factory.setConnectionRequestTimeout(2000); //设置超时时间 - factory.setConnectTimeout(10000); - factory.setReadTimeout(72000); HttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new DefaultRedirectStrategy()).build(); factory.setHttpClient(httpClient); //加入重定向方法 + RestTemplate restTemplate = new RestTemplate(); restTemplate.setRequestFactory(factory); RequestCallback requestCallback = request -> { request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));