從遠程路徑讀取圖片,進行base64轉碼


public static byte[] getImageFromNetByUrl(String strUrl){  
        try {  
            URL url = new URL(strUrl);  
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            conn.setRequestMethod("GET");  
            conn.setConnectTimeout(5 * 1000);  
            InputStream inStream = conn.getInputStream();//通過輸入流獲取圖片數據  
            byte[] btImg = readInputStream(inStream);//得到圖片的二進制數據  
            return btImg;  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
    //由於讀取需要一定時間,所以不能單純往字節數組里讀,所以需要判斷是否讀完 public static byte[] readInputStream(InputStream inStream) throws Exception{
  //存放讀取的所有的字節數組 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len=inStream.read(buffer)) != -1 ){ outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } private static String getImageStr(String imgUrl) { byte[] data = null; try { data =getImageFromNetByUrl(imgUrl);; } catch (Exception e) { e.printStackTrace(); } BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM