Android 发送包含多个参数的多部分/表单请求?

Android 发送包含多个参数的多部分/表单请求?,android,apache,android-networking,androidhttpclient,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); e

我正在尝试将图片上传到服务器,根据发送的实体,我从服务器获得不同的响应

当我发送

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 upload 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”}

  • 所以我想知道为什么这两种反应之间有差异 ?
  • 我是否以正确的方式发送post请求参数
  • 我要怎么做才能以同样的方式发布视频
下面是完整的代码供参考

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

}

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

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

我以
ByteArrayBody
的形式发送的图像文件具有filename属性,没有扩展名,因此导致了此问题

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);
它成功了