android HttpClient 上传图片
16lz
2021-01-23
- import java.io.File;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpVersion;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.FileEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.CoreProtocolPNames;
- import org.apache.http.util.EntityUtils;
- public class PostFile {
- public static void main(String[] args) throws Exception {
- HttpClient httpclient = new DefaultHttpClient();
- httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
- HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
- File file = new File("c:/TRASH/zaba_1.jpg");
- FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
- httppost.setEntity(reqEntity);
- reqEntity.setContentType("binary/octet-stream");
- System.out.println("executing request " + httppost.getRequestLine());
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity resEntity = response.getEntity();
- System.out.println(response.getStatusLine());
- if (resEntity != null) {
- System.out.println(EntityUtils.toString(resEntity));
- }
- if (resEntity != null) {
- resEntity.consumeContent();
- }
- httpclient.getConnectionManager().shutdown();
- }
- }
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer