在下载Base64.java文件 http://iharder.sourceforge.net/current/java/base64/,分别添加到客户端和服务器端.


1>我们知道在web中实现文件上传可以通过apache的项目,那么在android中把文件上传到服务器端,当然也可以通过该方式,但是也可以通过base64,
这样就相当于把一个字符串传到服务器,然后在服务器端通过Base64.decode()方法解码接口,返回的字节数组byte[]


在android side:
public class MainActivity extends Activity {    InputStream is = null;     @Override    public void onCreate(Bundle icicle) {         super.onCreate(icicle);         setContentView(R.layout.main);         Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),         R.drawable.a1);         ByteArrayOutputStream bao = new ByteArrayOutputStream();         bitmapOrg.compress(Bitmap.CompressFormat.PNG, 90, bao);         byte[] ba = bao.toByteArray();         String ba1 = Base64.encodeBytes(ba);         ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();         nameValuePairs.add(new BasicNameValuePair("image", ba1));         try {             HttpClient httpclient = new DefaultHttpClient();             HttpPost httppost = new             HttpPost(                    "http://192.168.0.101:8080/ServletClassloadTest/servlet/UploadImage");             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));             HttpResponse response = httpclient.execute(httppost);             HttpEntity entity = response.getEntity();             is = entity.getContent();         } catch (Exception e) {             Log.e("log_tag", "Error in http connection " + e.toString());         }     }}


在server side:
        String result = request.getParameter("image")                byte[] result = Base64.decode()        OutputStream out = new FileOutputStream("C:\\a.png");        out.write(result);        out.close();


测试结果:在C盘找到如下文件
2>同理我们也可以在客户端把对象传递到服务器端.(这是把多个对象传递给服务器端,传递单个对象更加简单) 在android side:
public void onCreate(Bundle icicle) {         super.onCreate(icicle);        setContentView(R.layout.main);        Collect conCollect = new Collect(new Person[]{new Person("yzq",12),new Person("johnny",21)});        String ba1 = null;        try {            ba1 = Base64.encodeObject(conCollect);        } catch (IOException e) {            e.printStackTrace();        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();        nameValuePairs.add(new BasicNameValuePair("image", ba1));        try {            HttpClient httpclient = new DefaultHttpClient();            HttpPost httppost = new            HttpPost(                    "http://192.168.0.101:8080/ServletClassloadTest/servlet/UploadImage");            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));            HttpResponse response = httpclient.execute(httppost);            HttpEntity entity = response.getEntity();            is = entity.getContent();        } catch (Exception e) {            Log.e("log_tag", "Error in http connection " + e.toString());        }    }

Person类public class Person implements Serializable{     private String name;    private int age;    public Person(String name, int age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}Collect 类public class Collect implements Serializable{     public Person[] ps;    public Collect(Person[] ps) {        super();        this.ps = ps;    }}在server side:public void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {        response.setContentType("text/html");        String image = request.getParameter("image");        System.out.println("result"+image);        try {            Collect collect = (Collect)com.ieheima.servlet.Base64.decodeToObject(image);            Person[] ps = collect.ps;            System.out.println("长度:"+ps.length);            for (int i = 0; i < ps.length; i++) {                System.out.println(ps[i].getName());            }        } catch (ClassNotFoundException e) {            e.printStackTrace();        }}在控制台输出结果:

需要注意的是在服务器端也要有相同的类Collect和Person,同时包名也要一样.如果传输的对象过大,可能会导致内存溢出. 还需要给实现了Serializable接口的类,定一个一个serialVersionUID

希望以上Base64的讲解能够对读者有帮助,如果有什么错误尽情读者批评之处,不胜感谢.. 转载请注明出处:http://blog.csdn.net/johnny901114/article/details/7536764 谢谢!



更多相关文章

  1. 某技术大牛的帖子(android项目总结)
  2. Android性能优化《Android开发艺术探索》笔记
  3. android之js与java互相调用
  4. Android实现带图标的ListView
  5. Android基础(14)SurfaceView
  6. Android(安卓)组件系列之Activity的传值和回传值
  7. LocalSocket实现进程间通信
  8. Android(安卓)Binder入门指南之Binder Native Service的Java调用
  9. Android写一个简单的欢迎界面

随机推荐

  1. android键盘事件
  2. android Scroller
  3. Android Activity启动慢
  4. Android滑动卡片效果:Swipecards
  5. android 手势识别
  6. Android中RadioGroup组与onCheckedChange
  7. Android Studio无法执行Java类的main方法
  8. Android 实现在Java代码中修改UI界面,并修
  9. Android API Differences Report
  10. Android ApiDemos示例解析(100):Views->A