InputStream 就是讀取二進制文件的, 任何文件都可以用這個流來讀, 也叫位元組輸入流
2. 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();
(2)java圖片二進制類型擴展閱讀:
如果是純文本使用字元流,如果二進制文件,使用位元組流。
如果只是得到信息,原樣不動,不進行修改操作,例如文件上傳和下載,這時就使用位元組流。文件上傳:在伺服器端把瀏覽器端信息提取出來。文件下載:把伺服器端內容寫給瀏覽器端。
如果要操作的是自定義信息,這時使用字元流。
通過response獲取的輸出流它的真實類型是什麼?
ServletOutputStream response.getOutputStream();
PrintWriter response.getWriter();
ServletOutputStream由於使用位元組流多數是原樣復制,所以使用write方法,而不是print方法。
PrintWriter:列印流,兩個特點:1.可以設置自動刷新。2.可以將信息原樣輸出。
3. java中怎樣把圖片轉換成二進制
File file = new File("C:/Program Files/HITACHI/EUR Print Service/IMAGE/aaa.jpg");
byte[] bbb = new byte[10240];
try {
InputStream a = new FileInputStream(file);
a.read(bbb);
System.out.println(bbb[0]);
System.out.println(Integer.toBinaryString(bbb[0]));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我這里是一次把圖片全讀進來,所以定義的數組長度為10240,也就是10kb吧,當然你也可以定義小點,然後分次讀。反正二進制都在這個數組裡面,最後兩句我只是舉了個例子教你怎麼看這個二進制,第一個是現實二進制對應的整數,第二個是顯示二進制了。
4. Java中如何把圖片轉換成二進制流
使用java的IO流對圖片進行二進制讀取操作即可
示例為:讀取圖片為二進制流,並寫入到其他圖片中
staticvoidtestCopyImage(){
Filesource=newFile("E:\share\Wallpaper\Bliss.jpg");
Filedesk=newFile("d:\images");
if(!desk.exists()){
desk.mkdir();
}
try{
FileInputStreaminputStream=newFileInputStream(source);
FileOutputStreamoutputStream=newFileOutputStream(newFile("d:/images/Bliss.jpg"));
intch=inputStream.read();
while(ch!=-1){
outputStream.write(ch);
ch=inputStream.read();
}
inputStream.close();
outputStream.close();
System.out.println("圖片復製成功!");
}catch(FileNotFoundExceptione){
System.out.println("文件不存在:"+e.getMessage());
}catch(IOExceptione){
System.out.println("文件讀取錯誤:"+e.getMessage());
}
}
5. java如何把圖片轉換成二進制並存到oracle的blob中,求代碼
importjavax.imageio.ImageIO;
importjava.awt.image.BufferedImage;
importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.IOException;
publicclassImageUtils{
publicstaticvoidmain(String[]args){
Stringstr=img2Binary("C:\Users\hny\Desktop\favicon.jpg");
System.out.println(str);
binary2Img("C:\Users\hny\Desktop\favicon2.jpg",str);
}
/**
*圖片轉二進制字元串
*
*@圖片路徑
*@return
*/
publicstaticStringimg2Binary(Stringpath){
Filefile=newFile(path);
if(!file.exists()){
returnnull;
}
try{
BufferedImagebi=ImageIO.read(file);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
Stringsuffix=getSuffix(path);
ImageIO.write(bi,suffix,baos);
byte[]bytes=baos.toByteArray();
returnnewsun.misc.BASE64Encoder().encodeBuffer(bytes).trim();
}catch(IOExceptione){
e.printStackTrace();
}
returnnull;
}
/**
*字元串轉圖片文件
*
*@parampath圖片路徑
*@paramimgBinary圖片字元串
*/
publicstaticvoidbinary2Img(Stringpath,StringimgBinary){
try{
Filefile=newFile(path);
byte[]bytes1=newsun.misc.BASE64Decoder().decodeBuffer(imgBinary);
ByteArrayInputStreams=newByteArrayInputStream(bytes1);
BufferedImagebi1=ImageIO.read(s);
Stringsuffix=getSuffix(path);
ImageIO.write(bi1,suffix,file);
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*獲取圖片後綴名
*
*@parampath
*@return
*/
privatestaticStringgetSuffix(Stringpath){
intindex=path.contains(".")?path.lastIndexOf("."):-1;
if(index>-1){
returnpath.substring(index+1);
}
returnnull;
}
}
6. 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();//關閉輸出流
}