本文目錄一覽:
- 1、java把圖片轉換成二進制流
- 2、Java中如何把圖片轉換成二進制流
- 3、java web二進制流的圖片如何用response返回給前台
- 4、Java怎麼做到把圖片轉換成流存入數據庫,然後怎麼再把圖片顯示出來。
java把圖片轉換成二進制流
public static void main(String[] args) throws Exception {
File file = new File(“d:\\L.jpg”);//圖片
FileInputStream fis = new FileInputStream(file);//把圖片變成流
FileOutputStream fos = new FileOutputStream(new File(“E:\\L.jpg”)); //把圖片流寫入E盤
byte[] read = new byte[1024]; //每次讀取的字節 可以自己定義 256 512 1024 2048 等。。。
int len = 0;
while((len = fis.read(read))!= -1){ //讀取變成流的圖片
fos.write(read,0,len);//寫入圖片
}
fis.close();//關閉輸入流
fos.close();//關閉輸出流
}
Java中如何把圖片轉換成二進制流
Java中將圖片轉為二進制流只需要使用FileImageInputStream取得圖片文件,然後使用ByteArrayOutputStream 寫入到二進制流中即可,下面是詳細代碼:
//圖片到byte數組
public byte[] image2byte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
}
catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
另外,如果需要將byte[]存回圖片或轉為String,則:
//byte數組到圖片
public void byte2image(byte[] data,String path){
if(data.length3||path.equals(“”)) return;
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println(“Make Picture success,Please find image in ” + path);
} catch(Exception ex) {
System.out.println(“Exception: ” + ex);
ex.printStackTrace();
}
}
//byte數組到16進制字符串
public String byte2string(byte[] data){
if(data==null||data.length=1) return “0x”;
if(data.length200000) return “0x”;
StringBuffer sb = new StringBuffer();
int buf[] = new int[data.length];
//byte數組轉化成十進制
for(int k=0;kdata.length;k++){
buf[k] = data[k]0?(data[k]+256):(data[k]);
}
//十進制轉化成十六進制
for(int k=0;kbuf.length;k++){
if(buf[k]16) sb.append(“0″+Integer.toHexString(buf[k]));
else sb.append(Integer.toHexString(buf[k]));
}
return “0x”+sb.toString().toUpperCase();
}
java web二進制流的圖片如何用response返回給前台
FileOutputStream很明顯你是用的文件流返回的
// 以byte流的方式打開文件 d:\1.gif
FileInputStream hFile = new FileInputStream(url); //得到文件大小
int i=hFile.available();
byte data[]=new byte[i]; //讀數據
hFile.read(data); //得到向客戶端輸出二進制數據的對象
OutputStream toClient=response.getOutputStream(); //輸出數據
toClient.write(data);
toClient.flush();
toClient.close();
hFile.close();
擴展資料:
如果是純文本使用字符流,如果二進制文件,使用字節流。
如果只是得到信息,原樣不動,不進行修改操作,例如文件上傳和下載,這時就使用字節流。文件上傳:在服務器端把瀏覽器端信息提取出來。文件下載:把服務器端內容寫給瀏覽器端。
如果要操作的是自定義信息,這時使用字符流。
通過response獲取的輸出流它的真實類型是什麼?
ServletOutputStream response.getOutputStream();
PrintWriter response.getWriter();
ServletOutputStream由於使用字節流多數是原樣複製,所以使用write方法,而不是print方法。
PrintWriter:打印流,兩個特點:1.可以設置自動刷新。2.可以將信息原樣輸出。
Java怎麼做到把圖片轉換成流存入數據庫,然後怎麼再把圖片顯示出來。
oracle如下
數據庫中提供了兩種字段類型 Blob 和 Clob 用於存儲大型字符串或二進制數據(如圖片)。
Blob 採用單字節存儲,適合保存二進制數據,如圖片文件。
Clob 採用多字節存儲,適合保存大型文本數據。
首先創建一個空 Blob/Clob 字段,再從這個空 Blob/Clob字段獲取游標,例如下面的代碼:
PreparedStatement ps = conn.prepareStatement( ” insert into PICTURE(image,resume) values(?,?) ” );
// 通過oralce.sql.BLOB/CLOB.empty_lob()構造空Blob/Clob對象
ps.setBlob( 1 ,oracle.sql.BLOB.empty_lob());
ps.setClob( 2 ,oracle.sql.CLOB.empty_lob());
ps.excuteUpdate();
ps.close();
// 再次對讀出Blob/Clob句柄
ps = conn.prepareStatement( ” select image,resume from PICTURE where id=? for update ” );
ps.setInt( 1 , 100 );
ResultSet rs = ps.executeQuery();
rs.next();
oracle.sql.BLOB imgBlob = (oracle.sql.BLOB)rs.getBlob( 1 );
oracle.sql.CLOB resClob = (oracle.sql.CLOB)rs.getClob( 2 );
// 將二進制數據寫入Blob
FileInputStream inStream = new FileInputStream( ” c://image.jpg ” );
OutputStream outStream = imgBlob.getBinaryOutputStream();
byte [] buf = new byte [ 10240 ];
int len;
while (len = inStream.read(buf) 0 ) {
outStream.write(buf, 0 ,len);
}
inStream.close();
outStream.cloese();
// 將字符串寫入Clob
resClob.putString( 1 , ” this is a clob ” );
// 再將Blob/Clob字段更新到數據庫
ps = conn.prepareStatement( ” update PICTURE set image=? and resume=? where id=? ” );
ps.setBlob( 1 ,imgBlob);
ps.setClob( 2 ,resClob);
ps.setInt( 3 , 100 );
ps.executeUpdate();
ps.close();
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/287407.html