diff --git a/CHANGELOG.md b/CHANGELOG.md
index f0171b913..3cd927feb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@
* 【json】 添加自定义序列化反序列化支持(issue#I1052A@Gitee)
* 【dfa】 优化特殊字符构建,优化查找,改为使用StrBuilder
* 【core】 ZipUtil增加FileFilter参数的重载,支持文件过滤(issue#I11RTP@Gitee)
+* 【http】 HttpRequest增加setChunkedStreamingMode方法(issue#525@Github)
### Bug修复
* 【core】 修复NetUtil.getUsableLocalPort问题(pr#69@Gitee)
diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java b/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java
index feb8b0dae..a1c5a80c3 100644
--- a/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java
@@ -349,11 +349,13 @@ public class HttpConnection {
* 采用流方式上传数据,无需本地缓存数据。
* HttpUrlConnection默认是将所有数据读到本地缓存,然后再发送给服务器,这样上传大文件时就会导致内存溢出。
*
- * @param blockSize 块大小(bytes数)
+ * @param blockSize 块大小(bytes数),0或小于0表示不设置Chuncked模式
* @return this
*/
public HttpConnection setChunkedStreamingMode(int blockSize) {
- conn.setChunkedStreamingMode(blockSize);
+ if(blockSize > 0) {
+ conn.setChunkedStreamingMode(blockSize);
+ }
return this;
}
diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java
index 399769d6b..5d1077bf6 100644
--- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java
@@ -123,6 +123,8 @@ public class HttpRequest extends HttpBase {
private int redirectCount;
/** 最大重定向次数 */
private int maxRedirectCount;
+ /** Chuncked块大小,0或小于0表示不设置Chuncked模式 */
+ private int blockSize;
/** 代理 */
private Proxy proxy;
@@ -857,6 +859,19 @@ public class HttpRequest extends HttpBase {
this.isRest = isRest;
return this;
}
+
+ /**
+ * 采用流方式上传数据,无需本地缓存数据。
+ * HttpUrlConnection默认是将所有数据读到本地缓存,然后再发送给服务器,这样上传大文件时就会导致内存溢出。
+ *
+ * @param blockSize 块大小(bytes数),0或小于0表示不设置Chuncked模式
+ * @return this
+ * @since 4.6.5
+ */
+ public HttpRequest setChunkedStreamingMode(int blockSize) {
+ this.blockSize = blockSize;
+ return this;
+ }
/**
* 执行Reuqest请求
@@ -946,6 +961,8 @@ public class HttpRequest extends HttpBase {
.setCookie(this.cookie)
// 定义转发
.setInstanceFollowRedirects(this.maxRedirectCount > 0 ? true : false)
+ // 流方式上传数据
+ .setChunkedStreamingMode(this.blockSize)
// 覆盖默认Header
.header(this.headers, true);