Skip to content

Commit d179823

Browse files
committed
UrlResource增加size方法
1 parent e8c1f7a commit d179823

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* 【core 】 ZipReader增加setMaxSizeDiff方法,自定义或关闭ZipBomb(issue#3018@Github)
1717
* 【db 】 Query.of(entity)构建时传入fields(issue#I7M5JU@Gitee)
1818
* 【db 】 clickhouse驱动名称变更为com.clickhouse.jdbc.ClickHouseDriver(issue#3224@Github)
19+
* 【core 】 UrlResource增加size方法(issue#3226@Github)
1920

2021
### 🐞Bug修复
2122
* 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题(issue#3162@Github)

hutool-core/src/main/java/cn/hutool/core/io/resource/UrlResource.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package cn.hutool.core.io.resource;
22

33
import cn.hutool.core.io.FileUtil;
4+
import cn.hutool.core.io.IORuntimeException;
45
import cn.hutool.core.util.ObjectUtil;
56
import cn.hutool.core.util.URLUtil;
67

7-
import java.io.File;
8-
import java.io.InputStream;
9-
import java.io.Serializable;
8+
import java.io.*;
109
import java.net.URI;
1110
import java.net.URL;
11+
import java.net.URLConnection;
1212

1313
/**
1414
* URL资源访问类
@@ -104,4 +104,14 @@ public File getFile(){
104104
public String toString() {
105105
return (null == this.url) ? "null" : this.url.toString();
106106
}
107+
108+
/**
109+
* 获取资源长度
110+
*
111+
* @return 资源长度
112+
* @since 5.8.21
113+
*/
114+
public long size() {
115+
return URLUtil.size(this.url);
116+
}
107117
}

hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,4 +776,51 @@ public static String getDataUri(String mimeType, Charset charset, String encodin
776776

777777
return builder.toString();
778778
}
779+
780+
/**
781+
* 获取URL对应数据长度
782+
* <ul>
783+
* <li>如果URL为文件,转换为文件获取文件长度。</li>
784+
* <li>其它情况获取{@link URLConnection#getContentLengthLong()}</li>
785+
* </ul>
786+
*
787+
* @param url URL
788+
* @return 长度
789+
* @since 6.0.0
790+
*/
791+
public static long size(final URL url) {
792+
if (URLUtil.isFileURL(url)) {
793+
// 如果资源以独立文件形式存在,尝试获取文件长度
794+
final File file = FileUtil.file(url);
795+
final long length = file.length();
796+
if (length == 0L && !file.exists()) {
797+
throw new IORuntimeException("File not exist or size is zero!");
798+
}
799+
return length;
800+
} else {
801+
// 如果资源打在jar包中或来自网络,使用网络请求长度
802+
// issue#3226, 来自Spring的AbstractFileResolvingResource
803+
try {
804+
final URLConnection con = url.openConnection();
805+
useCachesIfNecessary(con);
806+
if (con instanceof HttpURLConnection) {
807+
final HttpURLConnection httpCon = (HttpURLConnection) con;
808+
httpCon.setRequestMethod("HEAD");
809+
}
810+
return con.getContentLengthLong();
811+
} catch (final IOException e) {
812+
throw new IORuntimeException(e);
813+
}
814+
}
815+
}
816+
817+
/**
818+
* 如果连接为JNLP方式,则打开缓存
819+
*
820+
* @param con {@link URLConnection}
821+
* @since 6.0.0
822+
*/
823+
public static void useCachesIfNecessary(final URLConnection con) {
824+
con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));
825+
}
779826
}

0 commit comments

Comments
 (0)