0

I'm developing an application that can send different files to web server.Also I want to send large files, in able to do this I need to chunk the files. But when I'm sending the files to server nothing is uploaded. I don't know if there's error on how I'm sending the files and it gives an error 500 (internal server error) on my response.I don't think the server is the problem because when I'm uploading a file using multiPartEntity it works but when Im using BufferedInputStream and DataOutputStream it doesn't work. Please help me and tell what's wrong on my code why it can't send my files. Here's I got so far:

        String samplefile = "storage/sdcard0/Pictures/Images/picture.jpg";
        File mFile = new File(samplefile);

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

        int chunkId = 0;
        try {

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

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

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

                 URL url = new URL(urlString);

                 // Open a HTTP connection to the URL
                 HttpURLConnection 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");

                 String encoded = Base64.encodeToString((_username+":"+_password).getBytes(),Base64.NO_WRAP); 
                 conn.setRequestProperty("Authorization", "Basic "+encoded); 
                 conn.setRequestProperty("Connection", "Keep-Alive");

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

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

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



                // Send parameter #chunks
                dos.writeBytes("Content-Disposition: form-data; name=\"chunk\"" + 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: " + param3.length() + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(param3 + lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);


                byte[] buffer = new byte[mychunkSize];

                stream.read(buffer);

                dos.write(buffer);

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


            }
        } catch (Exception e) {
            Log.e("Error Uploading Files", e.toString());
        }
NewDroidDev
  • 1,656
  • 5
  • 19
  • 33
  • Please take a look at this other similar question [1]: http://stackoverflow.com/questions/9630430/upload-large-file-in-android-with-outofmemory-error – Chinmay Lokesh Aug 13 '13 at 04:40
  • @Chronos Sir Chronos if you're there please kindly check my code thanks – NewDroidDev Aug 13 '13 at 08:19
  • @NewDroidDev How you solved that question? I have the same problem. I am getting ***500 internal server error*** while uploading the first chunk of the file. https://stackoverflow.com/questions/59501141/why-i-can-not-upload-first-chunk-of-file-by-using-httpurlconnection – Seydazimov Nurbol Dec 30 '19 at 06:47

2 Answers2

0

You can make use of this code. It is a J2ME class to handle file uploads via HTTP POST Multipart Requests.

Hope this helps.

random
  • 10,238
  • 8
  • 57
  • 101
0

If you prefer then you can use this library. Its very easy to implement and dose all the work you need.

AndroidAsync

Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76
  • I need to send my file in chunk. What do you think wrong on my code? Why it doesn't give a request to the server? – NewDroidDev Aug 13 '13 at 06:31