/** * 上传 * * @param pathList */ public static void upload(List<String> pathList, String url) { String files = "FileUpload:"; url = NetManager.HTTP_DOMAIN + url; for (int i = 0; i < pathList.size(); i++) { Log.i("HttpUrlEncodedFormEntityPost", "filePath:" + pathList.get(i)); // 根据路径生成一个Bitmap Bitmap tBitmap = convertToBitmap(pathList.get(i), 400, 400); // 把Bitmap写进流里面 ByteArrayOutputStream stream = new ByteArrayOutputStream(); tBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); // 把流转化为数组 byte[] b = stream.toByteArray(); // 将图片流以字符串形式存储下来 String file = new String(Base64Coder.encodeLines(b)); // 设置一条分割线 files += "---------------------------7da2137580612"; // 累加每一个文件转化成的String数据 files += file; } HttpClient client = new DefaultHttpClient(); // 设置上传参数 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("file", files)); HttpPost post = new HttpPost(url); UrlEncodedFormEntity entity; try { entity = new UrlEncodedFormEntity(formparams, "UTF-8"); post.addHeader("Accept", "text/javascript, text/html, application/xml, text/xml"); post.addHeader("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3"); post.addHeader("Accept-Encoding", "gzip,deflate,sdch"); post.addHeader("Connection", "Keep-Alive"); post.addHeader("Cache-Control", "no-cache"); String BOUNDARY = "----------" + System.currentTimeMillis(); post.setHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); // post.addHeader("Content-Type", "multipart/form-data"); post.setEntity(entity); HttpResponse response = client.execute(post); Log.v( "HttpUrlEncodedFormEntityPost", "StatusCode: " + response.getStatusLine().getStatusCode()); HttpEntity e = response.getEntity(); Log.v("HttpUrlEncodedFormEntityPost", "response :" + EntityUtils.toString(e)); if (200 == response.getStatusLine().getStatusCode()) { // Toast.makeTast(Activity,"上传完成",1000).show; } else { // Toast.makeTast(Activity,"上传失败",1000).show(); } client.getConnectionManager().shutdown(); } catch (Exception e) { e.printStackTrace(); } }
public void upload() { ByteArrayOutputStream stream = new ByteArrayOutputStream(); String picPath = ""; Bitmap upbitmap = new BitmapFactory().decodeFile(picPath); upbitmap.compress(Bitmap.CompressFormat.JPEG, 60, stream); byte[] b = stream.toByteArray(); // 将图片流以字符串形式存储下来 String file = new String(Base64Coder.encodeLines(b)); HttpClient client = new DefaultHttpClient(); // 设置上传参数 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("contract", file)); formparams.add(new BasicNameValuePair("buyMoney", "213")); HttpPost post = new HttpPost(URLs.FEEDBACK_URL); UrlEncodedFormEntity entity; try { entity = new UrlEncodedFormEntity(formparams, "UTF-8"); post.addHeader("Accept", "text/javascript, text/html, application/xml, text/xml"); post.addHeader("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3"); post.addHeader("Accept-Encoding", "gzip,deflate,sdch"); post.addHeader("Connection", "Keep-Alive"); post.addHeader("Cache-Control", "no-cache"); post.addHeader("Content-Type", "application/x-www-form-urlencoded"); post.setEntity(entity); HttpResponse response = client.execute(post); System.out.println(response.getStatusLine().getStatusCode()); HttpEntity e = response.getEntity(); System.out.println(EntityUtils.toString(e)); if (200 == response.getStatusLine().getStatusCode()) { System.out.println("上传完成"); } else { System.out.println("上传失败"); } client.getConnectionManager().shutdown(); } catch (Exception e) { e.printStackTrace(); } }