上传文件的Andr​​oid与OutOfMemory错误 [英] Upload file in Android with outofmemory error

查看:123
本文介绍了上传文件的Andr​​oid与OutOfMemory错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传code如下:

String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
URL url = new URL(ActionUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Accept", "text/*");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"folder\"" + end + end);
ds.write(SavePath.getBytes("UTF-8"));
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"Filedata\"; filename=\"");
ds.write(FileName.getBytes("UTF-8"));
ds.writeBytes("\"" + end);
ds.writeBytes(end);
FileInputStream fStream = new FileInputStream(uploadFilepath+""+FileName);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
int pro = 0;
while((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}       
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
fStream.close();
ds.flush();
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while((ch = is.read()) != -1) {
b.append((char)ch);
}
ds.close();
}
catch(Exception e) {
e.printStackTrace();
}

它可以同时上传作品较小的文件。
不过,虽然更多的16 MB,它会上传失败,并显示内存不足错误。
我认为这会导致通过把所有的数据缓冲。
所以,我希望把它发送数据,而缓冲保存1024个字节。
但我不知道这样做。
任何人都可以帮我做吗?

It can works while upload a smaller file. But while more 16 mb, it will upload fail and show the OutOfMemory error. I think it cause by put all data in buffer. So I want to make it to send data while buffer save 1024 bytes. But I no idea to do that. May anyone help me to do it?

推荐答案

您应该发生点什么你的错误进行确认。我怀疑它读取响应中的。在这种情况下,似乎服务器可以与大量的数据,你在的StringBuffer 放置响应。你真的需要消耗整个响应,并保持它在内存中?如果它是一个文件,保存它,而不是在内存中是一致的。

You should confirm at what point your error occurs. I suspect that it's during reading the response. In this case, it seems that server may be responding with a lot of data that you place in the StringBuffer. Do you actually need to consume the entire response and keep it in memory? If it's a file, save it rather than keeping in memory.

我做了一些更多的研究和这里是另外一个可能性。 Android的JVM默认配备16MB的最大堆。请参见了解一些细节。

I did some more research and here is one other possibility. Android JVM by default has 16mb max heap. See this for some details.

在另一方面,如果你的服务器实际上并不消耗数据,其中大部分将驻留在客户端上。所以,如果你有比数据的客户将失败的最大堆更多。

On the other hand, if your server does not actually consume the data, most of it will reside on the client. So if you have more than max heap of data the client will fail.

所以我怀疑你的服务器只是不从流中读取数据。

So I suspect that your server just does not read the data from the stream.

下面的类(这是您的code的相关部分的片段)说明了这个问题。运行它像任何如下JVM:

The following class (which is a snippet of relevant parts of your code) illustrates the problem. Run it on any JVM like following:

java -Xmx16m -cp . Test

和它会产生OOM非常快。事实上,远早于预期。

and it will produce OOM very quickly. In fact, much earlier than expected.

import java.io.*;
import java.net.*;

public class Test {
  public static void main(String argv[]) throws Exception {
    new Thread() {
      public void run() {
        try {
           new ServerSocket(12000).accept();
        } catch (Throwable t) {
          t.printStackTrace();
        } 
      }
    }.start();

    URL url = new URL("http://localhost:12000/");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    DataOutputStream ds = new DataOutputStream(con.getOutputStream());
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];
    for (int i=0;i<100000;i++) {
      ds.write(buffer, 0, 1024);
      System.out.println("Written chunk " + i);
    }       
    ds.flush();
  }
}

这篇关于上传文件的Andr​​oid与OutOfMemory错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆