gpt4 book ai didi

android - 如何使用http客户端在Web Api中上传图片?

转载 作者:太空狗 更新时间:2023-10-29 16:40:51 28 4
gpt4 key购买 nike

我想使用名称、 block 、文件数据参数将我的文件从 android 设备发送到 web api。我有这段代码:它不会对我返回任何错误我不知道我做错了什么。请帮助我花了很多时间,但它仍然不起作用。

FileBody localFileBody = new FileBody(new File("/storage/sdcard0/Pictures/Images/angel_productshot.jpg"),
"image/jpg");

MultipartEntity localMultipartEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
HttpConnectionParams.setSoTimeout(httpParameters, 15000);
HttpPost httpPost = new HttpPost(
"http://phdsafar4.myserver.com.ph:1217/api/fileupload");
HttpClient httpclient = new DefaultHttpClient(httpParameters);
httpPost.addHeader("Authorization","Basic " + Base64.encodeToString(
(username + ":" + password).getBytes(), Base64.NO_WRAP));

httpPost.setHeader("Content-Type", "multipart/form-data");
httpPost.setHeader("Accept", "application/image");
localMultipartEntity.addPart("name", new StringBody(filename));
localMultipartEntity.addPart("chunk", new StringBody("1"));
localMultipartEntity.addPart("file data", localFileBody);
httpPost.setEntity(localMultipartEntity);
HttpResponse localHttpResponse = httpclient.execute(httpPost);
Log.i("response upload", localHttpResponse.toString());
Log.i("Multipart Entity", localMultipartEntity.toString());

最佳答案

this work for me try it...
to use MultipartEntity must be add this two jar file in your project lib
1. httpmime-4.0.1.jar
2. apache-mime4j-0-.6.jar

MultipartEntity multipartEntity = new MultipartEntity();
File image1 = new File(imagePath);
multipartEntity.addPart("Image1",image1);
File image2 = new File(imagePath2);
multipartEntity.addPart("Image2",image2);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Url);
post.setEntity(multipartEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
response_str = EntityUtils.toString(resEntity);

关于android - 如何使用http客户端在Web Api中上传图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18072596/

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