httpPost中上传图片 httpclient实现

futurexuxun 2011-07-27 10:52:34
大家好:
我想在Android软件中实现上传图片的功能。Post上传,用httpclient实现

相关代码如下:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

List<BasicNameValuePair> result = new ArrayList<BasicNameValuePair>();
//添加参数
BasicNameValuePair pair = new BasicNameValuePair("status", “文本信息”);
result.add(pair);

httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);

图片文件形式为:
File file = new File("/sdcard/picture.jpg");

请问各位高手,如何将图片file转换并且添加到参数中?
谢谢

...全文
4601 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
han_wang521 2013-03-23
  • 打赏
  • 举报
回复
真心不厚道啊……同求啊……
影子大 2013-03-14
  • 打赏
  • 举报
回复
怎么搞定的啊?求楼主分享代码,我也急需要用啊
jacarri 2012-11-30
  • 打赏
  • 举报
回复
LZ不厚道...............
fkwcowboy 2012-02-16
  • 打赏
  • 举报
回复
是啊 搞定了不分享下~
j_f0001 2011-12-25
  • 打赏
  • 举报
回复
应该是没有问题的,看下服务器
Qiqihar 2011-12-22
  • 打赏
  • 举报
回复
楼主不厚道啊,搞定了也不分享一下
  • 打赏
  • 举报
回复
兄弟,搞定了也分享一下。
liveths 2011-11-15
  • 打赏
  • 举报
回复
自己搞定了
米德文特 2011-11-14
  • 打赏
  • 举报
回复
图片是用post上传数据流,直接给你代码看吧

public boolean postFile(String url, File f) {
if (url == null || f == null) {
System.out.println("URL或者文件为NULL");
return false;
}
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = null;
try {
FileEntity entity = new FileEntity(f, "binary/octet-stream");
httpPost.setEntity(entity);
response = client.execute(httpPost);
} catch (Exception e) {
System.out.println(e.toString());
} finally {
}

// 判断上传的状态和打印调试信息
if (response != null
&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 打印调试信息,上传的url和上传的文件大小
System.out.println(MessageFormat.format(
"upload picture success! url = [{0}],file size = [{1}]", url,
f.length()));
return true;
}else
{
System.out.println(response.getStatusLine().getStatusCode());
}
return false;
}

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧