关于android图片的传输,android图片传输方式,xml传输图片,android 使用base64编码图片使用xml传输图片
16lz
2021-01-23
android客服端上传图片到服务器,使用的xml来传输base64编码后的图片
我使用的是android自带的httpclient来发送post请求的,我也想过自己使用post方式来发送数据,但是,数据在服务器端进行base64解码的时候保存,我也没找出原因,所以就没写出来了
发送post请求就是因为post允许一次传输的数据量大,因为图片经过base64编码后,数据量大,如果采用get或者其他的方式来传输数据,传输效率不过,而且数据量大小受到限制
1.获取android客服端图片
Java代码- //对文件的操作
- FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/images/musicmax.png");
- byte buffer[] = StreamUtil.read(in);//把图片文件流转成byte数组
- byte[] encod = Base64.encode(buffer,Base64.DEFAULT);//使用base64编码
//对文件的操作 FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/images/musicmax.png"); byte buffer[] = StreamUtil.read(in);//把图片文件流转成byte数组 byte[] encod = Base64.encode(buffer,Base64.DEFAULT);//使用base64编码
2.发送post请求,注意android客服端访问网络记得要加访问网络的权限
- String path ="http://192.168.1.173:7999/videonews/TestServlet";
- Map<String, String> params = new HashMap<String, String>();//定义一个保存key-value的Map用于保存需要传输的数据
- params.put("value", new String(encod));//保存数据到map对象
- Log.i(TAG,new String(encod));
- if(StreamUtil.sendHttpClientPOSTRequest(path, params, "utf-8")){//使用帮助类来发送HttpClient来发送post请求
- Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));
- }
String path ="http://192.168.1.173:7999/videonews/TestServlet"; Map<String, String> params = new HashMap<String, String>();//定义一个保存key-value的Map用于保存需要传输的数据 params.put("value", new String(encod));//保存数据到map对象 Log.i(TAG,new String(encod)); if(StreamUtil.sendHttpClientPOSTRequest(path, params, "utf-8")){//使用帮助类来发送HttpClient来发送post请求 Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT))); }
2.服务器端的代码
- String value = request.getParameter("value");//获取value的值
- FileOutputStream fileout = new FileOutputStream("c:/music.png");//设置文件保存在服务器的什么位置
- fileout.write(com.sun.org.apache.xml.internal.security.utils.Base64.decode(value.getBytes()));//使用base64解码
- fileout.close();
String value = request.getParameter("value");//获取value的值 FileOutputStream fileout = new FileOutputStream("c:/music.png");//设置文件保存在服务器的什么位置 fileout.write(com.sun.org.apache.xml.internal.security.utils.Base64.decode(value.getBytes()));//使用base64解码 fileout.close();
StreamUtil帮助类里面完整代码
- public class StreamUtil {
- /**
- * 返回字节数组
- *
- * @param in输入的流
- * @return
- * @throws Exception
- */
- public static byte[] read(InputStream in) throws Exception {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- if (in != null) {
- byte[] buffer = new byte[1024];
- int length = 0;
- while ((length = in.read(buffer)) != -1) {
- out.write(buffer, 0, length);
- }
- out.close();
- in.close();
- return out.toByteArray();
- }
- return null;
- }
- /**
- * 采用HttpClient发送POST请求
- * @param path 请求路径
- * @param params 请求参数
- * @throws Exception
- */
- public static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
- List<NameValuePair> param = new ArrayList<NameValuePair>();
- if(params!=null && !params.isEmpty()){
- for(Map.Entry<String, String> entry : params.entrySet()){
- param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
- }
- }
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param, encoding);
- HttpPost post = new HttpPost(path);
- // HttpGet get = new HttpGet();
- post.setEntity(entity);
- DefaultHttpClient client = new DefaultHttpClient();
- HttpResponse response = client.execute(post);
- if(response.getStatusLine().getStatusCode() == 200){
- // response.getEntity().getContent();//获取服务器返回的数据
- return true;
- }
- return false;
- }
- }
public class StreamUtil { /** * 返回字节数组 * * @param in输入的流 * @return * @throws Exception */ public static byte[] read(InputStream in) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (in != null) { byte[] buffer = new byte[1024]; int length = 0; while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } out.close(); in.close(); return out.toByteArray(); } return null; } /** * 采用HttpClient发送POST请求 * @param path 请求路径 * @param params 请求参数 * @throws Exception */ public static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{ List<NameValuePair> param = new ArrayList<NameValuePair>(); if(params!=null && !params.isEmpty()){ for(Map.Entry<String, String> entry : params.entrySet()){ param.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param, encoding); HttpPost post = new HttpPost(path);// HttpGet get = new HttpGet(); post.setEntity(entity); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post); if(response.getStatusLine().getStatusCode() == 200){// response.getEntity().getContent();//获取服务器返回的数据 return true; } return false; }}
关于自己写post请求的代码,这个代码我测试过,在服务器对传输过来的数据进行base64解码的时候总报错,具体的原因我也没找出来,下面我贴出来代码,希望朋友们帮我找找原因
Java代码- /*//对文件的操作 注:此方法测试有问题
- FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/images/musicmax.png");
- byte buffer[] = StreamUtil.read(in);
- byte[] encod = Base64.encode(buffer,Base64.DEFAULT);
- StringBuffer sb = new StringBuffer("value=");
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5 * 1000);
- conn.setRequestMethod("POST");
- conn.setDoOutput(true);//允许对外输出数据
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- conn.setRequestProperty("Content-Length", (sb.toString().getBytes().length + encod.length) + "");
- OutputStream outs = conn.getOutputStream();
- outs.write(sb.toString().getBytes());
- outs.write(encod);
- outs.close();
- Log.i(TAG,new String(encod));
- if(conn.getResponseCode() == 200){
- Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));
- //下面的代码是测试是否解码后能生成对应的图片没
- // FileOutputStream fileout = new FileOutputStream(Environment.getExternalStorageDirectory() + "/images/musicmax1.png");
- // fileout.write(Base64.decode(encod, Base64.DEFAULT));
- // fileout.close();
- }
/*//对文件的操作 注:此方法测试有问题FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/images/musicmax.png");byte buffer[] = StreamUtil.read(in);byte[] encod = Base64.encode(buffer,Base64.DEFAULT);StringBuffer sb = new StringBuffer("value=");URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5 * 1000);conn.setRequestMethod("POST");conn.setDoOutput(true);//允许对外输出数据conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");conn.setRequestProperty("Content-Length", (sb.toString().getBytes().length + encod.length) + "");OutputStream outs = conn.getOutputStream();outs.write(sb.toString().getBytes());outs.write(encod);outs.close();Log.i(TAG,new String(encod));if(conn.getResponseCode() == 200){Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));//下面的代码是测试是否解码后能生成对应的图片没//FileOutputStream fileout = new FileOutputStream(Environment.getExternalStorageDirectory() + "/images/musicmax1.png");//fileout.write(Base64.decode(encod, Base64.DEFAULT));//fileout.close();}
更多相关文章
- Android下通过hook技术实现透明加解密保障数据安全
- Android中打包含有Activity以及资源文件的jar包在工程中调用
- android开发中的数据库定义诀窍
- Android碎碎念 -- 性能测试,适用于Android Studio的代码审查和性
- Android缓存处理和清除数据、清除缓存、一键清理的区别
- 对系统学习与写博客的看法——学完《第一行代码》有感
- Android开发——纯JAVA代码方式界面设计