gpt4 book ai didi

android使用MultipartEntity上传图片

转载 作者:行者123 更新时间:2023-11-29 21:53:24 24 4
gpt4 key购买 nike

我正在尝试使用以下代码上传图片:

  HttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(
"http://konsole-data.de/uploadtest/upload.php");

MultipartEntity multiPart = new MultipartEntity();
multiPart.addPart("picture", new FileBody(new File(path)));

httpPost.setEntity(multiPart);
try {
HttpResponse res = httpClient.execute(httpPost);

Toast.makeText(getApplicationContext(),res.toString(),
Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

路径是一个字符串,用于标识图像,如/mnt/sdcard/DCIM/12712.jpg 连接正常但没有图像上传到服务器,您可以在此处查看调试文件:http://konsole-data.de/uploadtest/data/20121214-144802-.dbg
哪里做错了?

最佳答案

您可能应该指定 HttpMultipartMode 和文件的 MIME 类型(但我认为这不是必需的):

MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

FileBody bin = new FileBody(new File(path), "image/jpeg");
multipart.addPart("picture", bin);

编辑:

您还应该检查您是否使用了正确的路径。不是将 File 对象创建为匿名内部类:

File file = new File(path);
if(file.exists()){
FileBody bin = new FileBody(file, "image/jpeg");
multipart.addPart("picture", bin);
} else {
Log.w(YourClass.class.getSimpleName(), "File " + path + " doesn't exist!");
}

关于android使用MultipartEntity上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13880051/

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