Android 通過Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例
Android 通過Base64上傳圖片到服務(wù)器
之前做上傳圖片是采用HttpServlet上傳,不過用了一下Base64上傳圖片后,感覺比HttpServlet方便很多,大家也可以跟著嘗試一下。
前臺(tái)圖片處理:(傳Bitmap對(duì)象即可)
/** * 通過Base32將Bitmap轉(zhuǎn)換成Base64字符串 * @param bit * @return */ public String Bitmap2StrByBase64(Bitmap bit){ ByteArrayOutputStream bos=new ByteArrayOutputStream(); bit.compress(CompressFormat.JPEG, 40, bos);//參數(shù)100表示不壓縮 byte[] bytes=bos.toByteArray(); return Base64.encodeToString(bytes, Base64.DEFAULT); }
前臺(tái)發(fā)送數(shù)據(jù):(調(diào)用setImgByStr()方法,第一個(gè)參數(shù)imgStr 為Bitmap轉(zhuǎn)成Base64的字符串,第二個(gè)參數(shù)imgName為圖片的名字,包含后綴名.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 = client.getParams(); // 設(shè)置網(wǎng)絡(luò)超時(shí)參數(shù) HttpConnectionParams.setConnectionTimeout(httpParams, 3000); HttpConnectionParams.setSoTimeout(httpParams, 5000); HttpResponse response = client.execute(new HttpGet(url)); HttpEntity entity = response.getEntity(); if (entity != null) { BufferedReader reader = new BufferedReader(new InputStreamReader( entity.getContent(), "UTF-8"), 8192); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } reader.close(); } return sb.toString(); } public static HttpResponse post(Map<String, Object> params, String url) { HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("charset", HTTP.UTF_8); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); HttpResponse response = null; if (params != null && params.size() > 0) { List<NameValuePair> nameValuepairs = new ArrayList<NameValuePair>(); for (String key : params.keySet()) { nameValuepairs.add(new BasicNameValuePair(key, (String) params .get(key))); } try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuepairs, HTTP.UTF_8)); response = client.execute(httpPost); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); } } else { try { response = client.execute(httpPost); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return response; } public static Object getValues(Map<String, Object> params, String url) { String token = ""; HttpResponse response = post(params, url); if (response != null) { try { token = EntityUtils.toString(response.getEntity()); response.removeHeaders("operator"); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return token; } public static Object setImgByStr(String imgStr,String imgName){ String url = host+"channel-uploadImage.action"; Map<String,Object> params = new HashMap<String, Object>(); params.put("imgStr", imgStr); params.put("imgName", imgName); return getValues(params, url); }
后臺(tái)接收數(shù)據(jù):
public void uploadPhoto() { //獲取存儲(chǔ)路徑 HttpServletRequest request = ServletActionContext.getRequest(); String path = ServletActionContext.getServletContext().getRealPath("/")+"upload"; File file = new File(path); if(!file.exists()){ file.mkdir(); } String imgPath = path + request.getParameter("imgName"); String imgStr = request.getParameter("imgStr"); boolean flag = string2Image(imgStr, imgPath); JacksonUtil.responseJSON(response, flag); }
后臺(tái)圖片處理:
/** * 通過BASE64Decoder解碼,并生成圖片 * @param imgStr 解碼后的string */ public boolean string2Image(String imgStr, String imgFilePath) { // 對(duì)字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成圖片 if (imgStr == null) return false; try { // Base64解碼 byte[] b = new BASE64Decoder().decodeBuffer(imgStr); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) { // 調(diào)整異常數(shù)據(jù) b[i] += 256; } } // 生成Jpeg圖片 OutputStream out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } }
OK ! 如果成功上傳前端會(huì)接收到true ,反之失敗false。希望對(duì)大家有所幫助!
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android Retrofit 2.0框架上傳圖片解決方案
- Android實(shí)現(xiàn)上傳圖片至java服務(wù)器
- android上傳圖片到PHP的過程詳解
- Android 開發(fā) 使用WebUploader解決安卓微信瀏覽器上傳圖片中遇到的bug
- Android基于OkHttp實(shí)現(xiàn)下載和上傳圖片
- Android異步上傳圖片到PHP服務(wù)器
- Android使用OkHttp上傳圖片的實(shí)例代碼
- Android實(shí)現(xiàn)點(diǎn)擊圖片上傳SQLite數(shù)據(jù)庫
相關(guān)文章
Android實(shí)現(xiàn)計(jì)算器(計(jì)算表達(dá)式/計(jì)算小數(shù)點(diǎn)以及括號(hào))
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)計(jì)算器功能,計(jì)算表達(dá)式,能計(jì)算小數(shù)點(diǎn)以及括號(hào),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問題解決
這篇文章主要介紹了AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10用xutils3.0進(jìn)行下載項(xiàng)目更新
這篇文章主要介紹了用xutils3.0進(jìn)行下載項(xiàng)目更新的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08Android自定義view實(shí)現(xiàn)日歷打卡簽到
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)日歷打卡簽到,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Android編程之文件讀寫操作與技巧總結(jié)【經(jīng)典收藏】
這篇文章主要介紹了Android編程之文件讀寫操作與技巧,結(jié)合實(shí)例形式總結(jié)分析了Android常見的文件與目錄的讀寫操作,及相關(guān)函數(shù)的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Android 三種延遲操作的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 延遲操作的實(shí)現(xiàn)方法的相關(guān)資料,這里提供了三種實(shí)現(xiàn)方法,希望能幫助到大家,需要的朋友可以參考下2017-08-08Android開發(fā)時(shí)盡管已root但是ddms還是沒有data路徑怎么辦
這篇文章主要介紹了Android開發(fā)時(shí)盡管已root但是ddms還是沒有data路徑怎么辦的相關(guān)資料,需要的朋友可以參考下2015-12-12Android Jetpack 狠活Lifecycles與LiveData使用詳解
這篇文章主要為大家介紹了Android Jetpack 狠活Lifecycles與LiveData使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10