现在的位置: 首页 > 综合 > 正文

android通过Base64往服务器上传图片和对象

2013年10月07日 ⁄ 综合 ⁄ 共 3297字 ⁄ 字号 评论关闭

在下载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的讲解能够对读者有帮助,如果有什么错误尽情读者批评之处,不胜感谢..
谢谢!



抱歉!评论已关闭.