From af1c88a22010ebb444e7e2378e530df014e3f878 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 21 Aug 2021 20:14:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=9B=BE=E7=89=87=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=BF=9E=E6=8E=A5=E5=92=8C=E8=AF=BB=E5=8F=96=E7=9A=84?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在默认情况下,如果连接成功建立,但是下载速度极慢,在 Spring Web 应用中同步执行 Excel 的导出任务,会造成线程长时间被占用,最终造成 Tomcat 连接数超限,对应用造成毁灭性灾难。 --- .../alibaba/excel/converters/url/UrlImageConverter.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/excel/converters/url/UrlImageConverter.java b/src/main/java/com/alibaba/excel/converters/url/UrlImageConverter.java index b622d66d..d178a5e9 100644 --- a/src/main/java/com/alibaba/excel/converters/url/UrlImageConverter.java +++ b/src/main/java/com/alibaba/excel/converters/url/UrlImageConverter.java @@ -3,6 +3,7 @@ package com.alibaba.excel.converters.url; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.net.URLConnection; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; @@ -39,8 +40,12 @@ public class UrlImageConverter implements Converter { GlobalConfiguration globalConfiguration) throws IOException { InputStream inputStream = null; try { - inputStream = value.openStream(); - byte[] bytes = IoUtils.toByteArray(inputStream); + + URLConnection conn = value.openConnection(); + conn.setConnectTimeout(1000); + conn.setReadTimeout(5000); + inputStream = con.getInputStream(); + byte[] bytes = IoUtils.toByteArray(inputStream); return new CellData(bytes); } finally { if (inputStream != null) {