將 oracle.sql.BLOB轉爲byte[] 輸出(java)

/**
 	 * 將blob轉化爲byte[],只對純文本的有效
 	 * @param blob
 	 * @return
 	 */
 	private byte[] getBytes(BLOB blob) {
 		try {
 			InputStream ins = blob.getBinaryStream();
 			byte[] b = new byte[1024];
 			int num = 0;
 			String res = "";
 			while ((num = ins.read(b)) != -1) {
 				res += new String(b, 0, num);
 			}
 			return res.getBytes();
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
 		return null;
 	}
 	
 	/**
 	 * 將blob轉化爲byte[],可以轉化二進制流的
 	 * 
 	 * @param blob
 	 * @return
 	 */
 	private byte[] blobToBytes(BLOB blob) {
 		InputStream is = null;
 		byte[] b = null;
 		try {
 			is = blob.getBinaryStream();
 			b = new byte[(int) blob.length()];
 			is.read(b);
 			return b;
 		} catch (Exception e) {
 			e.printStackTrace();
 		} finally {
 			try {
 				is.close();
 				is = null;
 			} catch (IOException e) {
 				e.printStackTrace();
 			}
 		}
 		return b;
 	}

發佈了180 篇原創文章 · 獲贊 7 · 訪問量 59萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章