上传文件之至客户端处理http请求
来源:互联网 发布:一个爱心的软件 编辑:程序博客网 时间:2024/06/05 08:52
Android客户端主要代码:
public ImageHttp(Context context) {super(context);filePath = context.getCacheDir().getAbsolutePath();}public void uploadUserImg(Bitmap bitmap,IHttpSenderCallBack<String> callback) {this.callback = callback;File file = getFile(bitmap);// 注意与服务端协商if (file == null) {return;}List<Parameter> parameters = new ArrayList<Parameter>();parameters.add(new Parameter("file", file));uploadFile("uploadImg", parameters);}private File getFile(Bitmap bitmap) {File file = null;String path = filePath + "/image.jpg";if (bitmap != null && !StringUtil.emptyOrNull(path)) {try {FileOutputStream out = new FileOutputStream(path);bitmap.compress(Bitmap.CompressFormat.JPEG, 60, out);out.flush();out.close();file = new File(path);} catch (FileNotFoundException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}return file;}
protected String uploadFile(String methodName, List<Parameter> parameter) {String errorMsg = "";try {String serviceUrl = getPostUrl(methodName);HttpPost httpPost = new HttpPost(serviceUrl);//采用http4包,不需要再添加 multipart/form-data表单 ,切记,这儿耽搁了好久MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); // 文件传输if (parameter != null && !parameter.isEmpty()) {for (int i = 0; i < parameter.size(); i++) {Parameter p = parameter.get(i);if ("file".equals(p.getKey())) {mpEntity.addPart(p.getKey(),new FileBody(((File) p.getValue())));} else {mpEntity.addPart(p.getKey(), new StringBody(p.getValue().toString()));}}httpPost.setEntity(mpEntity);}HttpClient httpclient = new DefaultHttpClient();httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT);httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT);HttpResponse httpResponse = httpclient.execute(httpPost);String result = "";int status = httpResponse.getStatusLine().getStatusCode();LogUtil.d("httpResponseCode--------->" + status);if (status == HttpStatus.SC_OK) {result = EntityUtils.toString(httpResponse.getEntity());}parseProperty_(result, methodName);result = "";} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return errorMsg;}
0 0
- 上传文件之至客户端处理http请求
- jmeter之http请求-如何上传文件
- http请求上传文件
- Servlet处理客户端HTTP请求
- Android之Http请求实例(文件上传+servlet接收)
- jmeter之http请求-文件上传脚本编写
- Java客户端通过Http发送POST请求上传文件到web服务器
- Servlet之HTTP请求处理
- .net模拟http post请求上传文件
- Http get、post请求、文件上传
- .net模拟http post请求上传文件
- python发HTTP post请求上传文件
- HttpClient发送http请求以及上传文件
- Java 发送http请求,上传文件
- Socket实现发送Http请求上传文件
- HTTP文件上传请求格式详解,利用HttpURLConnection上传文件
- 使用multipart请求处理文件上传
- 使用multipart请求处理文件上传
- chrome 沙箱
- 使用Maven配置JBoss、Wildfly数据源的方法
- 编程之美 2.4 1的数目
- DLNA简介
- 第十四周项目四之电子词典
- 上传文件之至客户端处理http请求
- clone方法的实现
- android 关联源码,support包源码
- javaweb之Filter详解
- VC++深入详解笔记 C++类的设计习惯及头文件重复包含问题的解决
- 流行界面库
- java.util.Date和java.sql.Date的区别及应用
- 中缀表达式转换为前缀及后缀表达式并求值(java实现)
- linux、apache、svn服务器的搭建