91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android 通過Base64上傳圖片到服務器實現實例

發布時間:2020-08-26 20:56:15 來源:腳本之家 閱讀:485 作者:陋室銘 欄目:移動開發

Android 通過Base64上傳圖片到服務器

之前做上傳圖片是采用HttpServlet上傳,不過用了一下Base64上傳圖片后,感覺比HttpServlet方便很多,大家也可以跟著嘗試一下。

前臺圖片處理:(傳Bitmap對象即可)

/** 
 * 通過Base32將Bitmap轉換成Base64字符串 
 * @param bit 
 * @return 
 */ 
public String Bitmap2StrByBase64(Bitmap bit){ 
  ByteArrayOutputStream bos=new ByteArrayOutputStream(); 
  bit.compress(CompressFormat.JPEG, 40, bos);//參數100表示不壓縮 
  byte[] bytes=bos.toByteArray(); 
  return Base64.encodeToString(bytes, Base64.DEFAULT); 
} 

 前臺發送數據:(調用setImgByStr()方法,第一個參數imgStr 為Bitmap轉成Base64的字符串,第二個參數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(); 
  // 設置網絡超時參數 
  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); 
} 

 后臺接收數據:

public void uploadPhoto() { 
  //獲取存儲路徑 
  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); 
} 

 后臺圖片處理:


/** 
 * 通過BASE64Decoder解碼,并生成圖片 
 * @param imgStr 解碼后的string 
 */ 
public boolean string2Image(String imgStr, String imgFilePath) { 
  // 對字節數組字符串進行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) { 
        // 調整異常數據 
        b[i] += 256; 
      } 
    } 
    // 生成Jpeg圖片 
    OutputStream out = new FileOutputStream(imgFilePath); 
    out.write(b); 
    out.flush(); 
    out.close(); 
    return true; 
  } catch (Exception e) { 
    return false; 
  } 
}   

OK ! 如果成功上傳前端會接收到true ,反之失敗false。希望對大家有所幫助!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

登封市| 得荣县| 青河县| 乌兰察布市| 永安市| 区。| 马公市| 刚察县| 勃利县| 新竹市| 莱州市| 扎赉特旗| 庆阳市| 上高县| 石泉县| 久治县| 黎川县| 宁国市| 莆田市| 当涂县| 明星| 旬阳县| 广安市| 黄冈市| 阳春市| 界首市| 买车| 盖州市| 洛阳市| 汤阴县| 平遥县| 奈曼旗| 哈巴河县| 晋江市| 南漳县| 资阳市| 马边| 新巴尔虎左旗| 巫溪县| 大竹县| 富顺县|