java 有哪些不用分块创建不确定长度的大文件流式传输 http post 方法
时间: 2023-10-09 07:07:39 浏览: 107
Java中可以使用HttpURLConnection类来进行HTTP POST请求,并且可以通过设置Chunked Streaming Mode来实现不用分块创建不确定长度的大文件流式传输。以下是一些示例代码:
1. 使用HttpURLConnection类进行HTTP POST请求并设置Chunked Streaming Mode:
```java
URL url = new URL("http://example.com/upload");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setChunkedStreamingMode(0); // 0表示不确定长度
```
2. 使用Apache HttpClient库进行HTTP POST请求并设置Chunked Streaming Mode:
```java
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://example.com/upload");
httpPost.setEntity(new InputStreamEntity(inputStream, -1, ContentType.APPLICATION_OCTET_STREAM));
httpPost.setHeader("Transfer-Encoding", "chunked");
CloseableHttpResponse response = httpClient.execute(httpPost);
```
注意:以上示例代码中的inputStream是需要上传的文件的InputStream对象。
阅读全文