Download the base64.java File
Http://iharder.sourceforge.net/current/java/base64/, do not go to the client and server.
1> we know that files can be uploaded on the Web through Apache projects, so you can upload files to the server in Android, but you can also use base64,
This is equivalent to uploading a string to the server, and then decoding the interface through base64.decode () on the server side. The returned byte array byte []
On 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()); } }}
On server side:
String result = request.getParameter("image") byte[] result = Base64.decode() OutputStream out = new FileOutputStream("C:\\a.png"); out.write(result); out.close();
Test result: Find the following file on drive C:
2> similarly, we can also pass objects to the server on the client side. (This is to pass multiple objects to the server side, it is easier to pass a single object) on the 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 class 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 class public class collect implements serializable {public person [] pS; Public collect (person [] PS) {super (); this. PS = Ps;} in 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) COM. ieheima. servlet. base64.decodetoobject (image); person [] PS = collect. PS; system. out. println ("Length:" + PS. length); For (INT I = 0; I <ps. length; I ++) {system. out. println (PS [I]. getname () ;}} catch (classnotfoundexception e) {e. printstacktrace () ;}} outputs the result on the console:
It should be noted that the same class collect and person must be available on the server, and the package name must be the same. If the transmitted object is too large, it may cause memory overflow.
You also need to set a serialversionuid for the class that implements the serializable interface.
I hope the above base64 explanation will be helpful to the readers. If you have any mistakes, I would like to thank you for your criticism. For more information, see http://blog.csdn.net/johnny901114/article/details/7542564. thank you!