點擊這裡在下載Base64.java文件,分別添加到客戶端和服務器端.
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的講解能夠對讀者有幫助,如果有什麼錯誤盡情讀者批評之處,不勝感謝。