修复VersionComparator传入空字符串报错问题

This commit is contained in:
Looly
2024-06-12 00:34:16 +08:00
parent 59dc7dec39
commit 3a78b7d461
4 changed files with 93 additions and 19 deletions

View File

@@ -15,6 +15,7 @@ package org.dromara.hutool.http.client;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.http.HttpUtil;
@@ -173,6 +174,44 @@ public interface HeaderOperation<T extends HeaderOperation<T>> {
return (T) this;
}
/**
* 获取内容长度,以下情况长度无效:
* <ul>
* <li>Transfer-Encoding: Chunked</li>
* <li>Content-Encoding: XXX</li>
* </ul>
*
* @return 长度,-1表示服务端未返回或长度无效
* @since 5.7.9
*/
default long contentLength() {
long contentLength = Convert.toLong(header(HeaderName.CONTENT_LENGTH), -1L);
if (contentLength > 0 && (isChunked() || StrUtil.isNotBlank(contentEncoding()))) {
//按照HTTP协议规范在 Transfer-Encoding和Content-Encoding设置后 Content-Length 无效。
contentLength = -1;
}
return contentLength;
}
/**
* 是否为Transfer-Encoding:Chunked的内容
*
* @return 是否为Transfer-Encoding:Chunked的内容
* @since 4.6.2
*/
default boolean isChunked() {
return "Chunked".equalsIgnoreCase(header(HeaderName.TRANSFER_ENCODING));
}
/**
* 获取内容编码
*
* @return String
*/
default String contentEncoding() {
return header(HeaderName.CONTENT_ENCODING);
}
// endregion ----------------------------------------------------------- headers
// region ----------------------------------------------------------- auth

View File

@@ -203,6 +203,7 @@ public class JdkHttpConnection implements HeaderOperation<JdkHttpConnection>, Cl
* @param timeout 超时时间
* @return this
*/
@SuppressWarnings("resource")
public JdkHttpConnection setConnectionAndReadTimeout(final int timeout) {
setConnectTimeout(timeout);
setReadTimeout(timeout);
@@ -233,6 +234,22 @@ public class JdkHttpConnection implements HeaderOperation<JdkHttpConnection>, Cl
return this;
}
/**
* 设置固定长度的流模式会设置HTTP请求头中的Content-Length字段告知服务器整个请求体的精确字节大小。<br>
* 这在上传文件或大数据量时非常有用,因为它允许服务器准确地知道何时接收完所有的请求数据,而不需要依赖于连接的关闭来判断数据传输的结束。
*
* @param contentLength 请求体的长度。如果内容长度大于0则启用固定长度流模式。
* @return this
*/
public JdkHttpConnection setFixedLengthStreamingMode(final long contentLength) {
// 当内容长度大于0时启用固定长度流模式
if (contentLength > 0) {
conn.setFixedLengthStreamingMode(contentLength);
}
// 返回当前实例,支持方法链式调用
return this;
}
/**
* 采用流方式上传数据,无需本地缓存数据。<br>
* HttpUrlConnection默认是将所有数据读到本地缓存然后再发送给服务器这样上传大文件时就会导致内存溢出。