HttpUrlConnection.setChunkedStreamingMode 的影响
Effect of HttpUrlConnection.setChunkedStreamingMode
我不太了解HttpUrlConnection.setChunkedStreamingMode
,这个模式有什么作用?
我有以下示例代码:
HttpURLConnection conn = getHttpURLConnection(_url);
conn.setChunkedStreamingMode(4096); //4k
conn.setConnectTimeout(3000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = conn.getOutputStream();
byte[] buffer = new byte[1024 * 10];//10k
FileInputStream in= new FileInputStream(file); //Write the content of the file to the server
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
in.close();
说,文件大小是101k,我设置块大小是4096。
HttpUrlConnection每次写入都会向服务器发送4096字节?最后一次1k?
请注意,我使用了一个 10k 的缓冲区来写入输出流,块大小和缓冲区大小不一样有关系吗?
如果我在代码中禁用ChunkedStreamMode,与我设置的代码4096相比有什么效果?
- The HttpUrlConnection will send 4096 bytes to the server every write? and 1k for the last time?
是的。
- Note that I have used a 10k buffer to write to the outputstream, Does it matter that the chunk size and buffer size are not the same?
没有
- If I disable the ChunkedStreamMode in my code, what's the effect compared to the code that I have set 4096?
效果是整个输出都被缓冲,直到你关闭,这样Content-length header可以被设置和发送,这增加了很多延迟和内存。不建议在大文件的情况下使用。
我不太了解HttpUrlConnection.setChunkedStreamingMode
,这个模式有什么作用?
我有以下示例代码:
HttpURLConnection conn = getHttpURLConnection(_url);
conn.setChunkedStreamingMode(4096); //4k
conn.setConnectTimeout(3000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = conn.getOutputStream();
byte[] buffer = new byte[1024 * 10];//10k
FileInputStream in= new FileInputStream(file); //Write the content of the file to the server
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
in.close();
说,文件大小是101k,我设置块大小是4096。
HttpUrlConnection每次写入都会向服务器发送4096字节?最后一次1k?
请注意,我使用了一个 10k 的缓冲区来写入输出流,块大小和缓冲区大小不一样有关系吗?
如果我在代码中禁用ChunkedStreamMode,与我设置的代码4096相比有什么效果?
- The HttpUrlConnection will send 4096 bytes to the server every write? and 1k for the last time?
是的。
- Note that I have used a 10k buffer to write to the outputstream, Does it matter that the chunk size and buffer size are not the same?
没有
- If I disable the ChunkedStreamMode in my code, what's the effect compared to the code that I have set 4096?
效果是整个输出都被缓冲,直到你关闭,这样Content-length header可以被设置和发送,这增加了很多延迟和内存。不建议在大文件的情况下使用。