1

I'm developing an application that can upload data to web server. But I don't know what I'm going to put on my content-length. I tried to get the size of my file and the other text-data that I'm going to upload but it's not correct. Can anyone know what should I gonna put on my content-length?

          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");

                 String encoded = Base64.encodeToString((_username+":"+_password).getBytes(),Base64.NO_WRAP); 
                 conn.setRequestProperty("Authorization", "Basic "+encoded); 
                 conn.setRequestProperty("Connection", "Keep-Alive");
                 //conn.setChunkedStreamingMode(mychunkSize);
                 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
                 conn.setRequestProperty("Content-Length", "**I don't know what I'm going to put here**");
                 conn.connect();

                 dos = new DataOutputStream( conn.getOutputStream() );
                 //dos.writeBytes("Content-Length: " + String.valueOf(size) + lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);



                // Send parameter #file
                dos.writeBytes("Content-Disposition: form-data; name=\"fieldNameHere\";filename=\"" + mFile.getName() + "\"" + 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(param2 + lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);


                // Send parameter #name
                dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
                dos.writeBytes(param3 + lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);


                bytesAvailable = stream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                // read file and write it into form...
                bytesRead = stream.read(buffer, 0, bufferSize);


                while (bytesRead > 0) {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = stream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = stream.read(buffer, 0, bufferSize);
                    Log.i("BytesAvailable", String.valueOf(bytesAvailable));
                    Log.i("bufferSize", String.valueOf(bufferSize));
                    Log.i("buffer", String.valueOf(buffer));
                }

                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                // close streams

                stream.close();
                dos.flush();
                dos.close();


            }

        } catch (MalformedURLException ex) {
            System.out.println("From CLIENT REQUEST:" + ex);
        }catch (IOException ioe) {
            System.out.println("From CLIENT REQUEST:" + ioe);
        }catch (Exception e) {
            Log.e("From CLIENT REQUEST:", e.toString());
        }
NewDroidDev
  • 1,656
  • 5
  • 19
  • 33

0 Answers0