本文目錄一覽:
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怎麼實現讀取一個文件,拿到二進位流
Java讀取二進位文件,以位元組為單位進行讀取,還可讀取圖片、音樂文件、視頻文件等,
在Java中,提供了四種類來對文件進行操作,分別是InputStream OutputStream Reader Writer ,前兩種是對位元組流的操作,後兩種則是對字元流的操作。
示例代碼如下:
public static void readFileByBytes(String fileName){
File file = new File(fileName);
InputStream in = null;
try {
System.out.println(“一次讀一個”);
// 一次讀一個位元組
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/187960.html