尝试分块上传

发布于 2024-11-05 10:01:44 字数 598 浏览 1 评论 0原文

我正在尝试在黑莓上完成大文件上传。我能够成功上传文件,但前提是我读取文件并一次上传 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人哭 2024-11-12 10:01:44

这是 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.

榆西 2024-11-12 10:01:44

大多数输入流不能保证每次读取时都会填充缓冲区。 (DataInputStream 有一个特殊的方法,readFully(),如果流中没有足够的字节来填充缓冲区,则会抛出 EOFException .) 除非文件是缓冲区长度的倍数,否则没有流会在最终读取时填充缓冲区。因此,您需要存储读取的字节数并在写入期间使用它:

while(!isCancel)
{
  int n = fileIn.read(b);
  if (n < 0)
    break;
  num += n;
  Log.info(num + "WRITTEN");
  z.write(b, 0, n);
}

Most input streams aren't guaranteed to fill a buffer on every read. (DataInputStream has a special method for this, readFully(), which will throw an EOFException 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:

while(!isCancel)
{
  int n = fileIn.read(b);
  if (n < 0)
    break;
  num += n;
  Log.info(num + "WRITTEN");
  z.write(b, 0, n);
}
香草可樂 2024-11-12 10:01:44

你的循环不正确。您应该注意读取的返回值。它返回实际读取的字节数,并且并不总是与缓冲区大小相同。

编辑:
这是您通常编写循环来执行您想要执行的操作的方式:

OutputStream z = null;  //Shouldn't be null
InputStream in = null;  //Shouldn't be null
byte[] buffer = new byte[1024 * 32];
int len = 0;
while ((len = in.read(buffer)) > -1) {
    z.write(buffer, 0, len);
}

请注意,您可能希望使用缓冲流而不是无缓冲流。

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:

OutputStream z = null;  //Shouldn't be null
InputStream in = null;  //Shouldn't be null
byte[] buffer = new byte[1024 * 32];
int len = 0;
while ((len = in.read(buffer)) > -1) {
    z.write(buffer, 0, len);
}

Note that you might want to use buffered streams instead of unbuffered streams.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文