关于android图片的传输,android图片传输方式,xml传输图片,android 使用base64编码图片使用xml传输图片

本文介绍了一种使用Android客户端将图片以Base64编码形式通过HTTP POST请求上传至服务器的方法,并详细展示了客户端和服务端的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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编码
  //对文件的操作
  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客服端访问网络记得要加访问网络的权限

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. }
 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.服务器端的代码

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();
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帮助类里面完整代码

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. }
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代码 复制代码 收藏代码
  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. }
/*//对文件的操作  注:此方法测试有问题
					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();
					}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值