Java 數據流轉換工具類
import java.io.ByteArrayOutputStream;
import java.io.InputStream;/** * 數據流轉換工具類 * */ public class StreamUtil { /** * 從輸入流中獲取數據 * @param inStream 輸入流 * @return * @throws Exception */ public static byte[] readStreamToByte(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(); } /** * 從輸入流中獲取數據 * @param inStream 輸入流 * @return * @throws Exception */ public static String readStreamToString(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.toString(); } /** * 將輸入流轉化成某字符編碼的String * @param inStream 輸入流 * @param encoding 編碼 * @return * @throws Exception */ public static String readStreamToString(InputStream inStream, String encoding) 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 new String(outStream.toByteArray(), encoding); } } </pre><br />
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!