0

My upload code as below:

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();
}

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?

brian
  • 6,802
  • 29
  • 83
  • 124
  • could you try adding ds.flush(); into while((length = fStream.read(buffer)) != -1) { ds.write(buffer, 0, length); } so that it can flush all data to server... (i am assuming the server will be waiting until ds.writeBytes(twoHyphens + boundary + twoHyphens + end); called) – Sudar Nimalan Mar 12 '12 at 02:20
  • I have try it. It still occurs the same error at the same line. – brian Mar 12 '12 at 02:37

2 Answers2

3

brian, you should add

con.setChunkedStreamingMode(0);

before

DataOutputStream ds = new DataOutputStream(con.getOutputStream());

if your server could support chunked mode, or add

con.setFixedLengthStreamingMode(packet_size);

where packet_size = upload_file_size + header_size.

kratenko
  • 7,354
  • 4
  • 36
  • 61
Bob Cheng
  • 81
  • 7
1

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.


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.

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

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();
  }
}
Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • It occurs at line ds.write(buffer, 0, length); in while loop. – brian Mar 12 '12 at 02:10
  • In that part you don't keep anything in memory except 1024 bytes buffer. It could be something with `DataOutputSteram` I'd try to write directly to `OutputStream` and see what happens. – Alex Gitelman Mar 12 '12 at 02:18
  • I also try use con.setFixedLengthStreamingMode(1024);. The error " java.io.IOException: expected 865 bytes but received 1024" showed. – brian Mar 12 '12 at 06:51
  • After I test. The line ds.write(buffer, 0, length); in the while loop, my server does not read from the stream. Modify max heap can access larger file(may several gb), then it still outofmemory. So I have any other method to solve it? – brian Mar 13 '12 at 02:43
  • Can tell me how to use java -Xmx16m -cp . Test in Android? – brian Mar 13 '12 at 02:55
  • Per the article that I refer it is always 16mb on Android. Overall my point is that if your server does not read data, it will be accumulating in memory and cause OOM anyway. I assume that your server should read it at some point, so just make sure that it is read without delay. – Alex Gitelman Mar 13 '12 at 03:49