Browse Source

下载图片添加连接和读取的超时设置

在默认情况下,如果连接成功建立,但是下载速度极慢,在 Spring Web 应用中同步执行 Excel 的导出任务,会造成线程长时间被占用,最终造成 Tomcat 连接数超限,对应用造成毁灭性灾难。
pull/2046/head
James 3 years ago committed by GitHub
parent
commit
af1c88a220
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      src/main/java/com/alibaba/excel/converters/url/UrlImageConverter.java

7
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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
@ -39,7 +40,11 @@ public class UrlImageConverter implements Converter<URL> {
GlobalConfiguration globalConfiguration) throws IOException { GlobalConfiguration globalConfiguration) throws IOException {
InputStream inputStream = null; InputStream inputStream = null;
try { try {
inputStream = value.openStream();
URLConnection conn = value.openConnection();
conn.setConnectTimeout(1000);
conn.setReadTimeout(5000);
inputStream = con.getInputStream();
byte[] bytes = IoUtils.toByteArray(inputStream); byte[] bytes = IoUtils.toByteArray(inputStream);
return new CellData(bytes); return new CellData(bytes);
} finally { } finally {

Loading…
Cancel
Save