Android Post图片和数据
2010-08-23 01:25:00 来源:WEB开发网发送
1 @SuppressWarnings("deprecation") 2 public static boolean postImg(String url, String u, String c, File file){ 3 LogUtil.info("" + file.exists()); 4 5 PostMethod postMethod = new PostMethod(url); 6 7 Part[] part = new Part[4]; 8 part[0] = new StringPart("u", u); 9 part[1] = new StringPart("c", c);10 try {11 part[2] = new FilePart("Filedata", file);12 } catch (FileNotFoundException ex) {13 ex.printStackTrace();14 Log.e("file exception", ex.getMessage());15 }16 part[3] = new StringPart("flag", "image");17 18 MultipartRequestEntity mrp = new MultipartRequestEntity(part, postMethod.getParams());19 postMethod.setRequestEntity(mrp);20 org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();21 client.getParams().setContentCharset("utf-8");22 try {23 client.executeMethod(postMethod);24 if("false".equals(postMethod.getRequestEntity().toString())){25 return false;26 }27 } catch (HttpException e) {28 e.printStackTrace();29 return false;30 } catch (IOException e) {31 e.printStackTrace();32 return false;33 }finally {34 if (client != null) {35 client = null;36 }37 if (postMethod != null) {38 postMethod = null;39 }40 }41 return true;42 }
更多精彩
赞助商链接