Domanda

I am trying to upload a picture to a server and i am getting different responses from the server depending upon the entity that i am sending.

When i send

FileBody fb = new FileBody(new File(filePath), "image/jpeg");
StringBody contentString = new StringBody(directoryID + "");

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", fb);
entity.addPart("directory_id", contentString);
postRequest.setEntity(entity);

HttpResponse response = httpClient.execute(postRequest);
// Read the response
String jsonString = EntityUtils.toString(response.getEntity());

The response i get is {"status":"Success","code":"200","message":"File has been uploaded Successfully"}

if i send the same file in this way

Bitmap bitmapOrg = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] data = bao.toByteArray();

StringBody contentString = new StringBody(directoryID + "");
ByteArrayBody ba = new ByteArrayBody(data, "image/jpeg", "file");

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", ba);
entity.addPart("directory_id", contentString);

postRequest.setEntity(entity);
HttpResponse response = httpClient.execute(postRequest);
// Read the response
String jsonString = EntityUtils.toString(response.getEntity());

The response that i get now is {"status":"Failure","code":"501","message":"Invalid File to upload"}

  • So i was wondering Why is there difference between the two responses ?
  • Am i sending the post request parameter the right way ?
  • What do i have to do to post video in the same way ?

Below is the complete code for reference

public void uploadFile(int directoryID, String filePath) {
    Bitmap bitmapOrg = BitmapFactory.decodeFile(filePath);
    ByteArrayOutputStream bao = new ByteArrayOutputStream();

    String upload_url = BASE_URL + UPLOAD_FILE;
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

    byte[] data = bao.toByteArray();

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(upload_url);
    MultipartEntity entity = new MultipartEntity(
            HttpMultipartMode.BROWSER_COMPATIBLE);

    try {
        // Set Data and Content-type header for the image
        FileBody fb = new FileBody(new File(filePath), "image/jpeg");
        ByteArrayBody ba = new ByteArrayBody(data, "image/jpeg", "file");
        StringBody contentString = new StringBody(directoryID + "");

        // If i do this 
        entity.addPart("file", fb);
        // I get a valid response

        // If i do this
        entity.addPart("file", ba);
        // I get an invalid response

        entity.addPart("directory_id", contentString);
        postRequest.setEntity(entity);

        HttpResponse response = httpClient.execute(postRequest);
        // Read the response
        String jsonString = EntityUtils.toString(response.getEntity());
        Log.e("response after uploading file ", jsonString);

    } catch (Exception e) {
        Log.e("Error in uploadFile", e.getMessage());
    }

}
È stato utile?

Soluzione

I was able to solve this problem so i am posting this answer because it might help others facing the same issue.

The problem was that the server to which i was uploading the picture file only parsed files having extensions JPEG, PNG, GIF etc and it ignored the rest of the files.

The image file that i was sending in the form of ByteArrayBody had the filename attribute without extension which as a result lead to this problem.

ByteArrayBody ba = new ByteArrayBody(data, "image/jpeg", "file");
entity.addPart("file", ba);

I replaced it with

ByteArrayBody ba = new ByteArrayBody(data, "image/jpeg", "file.jpeg");
entity.addPart("file", ba);

and it worked.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top