- java.lang.Object
-
- java.net.http.HttpRequest.BodyPublishers
-
- Enclosing class:
- HttpRequest
public static class HttpRequest.BodyPublishers extends Object
实现各种有用的发布者的BodyPublisher的实现,例如从String或文件发布请求主体。以下是使用预定义的主体发布者将常见的高级Java对象转换为适合作为请求主体发送的数据流的示例:
// Request body from a String HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://foo.com/")) .header("Content-Type", "text/plain; charset=UTF-8") .POST(BodyPublishers.ofString("some body text")) .build(); // Request body from a File HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://foo.com/")) .header("Content-Type", "application/json") .POST(BodyPublishers.ofFile(Paths.get("file.json"))) .build(); // Request body from a byte array HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://foo.com/")) .POST(BodyPublishers.ofByteArray(new byte[] { ... })) .build();- 从以下版本开始:
- 11
-
-
方法摘要
所有方法 静态方法 具体的方法 变量和类型 方法 描述 static HttpRequest.BodyPublisherfromPublisher(Flow.Publisher<? extends ByteBuffer> publisher)返回从给定的Flow.Publisher检索其正文的请求正文发布Flow.Publisher。static HttpRequest.BodyPublisherfromPublisher(Flow.Publisher<? extends ByteBuffer> publisher, long contentLength)返回从给定的Flow.Publisher检索其正文的请求正文发布Flow.Publisher。static HttpRequest.BodyPublishernoBody()请求正文发布者,不发送请求正文。static HttpRequest.BodyPublisherofByteArray(byte[] buf)返回一个请求正文发布者,其主体是给定的字节数组。static HttpRequest.BodyPublisherofByteArray(byte[] buf, int offset, int length)返回一个请求主体发布者,其主体是从指定的offset开始的给定字节数组length字节的offset。static HttpRequest.BodyPublisherofByteArrays(Iterable<byte[]> iter)请求正文发布者,它从Iterable的字节数组中获取数据。static HttpRequest.BodyPublisherofFile(Path path)请求正文发布者,它从文件的内容中获取数据。static HttpRequest.BodyPublisherofInputStream(Supplier<? extends InputStream> streamSupplier)请求正文发布者,它从InputStream读取其数据。static HttpRequest.BodyPublisherofString(String body)返回一个请求正文发布者,其正文为给定的String,使用UTF_8字符集进行转换。static HttpRequest.BodyPublisherofString(String s, Charset charset)返回一个请求正文发布者,其主体是给定的String,使用给定的字符集进行转换。
-
-
-
方法详细信息
-
fromPublisher
public static HttpRequest.BodyPublisher fromPublisher(Flow.Publisher<? extends ByteBuffer> publisher)
返回从给定的Flow.Publisher检索其正文的请求正文发布Flow.Publisher。 返回的请求正文发布者具有未知的内容长度。- API Note:
-
此方法可用作
BodyPublisher和Flow.Publisher之间的适配器,其中发布者将发布的请求正文的数量未知。 - 参数
-
publisher- 负责发布正文的出版商 - 结果
- 一个BodyPublisher
-
fromPublisher
public static HttpRequest.BodyPublisher fromPublisher(Flow.Publisher<? extends ByteBuffer> publisher, long contentLength)
返回从给定的Flow.Publisher检索其正文的请求正文发布Flow.Publisher。 返回的请求正文发布者具有给定的内容长度。给定的
contentLength是一个正数,表示publisher必须发布的确切字节publisher。- API Note:
-
此方法可用作
BodyPublisher和Flow.Publisher之间的适配器,其中发布者将发布的请求正文的数量是已知的。 - 参数
-
publisher- 负责发布正文的出版商 -
contentLength- 表示发布者将发布的确切字节数的正数 - 结果
- 一个BodyPublisher
- 异常
-
IllegalArgumentException- 如果内容长度为非正数
-
ofString
public static HttpRequest.BodyPublisher ofString(String body)
返回一个请求正文发布者,其正文为给定的String,使用UTF_8字符集进行转换。- 参数
-
body- 包含正文的String - 结果
- 一个BodyPublisher
-
ofString
public static HttpRequest.BodyPublisher ofString(String s, Charset charset)
返回一个请求正文发布者,其主体是给定的String,使用给定的字符集进行转换。- 参数
-
s- 包含正文的String -
charset- 将字符串转换为字节的字符集 - 结果
- 一个BodyPublisher
-
ofInputStream
public static HttpRequest.BodyPublisher ofInputStream(Supplier<? extends InputStream> streamSupplier)
请求正文发布者,它从InputStream读取其数据。Supplier的InputStream用于需要重复请求的情况,因为内容未被缓冲。Supplier可能会在后续尝试时返回null,在这种情况下请求失败。- 参数
-
streamSupplier- 开放式InputStreams的供应商 - 结果
- 一个BodyPublisher
-
ofByteArray
public static HttpRequest.BodyPublisher ofByteArray(byte[] buf)
返回一个请求正文发布者,其主体是给定的字节数组。- 参数
-
buf- 包含正文的字节数组 - 结果
- 一个BodyPublisher
-
ofByteArray
public static HttpRequest.BodyPublisher ofByteArray(byte[] buf, int offset, int length)
返回一个请求主体发布者,其主体是从指定的offset开始的给定字节数组length字节的offset。- 参数
-
buf- 包含正文的字节数组 -
offset- 第一个字节的偏移量 -
length- 要使用的字节数 - 结果
- 一个BodyPublisher
- 异常
-
IndexOutOfBoundsException- 如果子范围被定义为超出范围
-
ofFile
public static HttpRequest.BodyPublisher ofFile(Path path) throws FileNotFoundException
请求正文发布者,它从文件的内容中获取数据。创建
BodyPublisher时,将在此工厂方法中执行安全管理器权限检查。 必须注意BodyPublisher不与不受信任的代码共享。- 参数
-
path- 包含正文的文件的路径 - 结果
- 一个BodyPublisher
- 异常
-
FileNotFoundException- 如果找不到路径 -
SecurityException- 如果已安装安全管理器并且它拒绝给定文件read access
-
ofByteArrays
public static HttpRequest.BodyPublisher ofByteArrays(Iterable<byte[]> iter)
- 参数
-
iter- 一个可iter的字节数组 - 结果
- 一个BodyPublisher
-
noBody
public static HttpRequest.BodyPublisher noBody()
请求正文发布者,不发送请求正文。- 结果
- BodyPublisher立即完成并且不发送请求正文。
-
-