mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package cn.hutool.http.client.body;
|
||||
|
||||
import cn.hutool.core.io.FastByteArrayOutputStream;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
@@ -29,4 +31,15 @@ public interface RequestBody {
|
||||
IoUtil.close(out);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取body资源流
|
||||
*
|
||||
* @return {@link InputStream}
|
||||
*/
|
||||
default InputStream getStream() {
|
||||
final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
|
||||
writeClose(out);
|
||||
return IoUtil.toStream(out);
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package cn.hutool.http.client.body;
|
||||
|
||||
import cn.hutool.core.io.resource.Resource;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
@@ -46,4 +47,9 @@ public class ResourceBody implements RequestBody {
|
||||
public void write(final OutputStream out) {
|
||||
resource.writeTo(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getStream() {
|
||||
return resource.getStream();
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,9 @@
|
||||
package cn.hutool.http.client.engine.httpclient4;
|
||||
|
||||
import cn.hutool.core.io.FastByteArrayOutputStream;
|
||||
import cn.hutool.http.client.body.BytesBody;
|
||||
import cn.hutool.http.client.body.RequestBody;
|
||||
import cn.hutool.http.client.body.ResourceBody;
|
||||
import org.apache.http.entity.AbstractHttpEntity;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -17,7 +14,7 @@ import java.nio.charset.Charset;
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class RequestBodyEntity extends AbstractHttpEntity {
|
||||
public class HttpClient4BodyEntity extends AbstractHttpEntity {
|
||||
|
||||
private final RequestBody body;
|
||||
|
||||
@@ -29,7 +26,7 @@ public class RequestBodyEntity extends AbstractHttpEntity {
|
||||
* @param chunked 是否块模式传输
|
||||
* @param body {@link RequestBody}
|
||||
*/
|
||||
public RequestBodyEntity(final String contentType, final Charset charset, final boolean chunked, final RequestBody body) {
|
||||
public HttpClient4BodyEntity(final String contentType, final Charset charset, final boolean chunked, final RequestBody body) {
|
||||
super();
|
||||
setContentType(contentType);
|
||||
setContentEncoding(null == charset ? null : charset.name());
|
||||
@@ -46,13 +43,7 @@ public class RequestBodyEntity extends AbstractHttpEntity {
|
||||
|
||||
@Override
|
||||
public InputStream getContent() {
|
||||
if (body instanceof ResourceBody) {
|
||||
return ((ResourceBody) body).getResource().getStream();
|
||||
} else {
|
||||
final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
|
||||
body.writeClose(out);
|
||||
return new ByteArrayInputStream(out.toByteArray());
|
||||
}
|
||||
return body.getStream();
|
||||
}
|
||||
|
||||
@Override
|
@@ -88,7 +88,7 @@ public class HttpClient4Engine implements ClientEngine {
|
||||
|
||||
// 填充自定义消息体
|
||||
final RequestBody body = message.body();
|
||||
request.setEntity(new RequestBodyEntity(
|
||||
request.setEntity(new HttpClient4BodyEntity(
|
||||
// 用户自定义的内容类型
|
||||
message.header(cn.hutool.http.meta.Header.CONTENT_TYPE),
|
||||
// 用户自定义编码
|
||||
|
@@ -1,12 +1,9 @@
|
||||
package cn.hutool.http.client.engine.httpclient5;
|
||||
|
||||
import cn.hutool.core.io.FastByteArrayOutputStream;
|
||||
import cn.hutool.http.client.body.BytesBody;
|
||||
import cn.hutool.http.client.body.RequestBody;
|
||||
import cn.hutool.http.client.body.ResourceBody;
|
||||
import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -18,7 +15,7 @@ import java.nio.charset.Charset;
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class RequestBodyEntity extends AbstractHttpEntity {
|
||||
public class HttpClient5BodyEntity extends AbstractHttpEntity {
|
||||
|
||||
private final RequestBody body;
|
||||
|
||||
@@ -30,7 +27,7 @@ public class RequestBodyEntity extends AbstractHttpEntity {
|
||||
* @param chunked 是否块模式传输
|
||||
* @param body {@link RequestBody}
|
||||
*/
|
||||
public RequestBodyEntity(final String contentType, final Charset charset, final boolean chunked, final RequestBody body) {
|
||||
public HttpClient5BodyEntity(final String contentType, final Charset charset, final boolean chunked, final RequestBody body) {
|
||||
super(contentType, null == charset ? null : charset.name(), chunked);
|
||||
this.body = body;
|
||||
}
|
||||
@@ -44,13 +41,7 @@ public class RequestBodyEntity extends AbstractHttpEntity {
|
||||
|
||||
@Override
|
||||
public InputStream getContent() {
|
||||
if (body instanceof ResourceBody) {
|
||||
return ((ResourceBody) body).getResource().getStream();
|
||||
} else {
|
||||
final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
|
||||
body.writeClose(out);
|
||||
return new ByteArrayInputStream(out.toByteArray());
|
||||
}
|
||||
return body.getStream();
|
||||
}
|
||||
|
||||
@Override
|
@@ -84,7 +84,7 @@ public class HttpClient5Engine implements ClientEngine {
|
||||
|
||||
// 填充自定义消息体
|
||||
final RequestBody body = message.body();
|
||||
request.setEntity(new RequestBodyEntity(
|
||||
request.setEntity(new HttpClient5BodyEntity(
|
||||
// 用户自定义的内容类型
|
||||
message.header(cn.hutool.http.meta.Header.CONTENT_TYPE),
|
||||
// 用户自定义编码
|
||||
|
Reference in New Issue
Block a user