【问题标题】:upload big file using multiple hppt connections in J2ME在 J2ME 中使用多个 hppt 连接上传大文件
【发布时间】:2009-12-15 22:58:43
【问题描述】:

我想从诺基亚手机上传一个大文件到服务器,我使用下面的代码。此代码适用于小文件。当我想上传一个更大的文件(大约 10mb)时,我会收到一条内存不足的消息。有谁知道如何转换此代码以使用上传文件 多个 httpConnections,每个连接发送一个文件块。假设服务器支持这一点。

fc = (FileConnection)Connector.open("file:///myfile", Connector.READ);
is = fc.openInputStream();

// opening http connection and outputstream
HttpConnection http = (HttpConnection)Connector.open(url, Connector.WRITE);
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("Content-Type", type);
http.setRequestProperty("Connection", "close");

OutputStream os = http.openOutputStream();


int total = 0;

while (total < fileSize) {      
    byte b[] = new byte[1024];   
    int length = is.read(b, 0, 1024);

    os.write(b, 0, length);
    total += length;
}
os.flush();


int rc = http.getResponseCode();
os.close();
http.close();

【问题讨论】:

    标签: http java-me upload


    【解决方案1】:

    尝试在while 循环内移动os.flush() 调用,以便每次都刷新缓冲区。就目前而言,您的整个文件在通过网络发送之前被读入内存。

    如果这不能解决问题,我建议使用profiler 在模拟器中运行代码,这样您就可以更好地了解内存被用完的地方。

    【讨论】:

    【解决方案2】:

    尝试在您的 while 循环之前移动缓冲区初始化 byte b[] = new byte[1024]; - 您不需要每次都重新创建它。并尝试将 System.gc() 放入循环中 - 它可能会有所帮助(或可能不会)。

    另外,当您调用fc.openInputStream时,某些设备可能会尝试将整个文件放入内存

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-25
      • 2014-12-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2018-10-03
      相关资源
      最近更新 更多