J2me multipart/form-data 发送 java.lang.exception 内存不足错误 - 需要帮助

发布于 2024-09-27 21:32:59 字数 537 浏览 8 评论 0原文

我正在尝试将图像从我的 midlet 发送到 HTTP 服务器。图像转换为字节 并使用http multipart/form-data 请求格式发送到服务器。

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(boundaryMessage.getBytes());
bos.write(fileBytes);
bos.write(endBoundary.getBytes());

当图像大小小于 500Kb 左右时,代码可以正常工作,但是当大小大于它时,会显示:未捕获异常 java.lang.OutOfMemoryError。我尝试使用 Java ME SDK 3.0 和诺基亚 S40 第五版 FP1。非常感谢任何帮助。感谢您的查看,

我使用了以下类文件:单击此处

I am trying to send image from my midlet to an HTTP server. images are converted into byte
and sent to server using http multipart/form-data request format.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(boundaryMessage.getBytes());
bos.write(fileBytes);
bos.write(endBoundary.getBytes());

When the image size is less than around 500Kb then the code works fine, but when the size is greater than it shows: Uncaught exception java.lang.OutOfMemoryError. I tried using Java ME SDK 3.0 and Nokia S40 5th edition FP1. Any help is greatly appreciated. Thanks for looking

I used the following class file: click here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

中性美 2024-10-04 21:32:59

被迫使用第一个 getFileBytes() 将整个文件读入内存,以便以一块形式传输,这很可能是系统内存不足的原因。

想办法读取大约100K,传输它,然后再读取100,直到整个文件完成。

编写的 HttpMultipartRequest 类的构造函数仅允许将文件作为单个对象进行传输。尽管它是 MIME 多部分内容协议的实现,但它仅限于仅传输一部分的情况:

可以修改该类以允许发送多个部分。查看协议规范 RFC1341,尤其是中间的示例。

将这三行放在构造函数中,整个文件将分为一部分发送;

bos.write(boundaryMessage.getBytes());
bos.write(fileBytes);
bos.write(endBoundary.getBytes());

但在多部分情况下,在 endBoundary 之前需要有多个边界:

 for(bytes=getMoreFileBytes(); ! bytes.empty; bytes=getMoreFileBytes()){
        bos.write(boundaryMessage.getBytes());
        bos.write(bytes);
    }
    bos.write(endBoundary.getBytes());

作为快速修复,让构造函数打开文件并一次读取 100k。它已经收到一个 fileName 参数。

另一端的 PHP 脚本应该将各个片段重新组合成原始文件。

Being forced to read the whole file into memory with the first `getFileBytes(), in order to transmit in one piece, is most likely what's running the system out of memory.

Find a way to read about 100K, transmit it, then read another 100, until the whole file is done.

The HttpMultipartRequest class's constructor as written allows only for the transmission of the file as one single object. Even though it's an implementation of the MIME multipart content protocol, it is limited to the case of transmitting just one part:

The class can be modified to allow sending multiple parts. Have a look at the protocol specification RFC1341, especially the example half-way through.

With these three lines together as they are in the constructor, the whole file is sent in one part;

bos.write(boundaryMessage.getBytes());
bos.write(fileBytes);
bos.write(endBoundary.getBytes());

But in the multipart case, there needs to be multiple boundaries, before the endBoundary:

 for(bytes=getMoreFileBytes(); ! bytes.empty; bytes=getMoreFileBytes()){
        bos.write(boundaryMessage.getBytes());
        bos.write(bytes);
    }
    bos.write(endBoundary.getBytes());

As a quick fix, let the constructor open the file and read it 100k at a time. It already receives a fileName parameter.

The PHP script on the other end, should reassemble the original file from the pieces.

凝望流年 2024-10-04 21:32:59

我对论坛规则不是很熟悉,我试图评论你的答案,但它显示是否定的。

好的..
现在我得到了 java.io.IOException: 在发送第一个块后持久连接丢失,无法重试

之前我尝试使用 application/x-www-form-urlencoded 请求类型和 Base64 编码,使用 Kidcandy 的代码: http://forums.sun.com/thread.jspa?threadID=538500

此代码将图像数据划分为块以避免“持久连接丢失”问题,并使用“for”循环创建与服务器的连接。问题是最大块大小可能只有 500-700 字节。因此,要发送 100kb 的图像,代码需要创建和关闭连接 200 次,我尝试在诺基亚 5310 手机上运行它,它的行为就像正在休眠一样......所以它没有用。

  1. 现在我应该使用 for 循环来处理“multipart/form-data”请求吗?
  2. 此类请求的最大块大小是多少?
  3. 或者还有其他想法吗?
    问候

I am not very familiar with the forum rules, I tried to comment your answer but it shows negative.

Okay..
Now I am getting java.io.IOException: Persistent connection dropped after first chunk sent, cannot retry

previously I tried to use application/x-www-form-urlencoded request type with Base64 encoding using kidcandy's code here: http://forums.sun.com/thread.jspa?threadID=538500

This code divides the imagedata in chunks to avoid 'Persistent connection drop' problem and creates connection with the server using 'for' loop. The problem is maximum chunk size maybe only 500-700 bytes. So to send a 100kb image the code needs to create and close connection 200 times, I tried to run this on nokia 5310 phone, it behaves like it is hibernating... so it is not useful.

  1. Now should I use that for loop for 'multipart/form-data' request?
  2. What is the maximum chunk size for this type of request?
  3. Or any other idea?
    Regards
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文