gpt4 book ai didi

android - 发送带有多个参数的多部分/表单请求?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:03 31 4
gpt4 key购买 nike

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

当我发送

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"}

  • 所以我想知道为什么两个响应之间存在差异?
  • 我发送 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);

它奏效了。

关于android - 发送带有多个参数的多部分/表单请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22652274/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com