9

I've read many many posts about sending an image to the server from an Android app and Content-type-wise, they are divided in three categories:

a) they dont set the content-type at all and probably somehow their code works

b) they are using deprecated methods

c) they are using completely different approach from the one I've selected.

I would like to send the file over to the server and store it in a folder.

My code is a total patchwork, a butchery job that I managed to come up with after reading lots of posts and articles, here is it:

public void uploadImageToServer(String imagePath) throws Exception {

    try {

        // set the http handlers
        httpClient = new DefaultHttpClient();
        localContext = new BasicHttpContext(); // why do I need this?
        postRequest = new HttpPost("http://asd.com/asd.php"); 
        //postRequest.addHeader("Content-type", "image/jpeg"); // - this didnt work

        // deal with the file
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap = BitmapFactory.decodeFile(imagePath);
        bitmap.compress(CompressFormat.JPEG, 75, byteArrayOutputStream); 
        byte[] byteData = byteArrayOutputStream.toByteArray();
        //String strData = Base64.encodeToString(data, Base64.DEFAULT); // I have no idea why Im doing this
        ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image"); // second parameter is the name of the image (//TODO HOW DO I MAKE IT USE THE IMAGE FILENAME?)

        // send the package
        multipartEntity = MultipartEntityBuilder.create();
        multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("uploaded_file", byteArrayBody);

        postRequest.setEntity(multipartEntity.build());

        // get the response. we will deal with it in onPostExecute.
        response = httpClient.execute(postRequest, localContext);
        bitmap.recycle();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

And the error is:

 FATAL EXCEPTION: AsyncTask #1
 java.lang.RuntimeException: An error occured while executing doInBackground()
android.os.AsyncTask$3.done(AsyncTask.java:200)
 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
java.util.concurrent.FutureTask.setException(FutureTask.java:125)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
java.util.concurrent.FutureTask.run(FutureTask.java:138)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
 java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
 org.apache.http.entity.mime.content.ByteArrayBody.<init>(ByteArrayBody.java:67)
 org.apache.http.entity.mime.content.ByteArrayBody.<init>(ByteArrayBody.java:87)
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

5 Answers5

5

If you are using a library, you need to put it into /libs folder.

EDIT:

download httpmime, httpcore and httpclient library from http://hc.apache.org/downloads.cgi

Ezrou
  • 438
  • 6
  • 14
  • I am using a library and it is in the /libs folder. The error says I havent set the Content-type – Kaloyan Roussev Dec 27 '13 at 10:41
  • 2
    No, the error says that not found the part of the library Content-type: Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType – Ezrou Dec 27 '13 at 10:45
  • oh, well first of all sorry for disagreeing with you there. second I am using httpmime-4.3.jar and its in the libs folder, so what do I do – Kaloyan Roussev Dec 27 '13 at 10:49
  • Go there http://hc.apache.org/downloads.cgi and download httpmime, httpclient and httpcore, and then try it again – Ezrou Dec 27 '13 at 10:53
  • 2
    wow thanks that helped fix the problem. you could edit your answer to add this info to it. Im accepting it. – Kaloyan Roussev Dec 27 '13 at 10:58
5

Use this code to upload image file

HttpClient client = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("http://localhost/Upload/index.php");
File file = new File(filePath);
MultipartEntity entity = new MultipartEntity();
FileBody contentFile = new FileBody(file);
entity.addPart("userfile",contentFile);
StringBody contentString = new StringBody("This is contentString");
entity.addPart("contentString",contentString);

postMethod.setEntity(entity);
client.execute(postMethod);

and in PHP use this code to receive

$uploads_dir = '/Library/WebServer/Documents/Upload/upload/'.$_FILES['userfile']['name'];
if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    echo $_POST["contentString"]."\n";
    echo  "File path = ".$uploads_dir;
    move_uploaded_file ($_FILES['userfile'] ['tmp_name'], $uploads_dir);
} else {
    echo "\n Upload Error";
    echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
    print_r($_FILES);
}
henry4343
  • 3,871
  • 5
  • 22
  • 30
1

Use this code:--

public class HttpMultipartUpload {
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "AaB03x87yxdkjnxvi7";

    public String upload(URL url, File file, String fileParameterName,
            HashMap<String, String> parameters) throws IOException {
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream dis = null;
        FileInputStream fileInputStream = null;

        byte[] buffer;
        int maxBufferSize = 20 * 1024;
        try {
            // ------------------ CLIENT REQUEST
            fileInputStream = new FileInputStream(file);

            // open a URL connection to the Servlet
            // 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("Content-Type",
                    "multipart/form-data;boundary=" + boundary);

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

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\""
                    + fileParameterName + "\"; filename=\"" + mFileName
                    + ".jpg" + "\"" + lineEnd);
            dos.writeBytes("Content-Type:image/jpg" + lineEnd);
            dos.writeBytes(lineEnd);

            // create a buffer of maximum size
            buffer = new byte[Math.min((int) file.length(), maxBufferSize)];
            int length;
            // read file and write it into form...
            while ((length = fileInputStream.read(buffer)) != -1) {
                dos.write(buffer, 0, length);
            }

            for (String name : parameters.keySet()) {
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\""
                        + name + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(parameters.get(name));
            }

            // send multipart form data necessary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            dos.flush();
        } finally {
            if (fileInputStream != null)
                fileInputStream.close();
            if (dos != null)
                dos.close();
        }

        // ------------------ read the SERVER RESPONSE
        try {
            dis = new DataInputStream(conn.getInputStream());
            StringBuilder response = new StringBuilder();

            String line;
            while ((line = dis.readLine()) != null) {
                response.append(line).append('\n');
            }

            System.out.println("Upload file responce:"
                    + response.toString());
            return response.toString();
        } finally {
            if (dis != null)
                dis.close();
        }
    }
}
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47
  • I will wait and see whether its possible to just modify my code to make it work, because I dont wanna start all over again. This is really unfamiliar territory to me – Kaloyan Roussev Dec 27 '13 at 10:42
  • yeah sure.it works coz i use this code already and works fine – Kailash Dabhi Dec 27 '13 at 10:43
  • If it is really work , will be very good, but can you explain please what is it String fileParameterName, HashMap parameters, mFileName? Because i can't understand – Sirop4ik May 01 '16 at 15:18
  • its old code now i think we should not do that we have very nice libraries like OkHttp, Retrofit etc. so just use them would be very beneficial and you will have clean code. – Kailash Dabhi May 27 '16 at 06:15
0

If someone just cannot figure out what is going on with headers, take a look at this article http://develop-for-android.blogspot.com/2014/01/using-volley-in-your-application.html It just saved me the day.

Roman
  • 2,079
  • 4
  • 35
  • 53
0

Read the source of the file http. Check this solution:

  1. Call new MultipartEntity:

    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));

  2. Add request header

    heads.put("Content-Type", "image/png;charset=utf-8");

Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
Xuefei
  • 1