我想上传一个我从相机背面的JPEG图像缓冲区。我所知道的长度,但如果我通过只是长度与setFixedLengthStreamingMode()函数,我得到了错误的logcat告诉我,它预计其大小。我不知道这是否是一个机器人的错误(我运行2.3.3在此设备上),或者我应该在头的尺寸增加除了POST数据还是什么。我可以用setChunkedStreamingMode(0),而且工作得很好,但我想这将是很好的避免复制数据的开销。我似乎永远是155个字节。除非我在155字节任意添加,然后它告诉我,我是2个字节。: - )
I'm trying to upload a jpeg image buffer I got back from the Camera. I know the length, but if I pass just that length to the setFixedLengthStreamingMode() routine I get errors in the logcat telling me it expected a different size. I don't know if this is a android bug (I'm running 2.3.3 on this device), or I'm supposed to add in the sizes of headers in addition to the POST data or what. I can use setChunkedStreamingMode(0) and that works fine, but I was thinking it would be nice to avoid the overhead of copying the data. I seem to always be 155 bytes off except when I arbitrarily add in 155 bytes, then it tells me I am 2 bytes off :-).
在的URLConnection
缓冲区默认的所有的是被写入到的getOutputStream()
在客户端的内存,直到它关闭。这是必需的,因为HTTP 内容长度
响应头需要设置。但内容长度一旦所有字节都被写入唯一已知的。这可能是存储器占用时响应主体是比较大的。
The URLConnection
buffers by default everything which is been written to its getOutputStream()
in client's memory until it's closed. This is mandatory because the HTTP Content-Length
response header needs to be set. But the content length is only known once all bytes have been written. This may be memory hogging when the response body is relatively large.
如果你事先知道字节的具体数额(注:字节,而不是字符)被写入,那么你可以使用 setFixedLengthStreamingMode()
与设置的正是的量的字节,从而使内容长度
头可设置更快,并让的URLConnection
可以更频繁地刷新。在特定的情况下,显然使用了错误的值。
If you know beforehand the exact amount of bytes (note: bytes, not characters) being written, then you could use setFixedLengthStreamingMode()
to set it with exactly that amount of bytes, so that the Content-Length
header could be set much sooner and so that URLConnection
can flush more often. In your particular case, you apparently used the wrong value.
在 setChunkedStreamingMode()
基本上改变了传输编码为分块
。这基本上是一条线与十六进制code中的字节长度,然后一行写入的字节therafter,然后空一行(另见的维基百科)。最后一行具有0的字节长度,因此服务器知道当它的主体的端部,以便它不必等待任何输入数据。这使得响应主体被刷新更快。应该只未用0设置它,而是用一个明智的值,如1000左右。这样,每1000写入的字节将作为一个新块。
The setChunkedStreamingMode()
basically changes the transfer encoding to chunked
. This is basically one line with the byte length in hexcode and then one line of written bytes therafter and then a blank line (see also wikipedia). The last line has a byte length of 0 and so the server knows when it's the end of the body so that it doesn't have to wait for any incoming data. This allows the response body being flushed more often. You should only not set it with 0, but with a sensible value, such as 1000 or so. This way every 1000 written bytes will be sent as a new chunk.
上一篇:Twitter API 邮递员:代码 32:无法验证您的身份您的、邮递员、身份、代码
下一篇:java.lang.RuntimeException的Theme.Sherlocklang、java、RuntimeException、Sherlock