其他分享
首页 > 其他分享> > android – 使用多个参数发送Multipart / Form请求?

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

作者:互联网

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

当我发送

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”:“文件已成功上传”}

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

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”:“失败”,“代码”:“501”,“消息”:“要上传的文件无效”}

>所以我想知道为什么两个回答之间存在差异

>我是以正确的方式发送帖子请求参数吗?
>以同样的方式发布视频我需要做什么?

以下是完整的参考代码

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形式发送的图像文件的文件名属性没有扩展名,因此导致了这个问题.

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

它起作用了.

标签:apache,android,android-networking,androidhttpclient
来源: https://codeday.me/bug/20190624/1279472.html