diff --git a/CHANGELOG.md b/CHANGELOG.md index 84b31c3a4..5262c1d99 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.33(2024-09-11) +# 5.8.33(2024-09-13) ### 🐣新特性 * 【core 】 SyncFinisher增加setExecutorService方法(issue#IANKQ1@Gitee) @@ -11,6 +11,7 @@ * 【crypto 】 SM2解密时,兼容GmSSL非压缩省略的04头的密文(issue#IAP1QJ@Gitee) * 【core 】 兼容NumberUtil.add方法传入整型自动类型转换为浮点类型的精度丢失问题(pr#3721@Github) * 【core 】 ModifierUtil明确注释,并增加hasAllModifiers方法(issue#IAQ2U0@Gitee) +* 【http 】 HttpRequest增加setFixedLengthStreamingMode方法(issue#3462@Github) ### 🐞Bug修复 * 【json 】 修复JSONConfig.setDateFormat设置后toBean无效问题(issue#3713@Github) 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 08744f359..003e918cc 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java @@ -351,6 +351,20 @@ public class HttpConnection { return this; } + /** + * 设置固定长度流模式,默认为0,表示不设置固定长度流模式,即默认为按需发送数据长度。
+ * 当发送大文件时,如果每次发送的数据长度不一致,可能造成接收方无法正常接收,此时可以设置固定长度流模式,此时发送的数据长度为contentLength,不足部分用0补齐。 + * + * @param contentLength 固定长度 + * @return this + */ + public HttpConnection setFixedLengthStreamingMode(long contentLength){ + if(contentLength > 0){ + conn.setFixedLengthStreamingMode(contentLength); + } + return this; + } + /** * 采用流方式上传数据,无需本地缓存数据。
* HttpUrlConnection默认是将所有数据读到本地缓存,然后再发送给服务器,这样上传大文件时就会导致内存溢出。 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 b9e573630..e94a51dab 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -350,6 +350,19 @@ public class HttpRequest extends HttpBase { return this.httpConnection; } + /** + * 设置固定长度流模式,默认为0,表示不设置固定长度流模式,即默认为按需发送数据长度。
+ * 当发送大文件时,如果每次发送的数据长度不一致,可能造成接收方无法正常接收,此时可以设置固定长度流模式,此时发送的数据长度为contentLength,不足部分用0补齐。 + * + * @param contentLength 固定长度 + * @return this + * @since 5.8.33 + */ + public HttpRequest setFixedLengthStreamingMode(long contentLength){ + this.httpConnection.setFixedLengthStreamingMode(contentLength); + return this; + } + /** * 设置请求方法 *