1

I am trying to upload image to php server. But it gives me

java.io.FileNotFoundException: http://*******.info/test.php

how can I solve this problem?

 package com.androidexample.uploadtoserver;

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class UploadToServer extends Activity {

    TextView messageText;
    Button uploadButton;
    int serverResponseCode = 0;
    ProgressDialog dialog = null;       
    String upLoadServerUri = null;

    /**********  File Path *************/
    final String uploadFilePath = "/mnt/sdcard/";
    final String uploadFileName = "service.jpg";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_to_server);

        uploadButton = (Button)findViewById(R.id.uploadButton);
        messageText  = (TextView)findViewById(R.id.messageText);


        uploadButton.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {

                dialog = ProgressDialog.show(UploadToServer.this, "", "Uploading file...", true);

                new Thread(new Runnable() {
                        public void run() {
                             runOnUiThread(new Runnable() {
                                    public void run() {
                                        //messageText.setText("uploading started.....");
                                    }
                                });                      
                             doFileUpload();                             
                             Log.d("deneme", "buraya geldim i");                       
                        }
                      }).start();        
                }
            });
    }


        private void doFileUpload() { 
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;           
            String existingFileName = uploadFilePath+uploadFileName;
          //  imageView1.setImageURI(Uri.parse(existingFileName));
            Log.e("--adress Debug--",existingFileName);
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            String responseFromServer ="";

            String urlString = "http://w.****.info/test.php";           
            Log.d("deneme1", "buraya geldim i");  
            try {            
                //------------------ CLIENT REQUEST
                FileInputStream fileInputStream = new FileInputStream(new File(existingFileName));
                // 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.
                Log.d("deneme2", "buraya geldim i");  
                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);
                dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                // create a buffer of maximum size
                Log.d("deneme3", "buraya geldim i");  
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                // read file and write it into form...
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                Log.e("--burya geldimi --","");
                while (bytesRead > 0) {

                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                    Log.e("--b----------------mi --","");
                }
                Log.d("deneme4", "buraya geldim i");  
                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                // close streams
                Log.e("Debug", "File is written");
                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (MalformedURLException ex) {
                Log.e("Debug0", "error1: " + ex.getMessage(), ex);
            } catch (IOException ioe) {
                Log.e("Debug1", "error2: " + ioe.getMessage(), ioe);
            }

            //------------------ read the SERVER RESPONSE
            try {

                inStream = new DataInputStream(conn.getInputStream());
                String str;

                while ((str = inStream.readLine()) != null) {

                    Log.e("Debug2", "Server Response " + str);

                }

                inStream.close();

            } catch (IOException ioex) {
                Log.e("Debug3", "error3: " + ioex.getMessage(), ioex);
            }
        }

}

}
}

my cat log

12-15 21:10:33.599: E/Debug3(9087): error3: http://***.info/test.php
12-15 21:10:33.599: E/Debug3(9087): java.io.FileNotFoundException: http://****.info/test.php
12-15 21:10:33.599: E/Debug3(9087):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
12-15 21:10:33.599: E/Debug3(9087):     at com.androidexample.uploadtoserver.UploadToServer.doFileUpload(UploadToServer.java:140)
12-15 21:10:33.599: E/Debug3(9087):     at com.androidexample.uploadtoserver.UploadToServer.access$0(UploadToServer.java:66)
12-15 21:10:33.599: E/Debug3(9087):     at com.androidexample.uploadtoserver.UploadToServer$1$1.run(UploadToServer.java:57)
12-15 21:10:33.599: E/Debug3(9087):     at java.lang.Thread.run(Thread.java:1019)
fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • how are you accessing filepath of the file to be uploaded. Show code for that please – Suhail Mehta Dec 15 '14 at 14:01
  • To debug this, create a very simple HTML page on a server other than the PHP server, place an HTML `FORM` on it with a method of `POST` and an action of the PHP page and a submit button. See if that gives you an error. Once that's resolved, add an file HTML input field and see if that works. When testing this file upload, use the exact same file that you are using on your device. On the PHP server, turn on some logging to see what's actually coming in. Ultimately you are [receiving an HTTP status of 400 or greater](http://tinyurl.com/q85hthw) from the server. – Chris Haas Dec 15 '14 at 14:37

1 Answers1

1

this is my server site code

<?php

    $file_path = "uploads/";

    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }
 ?>