- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我对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,我将 block 大小设置为 4096。
HttpUrlConnection 每次写入都会向服务器发送 4096 个字节?最后一次 1k?
请注意,我使用了 10k 的缓冲区来写入输出流, block 大小和缓冲区大小不一样有关系吗?
如果我在我的代码中禁用 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就可以先被设置和发送,这增加了很多延迟和内存。在大文件的情况下不推荐。
关于java - HttpUrlConnection.setChunkedStreamingMode 的效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46249401/
试图将大文件从 android 发送到 php,我可以发送小尺寸(最多 1 mp)我四处寻找解决方案,出现了“setChunkedStreamingMode”当我设置 connection.setco
我对HttpUrlConnection.setChunkedStreamingMode没有很好的理解,这个模式有什么作用? 我有以下示例代码: HttpURLConnection conn = get
我的服务器版本在我的开发机器上如下: Apache/2.2.21 (Win32) mod_fcgid/2.3.6 我一直在测试 HttpURLConnection,因为我的项目需要简单的流媒体功能。我
当我使用 conn.setChunkedStreamingMode(1024) 时,程序会运行并上传文件,但上传的文件在上传文件夹中不可用(未找到)。 当我删除 setchunk.... 行时,文件上
在 HttpURLConnection.setChunkedStreamingMode() 的文档中,它说如果我在参数中指定0,它将使用默认的 block 长度,例如: conn.setChunked
我花了大约一天的时间来调试这个问题,但我没有想法。基本上我有一个 Android 应用程序,它正在将一些数据发布到 PHP/Apache Web 服务器。当我将它指向我的本地测试服务器时,这段代码似乎
我是一名优秀的程序员,十分优秀!