安卓客户端上传图片到服务器

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

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

1.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.   
  4. params.put("value"new String(encod));//保存数据到map对象  
  5. Log.i(TAG,new String(encod));  
  6. if(StreamUtil.sendHttpClientPOSTRequest(path, params, "utf-8")){//使用帮助类来发送HttpClient来发送post请求  
  7.  Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));  
  8. }  

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

 关于自己写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.                       
  6.                     StringBuffer sb = new StringBuffer("value=");  
  7.                     URL url = new URL(path);  
  8.                     HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  9.                       
  10.                     conn.setConnectTimeout(5 * 1000);  
  11.                     conn.setRequestMethod("POST");  
  12.                     conn.setDoOutput(true);//允许对外输出数据  
  13.                     conn.setRequestProperty("Content-Type""application/x-www-form-urlencoded");  
  14.                     conn.setRequestProperty("Content-Length", (sb.toString().getBytes().length + encod.length) + "");  
  15.                     OutputStream outs = conn.getOutputStream();  
  16.                                         outs.write(sb.toString().getBytes());  
  17.                     outs.write(encod);  
  18.                     outs.close();  
  19.                     Log.i(TAG,new String(encod));  
  20.                     if(conn.getResponseCode() == 200){  
  21.                                                 Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));  
  22. //下面的代码是测试是否解码后能生成对应的图片没  
  23. //                      FileOutputStream fileout = new FileOutputStream(Environment.getExternalStorageDirectory() + "/images/musicmax1.png");  
  24. //                      fileout.write(Base64.decode(encod, Base64.DEFAULT));  
  25. //                      fileout.close();  
  26.                     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值