尝试分块上传
我正在尝试在黑莓上完成大文件上传。我能够成功上传文件,但前提是我读取文件并一次上传 1 个字节。对于大文件,我认为这会降低性能。我希望能够一次读取和写入 128 kb 以上的数据。如果我尝试将缓冲区初始化为 1 以外的任何值,那么在写入所有内容后我永远不会从服务器收到响应。
有什么想法为什么我一次只能使用 1 个字节上传吗?
z.write(boundaryMessage.toString().getBytes());
DataInputStream fileIn = fc.openDataInputStream();
boolean isCancel = false;
byte[]b = new byte[1];
int num = 0;
int left = buffer;
while((fileIn.read(b)>-1))
{
num += b.length;
left = buffer - num * 1;
Log.info(num + "WRITTEN");
if (isCancel == true)
{
break;
}
z.write(b);
}
z.write(endBoundary.toString().getBytes());
I am trying to accomplish a large file upload on a blackberry. I am succesfully able to upload a file but only if I read the file and upload it 1 byte at a time. For large files I think this is decreasing performance. I want to be able to read and write at something more 128 kb at a time. If i try to initialise my buffer to anything other than 1 then I never get a response back from the server after writing everything.
Any ideas why i can upload using only 1 byte at a time?
z.write(boundaryMessage.toString().getBytes());
DataInputStream fileIn = fc.openDataInputStream();
boolean isCancel = false;
byte[]b = new byte[1];
int num = 0;
int left = buffer;
while((fileIn.read(b)>-1))
{
num += b.length;
left = buffer - num * 1;
Log.info(num + "WRITTEN");
if (isCancel == true)
{
break;
}
z.write(b);
}
z.write(endBoundary.toString().getBytes());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 BlackBerry OS 中的一个错误,出现在 OS 5.0 中,并在 OS 6.0 中持续存在。如果您尝试在 OS 5 之前使用多字节读取,它会正常工作。 OS5 及更高版本会产生您所描述的行为。
您还可以通过创建安全连接来解决该问题,因为该错误不会在安全套接字上显现出来,而只会在普通套接字上显现出来。
It's a bug in BlackBerry OS that appeared in OS 5.0, and persists in OS 6.0. If you try using a multi-byte read before OS 5, it will work fine. OS5 and later produce the behavior you have described.
You can also get around the problem by creating a secure connection, as the bug doesn't manifest itself for secure sockets, only plain sockets.
大多数输入流不能保证每次读取时都会填充缓冲区。 (
DataInputStream
有一个特殊的方法,readFully(),如果流中没有足够的字节来填充缓冲区,则会抛出EOFException
.) 除非文件是缓冲区长度的倍数,否则没有流会在最终读取时填充缓冲区。因此,您需要存储读取的字节数并在写入期间使用它:Most input streams aren't guaranteed to fill a buffer on every read. (
DataInputStream
has a special method for this, readFully(), which will throw anEOFException
if there aren't enough bytes left in the stream to fill the buffer.) And unless the file is a multiple of the buffer length, no stream will fill the buffer on the final read. So, you need to store the number of bytes read and use it during the write:你的循环不正确。您应该注意读取的返回值。它返回实际读取的字节数,并且并不总是与缓冲区大小相同。
编辑:
这是您通常编写循环来执行您想要执行的操作的方式:
请注意,您可能希望使用缓冲流而不是无缓冲流。
Your loop isn't correct. You should take care of the return value from read. It returns how many bytes that were actually read, and that isn't always the same as the buffer size.
Edit:
This is how you usually write loops that does what you want to do:
Note that you might want to use buffered streams instead of unbuffered streams.