In android projects, many applications such as qq, Sina Weibo, and fetion have the need to upload portraits.
Two articles I have written earlier: selecting a photo from the album/taking a photo under andorid and combining them with the conversion between Bitmap and base64 in android, you can select an Avatar from the album or photo, and then crop the Avatar before uploading it.
Download complete source code
Here I only paste the method code for uploading images. For how to obtain the obtained images, see select a photo from the album/take a photo under andorid and cut it
/** Upload image */public void upload (View view) {try {ByteArrayOutputStream out = new ByteArrayOutputStream (); bitmap. compress (Bitmap. compressFormat. JPEG, 100, out); out. flush (); out. close (); byte [] buffer = out. toByteArray (); byte [] encode = Base64.encode (buffer, Base64.DEFAULT); String photo = new String (encode); RequestParams params = new RequestParams (); params. put ("photo", photo); String url = "http: // 110.65. 99.66: 8080/jerry/UploadImgServlet "; AsyncHttpClient client = new AsyncHttpClient (); client. post (url, params, new AsyncHttpResponseHandler () {@ Overridepublic void onSuccess (int statusCode, Header [] headers, byte [] responseBody) {try {if (statusCode = 200) {Toast. makeText (MainActivity. this, "the Avatar is uploaded successfully! ", 0 ). show ();} else {Toast. makeText (MainActivity. this, "Network Access exception, error code:" + statusCode, 0 ). show () ;}} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}@ Overridepublic void onFailure (int statusCode, Header [] headers, byte [] responseBody, Throwable error) {Toast. makeText (MainActivity. this, "Network Access exception, error code>" + statusCode, 0 ). show () ;}) ;}catch (Exception e) {e. printStackTrace ();}}
From the code above, we can see that I use my favorite android-async-http android asynchronous http framework to access the network.
In this way, the image is transmitted to the server in Base64 encoding format, and the server code is also very simple.
Package com. jerry. servlet; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com.sun.org. apache. xml. internal. security. exceptions. base64DecodingException; import com.sun.org. apache. xml. internal. securit Y. utils. base64; public class UploadImgServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html"); String photo = request. getParameter ("photo"); try {// decodes base64 data byte [] decode = Base64.decode (photo); File File = new File ("e: \ decode.jpg"); if (! File. exists () {file. createNewFile ();} FileOutputStream out = new FileOutputStream (file); out. write (decode); out. flush (); out. close ();} catch (Base64DecodingException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this. doGet (request, response );}}
In this way, the image is successfully saved to the drive with an edisk.