使用多个参数发送Multipart / Form请求?

时间:2014-03-26 05:32:34

标签: android apache android-networking androidhttpclient

我正在尝试将图片上传到服务器,我会从服务器获得不同的响应,具体取决于我发送的实体。

当我发送

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

我得到的回复是{"status":"Success","code":"200","message":"File has been uploaded Successfully"}

如果我以这种方式发送相同的文件

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

我现在得到的回复是{"status":"Failure","code":"501","message":"Invalid File to upload"}

  • 所以我想知道为什么两个回答之间存在差异 ?
  • 我是否以正确的方式发送了帖子请求参数?
  • 以同样的方式发布视频我该怎么做?

以下是完整的参考代码

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());
    }

}

1 个答案:

答案 0 :(得分:4)

我能够解决这个问题,所以我发布这个答案,因为它可能会帮助其他人面对同样的问题。

问题是我上传图片文件的服务器只解析了扩展名为 JPEG,PNG,GIF 等的文件,而忽略了其余的文件。

我以ByteArrayBody形式发送的图片文件的文件名属性没有扩展名,因此会导致此问题。

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

我用

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

并且有效。

相关问题