Android uploads images to server via Base64
Previously, I uploaded pictures using HttpServlet. However, after using Base64 to upload pictures, it feels much more convenient than HttpServlet. You can also try it.
Front-end image processing: (Just pass Bitmap object)
/** * Convert Bitmap to Base64 string via Base32 * @param bit * @return */ public String Bitmap2StrByBase64(Bitmap bit){ ByteArrayOutputStream bos=new ByteArrayOutputStream(); (, 40, bos);// Parameter 100 means no compression byte[] bytes=(); return (bytes, ); }
Front-end sending data: (Call setImgByStr() method, the first parameter imgStr is the string converted to Base64 by Bitmap, and the second parameter imgName is the name of the image, including the suffix name.jpg)
public static String host = "http://192.168.1.166:8080/ImageServer/"; public static String getContent(String url) throws Exception { StringBuilder sb = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpParams httpParams = (); // Set network timeout parameters (httpParams, 3000); (httpParams, 5000); HttpResponse response = (new HttpGet(url)); HttpEntity entity = (); if (entity != null) { BufferedReader reader = new BufferedReader(new InputStreamReader( (), "UTF-8"), 8192); String line = null; while ((line = ()) != null) { (line + "\n"); } (); } return (); } public static HttpResponse post(Map<String, Object> params, String url) { HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); ("charset", HTTP.UTF_8); ("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); HttpResponse response = null; if (params != null && () > 0) { List<NameValuePair> nameValuepairs = new ArrayList<NameValuePair>(); for (String key : ()) { (new BasicNameValuePair(key, (String) params .get(key))); } try { (new UrlEncodedFormEntity(nameValuepairs, HTTP.UTF_8)); response = (httpPost); } catch (UnsupportedEncodingException e) { (); } catch (ClientProtocolException e) { (); } catch (IOException e) { (); } catch (RuntimeException e) { (); } } else { try { response = (httpPost); } catch (ClientProtocolException e) { (); } catch (IOException e) { (); } } return response; } public static Object getValues(Map<String, Object> params, String url) { String token = ""; HttpResponse response = post(params, url); if (response != null) { try { token = (()); ("operator"); } catch (ParseException e) { (); } catch (IOException e) { (); } } return token; } public static Object setImgByStr(String imgStr,String imgName){ String url = host+""; Map<String,Object> params = new HashMap<String, Object>(); ("imgStr", imgStr); ("imgName", imgName); return getValues(params, url); }
Backend receiving data:
public void uploadPhoto() { //Get storage path HttpServletRequest request = (); String path = ().getRealPath("/")+"upload"; File file = new File(path); if(!()){ (); } String imgPath = path + ("imgName"); String imgStr = ("imgStr"); boolean flag = string2Image(imgStr, imgPath); (response, flag); }
Backstage image processing:
/** * Decode through BASE64Decoder and generate pictures * @param imgStr decoded string */ public boolean string2Image(String imgStr, String imgFilePath) { // Base64 decode the byte array string and generate the picture if (imgStr == null) return false; try { // Base64 decoding byte[] b = new BASE64Decoder().decodeBuffer(imgStr); for (int i = 0; i < ; ++i) { if (b[i] < 0) { // Adjust exception data b[i] += 256; } } // Generate Jpeg image OutputStream out = new FileOutputStream(imgFilePath); (b); (); (); return true; } catch (Exception e) { return false; } }
OK! If the front-end is successfully uploaded, it will receive true, otherwise it will fail false. Hope it will be helpful to everyone!