android 上传图片
2013-10-31 22:27
330 查看
public static String uploadPicture(String url, String uploadFile) {
String resultcode = "1";// 0表示成功 ,1表示失败
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(url);
File file = new File(uploadFile); // DEBUG
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpg");
ContentBody cbMessage;
try {
cbMessage = new StringBody("TEST TSET");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
}
ContentBody cbAccessToken;
try {
cbAccessToken = new StringBody("sfsdfsdf");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
}
mpEntity.addPart("source", cbFile);// 上传文件
// mpEntity.addPart("message", cbMessage);// 上传信息
// mpEntity.addPart("access_token", cbAccessToken);// 上传信息
httppost.setEntity(mpEntity); // DEBUG
HttpResponse response;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
}
HttpEntity resEntity = response.getEntity(); // DEBUG
if (resEntity != null) {
try {
if (EntityUtils.toString(resEntity) == "1") {
resultcode = "0";
}
} catch (org.apache.http.ParseException e) {
e.printStackTrace();
return e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
httpclient.getConnectionManager().shutdown();
return resultcode;
}
String resultcode = "1";// 0表示成功 ,1表示失败
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(url);
File file = new File(uploadFile); // DEBUG
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpg");
ContentBody cbMessage;
try {
cbMessage = new StringBody("TEST TSET");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
}
ContentBody cbAccessToken;
try {
cbAccessToken = new StringBody("sfsdfsdf");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
}
mpEntity.addPart("source", cbFile);// 上传文件
// mpEntity.addPart("message", cbMessage);// 上传信息
// mpEntity.addPart("access_token", cbAccessToken);// 上传信息
httppost.setEntity(mpEntity); // DEBUG
HttpResponse response;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
}
HttpEntity resEntity = response.getEntity(); // DEBUG
if (resEntity != null) {
try {
if (EntityUtils.toString(resEntity) == "1") {
resultcode = "0";
}
} catch (org.apache.http.ParseException e) {
e.printStackTrace();
return e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
httpclient.getConnectionManager().shutdown();
return resultcode;
}
相关文章推荐
- android之调用webservice 实现图片上传
- android 选择本地图片上传到服务端并保存
- android调用webservice实现图片上传
- Android 批量上传sd卡图片
- Android 上传图片到服务器(PHP)
- Android 通过Base64上传图片到服务器
- android 上传图片到服务器Tomcat(Struts2)
- Android图片上传队列Service
- Android开发:相册读取、拍照、图片裁剪和图片上传服务器等功能的实现
- 【Android实战】----基于Retrofit实现多图片/文件、图文上传
- Android拍照显示图片并上传遇到的诸多问题
- Android Retrofit 2.0框架上传图片解决方案
- Android实现文件, 图片上传,以及服务端接收相关
- Android 图片压缩上传
- android上传图片到PHP后台全过程
- android 图片上传,使用 httpclient 的MultipartEntity 上传
- Android选择图片或拍照图片上传到服务器(包括上传参数)
- Android文件图片上传的详细讲解(四)---服务端编码
- android选择图片或拍照图片上传到服务器(包括上传参数)
- 在 Android 上通过模拟 HTTP multipart/form-data 请求协议信息实现图片上传