android客服端上传图片到服务器,使用的xml来传输base64编码后的图片
我使用的是android自带的httpclient来发送post请求的,我也想过自己使用post方式来发送数据,但是,数据在服务器端进行base64解码的时候保存,我也没找出原因,所以就没写出来了

发送post请求就是因为post允许一次传输的数据量大,因为图片经过base64编码后,数据量大,如果采用get或者其他的方式来传输数据,传输效率不过,而且数据量大小受到限制

1.获取android客服端图片

Java代码 复制代码 收藏代码
  1. //对文件的操作
  2. FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/images/musicmax.png");
  3. byte buffer[] = StreamUtil.read(in);//把图片文件流转成byte数组
  4. byte[] encod = Base64.encode(buffer,Base64.DEFAULT);//使用base64编码


2.发送post请求,注意android客服端访问网络记得要加访问网络的权限

Java代码 复制代码 收藏代码
  1. String path ="http://192.168.1.173:7999/videonews/TestServlet";
  2. Map<String, String> params = new HashMap<String, String>();//定义一个保存key-value的Map用于保存需要传输的数据
  3. params.put("value", new String(encod));//保存数据到map对象
  4. Log.i(TAG,new String(encod));
  5. if(StreamUtil.sendHttpClientPOSTRequest(path, params, "utf-8")){//使用帮助类来发送HttpClient来发送post请求
  6. Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));
  7. }


2.服务器端的代码

Java代码 复制代码 收藏代码
  1. String value = request.getParameter("value");//获取value的值
  2. FileOutputStream fileout = new FileOutputStream("c:/music.png");//设置文件保存在服务器的什么位置
  3. fileout.write(com.sun.org.apache.xml.internal.security.utils.Base64.decode(value.getBytes()));//使用base64解码
  4. fileout.close();


StreamUtil帮助类里面完整代码

Java代码 复制代码 收藏代码
  1. public class StreamUtil {
  2. /**
  3. * 返回字节数组
  4. *
  5. * @param in输入的流
  6. * @return
  7. * @throws Exception
  8. */
  9. public static byte[] read(InputStream in) throws Exception {
  10. ByteArrayOutputStream out = new ByteArrayOutputStream();
  11. if (in != null) {
  12. byte[] buffer = new byte[1024];
  13. int length = 0;
  14. while ((length = in.read(buffer)) != -1) {
  15. out.write(buffer, 0, length);
  16. }
  17. out.close();
  18. in.close();
  19. return out.toByteArray();
  20. }
  21. return null;
  22. }
  23. /**
  24. * 采用HttpClient发送POST请求
  25. * @param path 请求路径
  26. * @param params 请求参数
  27. * @throws Exception
  28. */
  29. public static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
  30. List<NameValuePair> param = new ArrayList<NameValuePair>();
  31. if(params!=null && !params.isEmpty()){
  32. for(Map.Entry<String, String> entry : params.entrySet()){
  33. param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  34. }
  35. }
  36. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param, encoding);
  37. HttpPost post = new HttpPost(path);
  38. // HttpGet get = new HttpGet();
  39. post.setEntity(entity);
  40. DefaultHttpClient client = new DefaultHttpClient();
  41. HttpResponse response = client.execute(post);
  42. if(response.getStatusLine().getStatusCode() == 200){
  43. // response.getEntity().getContent();//获取服务器返回的数据
  44. return true;
  45. }
  46. return false;
  47. }
  48. }

关于自己写post请求的代码,这个代码我测试过,在服务器对传输过来的数据进行base64解码的时候总报错,具体的原因我也没找出来,下面我贴出来代码,希望朋友们帮我找找原因

Java代码 复制代码 收藏代码
  1. /*//对文件的操作 注:此方法测试有问题
  2. FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/images/musicmax.png");
  3. byte buffer[] = StreamUtil.read(in);
  4. byte[] encod = Base64.encode(buffer,Base64.DEFAULT);
  5. StringBuffer sb = new StringBuffer("value=");
  6. URL url = new URL(path);
  7. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  8. conn.setConnectTimeout(5 * 1000);
  9. conn.setRequestMethod("POST");
  10. conn.setDoOutput(true);//允许对外输出数据
  11. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  12. conn.setRequestProperty("Content-Length", (sb.toString().getBytes().length + encod.length) + "");
  13. OutputStream outs = conn.getOutputStream();
  14. outs.write(sb.toString().getBytes());
  15. outs.write(encod);
  16. outs.close();
  17. Log.i(TAG,new String(encod));
  18. if(conn.getResponseCode() == 200){
  19. Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));
  20. //下面的代码是测试是否解码后能生成对应的图片没
  21. // FileOutputStream fileout = new FileOutputStream(Environment.getExternalStorageDirectory() + "/images/musicmax1.png");
  22. // fileout.write(Base64.decode(encod, Base64.DEFAULT));
  23. // fileout.close();
  24. }

更多相关文章

  1. Android下通过hook技术实现透明加解密保障数据安全
  2. Android中打包含有Activity以及资源文件的jar包在工程中调用
  3. android开发中的数据库定义诀窍
  4. Android碎碎念 -- 性能测试,适用于Android Studio的代码审查和性
  5. Android缓存处理和清除数据、清除缓存、一键清理的区别
  6. 对系统学习与写博客的看法——学完《第一行代码》有感
  7. Android开发——纯JAVA代码方式界面设计

随机推荐

  1. Android中Context的传递
  2. Android(安卓)Activity生命周期举例说明(
  3. Kotlin集合—MutableList可变列表、Set、
  4. 【Android(安卓)进阶】Iconfont 图标的使
  5. Flutter ubuntu 环境搭建
  6. 2014.04.22 ——— android listview hea
  7. Android(安卓)Studio连接不上模拟器的解
  8. android 权限介绍(二)
  9. 蓝牙hid协议源码解析
  10. [原]android 链接错误