Android 通过HTTP POST 上传图片文件
2013-04-08 15:52
459 查看
/article/7675030.html
Sending images can be done using the HttpComponents
libraries. Download the latest HttpClient (currently4.0.1)
binary with dependencies package and copy
your project and add them to your Java build path.
You will need to add the following imports to your class.
Now you can create a
Sending images can be done using the HttpComponents
libraries. Download the latest HttpClient (currently4.0.1)
binary with dependencies package and copy
apache-mime4j-0.6.jarand
httpmime-4.0.1.jarto
your project and add them to your Java build path.
You will need to add the following imports to your class.
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody;
Now you can create a
MultipartEntityto attach an image to your POST request. The following code shows an example of how to do this:
public void post(String url, List<NameValuePair> nameValuePairs) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); try { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for(int index=0; index < nameValuePairs.size(); index++) { if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) { // If the key equals to "image", we use FileBody to transfer the data entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue()))); } else { // Normal string data entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue())); } } httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); } catch (IOException e) { e.printStackTrace(); } }
相关文章推荐
- Android 通过HTTP POST 上传图片文件
- Android 通过HTTP POST 上传图片文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- Android 通过post上传文件--HttpURLConnection实现
- android http通过post上传文件和提交参数(通过拼装协议)
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- Android 通过httppost上传文本文件到服务器的实例代码
- iOS通过http post上传图片, 文件等
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- android通过http上传文件(图片)
- Qt通过HTTP POST上传文件
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- Android Volley框架定制PostUploadRequest上传文件(图片)
- 通过 HTTP POST 上传文件到服务器
- iOS通过http post 文件流上传文件(头像)代码实现
- iOS通过http post上传图片
- iOS通过http post上传图片
- OS通过http post上传图片