0

Im writing an Android App. This App should upload some pictures to my server via POST. It is working if I use picture smaller then 1MB. But if i use larger images the log says me that the image was uploaded but the image is not on the server. The post_max_size is 4MB

My Method is from a stackoverflow solution and should work but it doesnt and I dont know why.

// http post
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("c_user_id", params[1]));
        nameValuePairs.add(new BasicNameValuePair("c_task_id", params[2]));
        nameValuePairs.add(new BasicNameValuePair("c_comment", params[3]));
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(phpPath + "insert_task.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection" + e.toString());
            this.cancel(true);

        }

        String Tag = "UPLOADER";
        String urlString = phpPath + "upload_task.php?user_id=" + params[1]
                + "&task_id=" + params[2];
        HttpURLConnection conn = null;

        String exsistingFileName = params[0];
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        try {
            // ------------------ CLIENT REQUEST

            Log.e(Tag, "Inside second Method");

            FileInputStream fileInputStream = new FileInputStream(new File(
                    exsistingFileName));

            // open a URL connection to the Servlet

            URL url = new URL(urlString);

            // Open a HTTP connection to the URL

            conn = (HttpURLConnection) url.openConnection();

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

            DataOutputStream dos = new DataOutputStream(
                    conn.getOutputStream());

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
                    + exsistingFileName + "" + lineEnd);
            dos.writeBytes(lineEnd);

            Log.e(Tag, "Headers are written");

            // create a buffer of maximum size
            int bytesAvailable = fileInputStream.available();   
            int maxBufferSize = 1 * 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[] buffer = new byte[bufferSize];
            buffer = new byte[bufferSize];
            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            // Read file

            Log.e("Image length", bytesAvailable + "");
            try {
                while (bytesRead > 0) {
                    try {
                        dos.write(buffer, 0, bufferSize);
                    } catch (OutOfMemoryError e) {
                        e.printStackTrace();

                    }
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }
            } catch (Exception e) {

            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);



            Log.i("File Name in Server : ", exsistingFileName);

            fileInputStream.close();
            dos.flush();
            dos.close();
            dos = null;
        } catch (Exception ex) {
            // Exception handling

            Log.e("Send file Exception", ex.getMessage() + "");
            ex.printStackTrace();
        }
Dennis
  • 143
  • 5
  • 17
  • Is your server running Java? Are you reading the sent file with a DataInputStream? What error are you getting? – Elliott Frisch Feb 23 '14 at 15:08
  • no my server is running php. I send the post data to a php file. if i read the inputstream the method does not continue. i get no error the method finish without exception – Dennis Feb 23 '14 at 15:13
  • Right. Well, a `DataOutputStream` will not be sending `multipart/form-data`. Assuming you're on Android you should see this [question](http://stackoverflow.com/questions/3360957/how-use-multipart-form-data-upload-picture-image-on-android?rq=1). – Elliott Frisch Feb 23 '14 at 15:38
  • ill take a look thanks...but its odd that it is working with small pictures – Dennis Feb 23 '14 at 15:53
  • You aren't encoding the file length. With small images it works because you fit within a decode window and the other side determines it's image data (correctly). With larger files you don't fit within the decode window and the other side timeouts. See also [this](http://stackoverflow.com/questions/7616776/sending-a-file-using-dataoutputstream-in-java) question. Finally, I would probably use an existing library like [HttpClient](http://hc.apache.org/httpcomponents-client-ga/) myself. – Elliott Frisch Feb 23 '14 at 15:59
  • thanks for your help. i solved the problem with the MultipartEntityBuilder – Dennis Feb 23 '14 at 16:43

0 Answers0