位元組流轉換為字串的方法
阿新 • • 發佈:2019-01-30
private String streamToString(InputStream inputStream, String charset) { try { //轉換流,將位元組流轉字串,並轉換編碼 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset); //字元緩衝流 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); //字串緩衝區 StringBuilder stringBuilder = newStringBuilder(); // String len = null; //按行讀 while((len=bufferedReader.readLine())!=null){ //追加到字串緩衝區存放 stringBuilder.append(len); } //將字串返回 return stringBuilder.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }