java讀取檔案以位元組流的形式讀取 然後轉化為位元組流陣列,最後經過SHA1加密生成返回16進位制字串存入資料庫
阿新 • • 發佈:2019-01-25
//根據產品圖片的url返回產品圖片的位元組流陣列
public static final byte[] input2byte(String Url) throws IOException {
InputStream in = null;
ByteArrayOutputStream byteOut = null;
byte[] in2b = null;
try {
in = new FileInputStream(new File(Url)); //建立位元組輸入流,路徑可以是絕對路徑,也可以是相對路徑(相對於專案工程為根目錄)
if(StringUtil.isNotEmpty(in)){
byteOut = new ByteArrayOutputStream();
byte[] buff = new byte[1024 * 4]; //buff用於存放迴圈讀取的臨時資料
int len = 0;
while ((len = in.read(buff)) != -1) { // read如果讀完了所有資料會返回-1。// 讀入緩衝區(輸入流中的)的位元組總數,如果因為已經到達檔案末尾而沒有更多的資料,則返回 -1
byteOut.write(buff, 0, len); // 把讀到的資料以追加的方式新增到記憶體流中 ,此方法寫入len個位元組到輸出流
in2b = byteOut.toByteArray();
}
}
} catch (IOException e) {
public static final byte[] input2byte(String Url) throws IOException {
InputStream in = null;
ByteArrayOutputStream byteOut = null;
byte[] in2b = null;
try {
in = new FileInputStream(new File(Url)); //建立位元組輸入流,路徑可以是絕對路徑,也可以是相對路徑(相對於專案工程為根目錄)
if(StringUtil.isNotEmpty(in)){
byteOut = new ByteArrayOutputStream();
byte[] buff = new byte[1024 * 4]; //buff用於存放迴圈讀取的臨時資料
int len = 0;
while ((len = in.read(buff)) != -1) { // read如果讀完了所有資料會返回-1。// 讀入緩衝區(輸入流中的)的位元組總數,如果因為已經到達檔案末尾而沒有更多的資料,則返回 -1
byteOut.write(buff, 0, len); // 把讀到的資料以追加的方式新增到記憶體流中 ,此方法寫入len個位元組到輸出流
in2b = byteOut.toByteArray();
}
}
} catch (IOException e) {