httpPost中上传图片 httpclient实现
大家好:我想在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转换并且添加到参数中?
谢谢
--------------------编程问答-------------------- 图片是用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;
} --------------------编程问答-------------------- 自己搞定了 --------------------编程问答-------------------- 兄弟,搞定了也分享一下。 --------------------编程问答-------------------- 楼主不厚道啊,搞定了也不分享一下 --------------------编程问答-------------------- 应该是没有问题的,看下服务器 --------------------编程问答-------------------- 是啊 搞定了不分享下~ --------------------编程问答-------------------- LZ不厚道............... --------------------编程问答-------------------- 怎么搞定的啊?求楼主分享代码,我也急需要用啊 --------------------编程问答-------------------- 真心不厚道啊……同求啊……
补充:移动开发 , Android