3

The thing is I'm trying to upload an image to a server. The image has to be uploaded by chunks of 256kb and I need to pass the chunks count and id with every call. I can get the total number of chunks to upload and I'm using a BufferedInputStream to get the chunks bytes. But when I finish to upload all the chunks the image showed is always corrupted.

My code so far:

int chunkSize = 255 * 1024;
final long size = mFile.length();
final long chunks = mFile.length() < chunkSize? 1: (mFile.length() / chunkSize);

int chunkId = 0;

BufferedInputStream stream = new BufferedInputStream(new FileInputStream(mFile));

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "RQdzAAihJq7Xp1kjraqf";// random data

for (chunkId = 0; chunkId < chunks; chunkId++) {

     URL url = new URL(urlString);
     // Open a HTTP connection to the URL
     conn = (HttpURLConnection) url.openConnection();

     conn.setReadTimeout(20000 /* milliseconds */);
     conn.setConnectTimeout(20000 /* milliseconds */);


     // Allow Inputs
     conn.setDoInput(true);
     // Allow Outputs
     conn.setDoOutput(true);
     // Don't use a cached copy.
     conn.setUseCaches(false);
     // Use a post method.
     conn.setRequestMethod("POST");
     conn.setRequestProperty("Connection", "Keep-Alive");

     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
     dos = new DataOutputStream( conn.getOutputStream() );
     dos.writeBytes(twoHyphens + boundary + lineEnd);

     String param1 = ""+chunkId;
     String param2 = ""+chunks;
     String param3 = mFile.getName();

    // for every param
    dos.writeBytes("Content-Disposition: form-data; name=\"chunk\"" + lineEnd);
    dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
    dos.writeBytes("Content-Length: " + param1.length() + lineEnd);
    dos.writeBytes(lineEnd);
    dos.writeBytes(param1 + lineEnd);
    dos.writeBytes(twoHyphens + boundary + lineEnd);

    // Send parameter #chunks
    dos.writeBytes("Content-Disposition: form-data; name=\"chunks\"" + lineEnd);
    dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
    dos.writeBytes("Content-Length: " + param2.length() + lineEnd);
    dos.writeBytes(lineEnd);
    dos.writeBytes(param2 + lineEnd);
    dos.writeBytes(twoHyphens + boundary + lineEnd);


    // Send parameter #name
    dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
    dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
    dos.writeBytes("Content-Length: " + param4.length() + lineEnd);
    dos.writeBytes(lineEnd);
    dos.writeBytes(param3 + lineEnd);
    dos.writeBytes(twoHyphens + boundary + lineEnd);

    // Send parameter #file
    dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + param4 + "\"" + lineEnd); // filename is the Name of the File to be uploaded

    dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
    dos.writeBytes(lineEnd);

    byte[] buffer = new byte[chunkSize];

    stream.skip(chunkId * chunkSize);
    stream.read(buffer);

    // dos.write(buffer, 0, bufferSize);
    dos.write(buffer);


    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    dos.flush();
    dos.close();


// read response...

}

Many thanks!

Chronos
  • 1,972
  • 17
  • 22
  • did you find a solution on this problem please tell me what you do I have the same problem. Thanks – NewDroidDev Aug 12 '13 at 09:22
  • @NewDroidDev, this line was wrong: stream.skip(chunkId * chunkSize);. I deleted it and all works fine now. – Chronos Aug 12 '13 at 18:47
  • may I know what is the value of your param4? – NewDroidDev Aug 13 '13 at 00:31
  • Look here's my code. Please help I'm having a lack of time I need to finish my task. :( http://stackoverflow.com/questions/18200299/how-to-upload-files-using-dataoutputstream?noredirect=1#comment26674565_18200299 – NewDroidDev Aug 13 '13 at 08:22

2 Answers2

2

Well,

I solved the issue. I deleted the following line:

stream.skip(chunkId * chunkSize);

I was skipping several chunks of the stream :). Sorry my bad.

Chronos
  • 1,972
  • 17
  • 22
  • it doesn't work on me. Do I need to add some code in able to upload files. It doesn't give me error but it doesn't upload the files through web server. Please help me I'm stack. – NewDroidDev Aug 13 '13 at 04:37
  • Please help me with this problem why it doesn't upload to the server is anything I need to add to make this code work? – NewDroidDev Aug 13 '13 at 07:26
  • I don't know why I get error 500 (internal server error). I don't think the server have the problem because when I'm sending file using multiPartEntity it works but I go to this code because I can't chunk my files in multiPartEntity – NewDroidDev Aug 13 '13 at 09:19
0

You have to define "multipart/form-data" instead of form-data when sending image (file)

Chirag Nagpal
  • 729
  • 10
  • 25