⑴ java中如何把圖片轉換成二進制流
Java中將圖片轉為二進制流只需要使用FileImageInputStream取得圖片文件,然後使用ByteArrayOutputStream 寫入到二進制流中即可,下面是詳細代碼:
//圖片到byte數組
publicbyte[]image2byte(Stringpath){
byte[]data=null;
FileImageInputStreaminput=null;
try{
input=newFileImageInputStream(newFile(path));
ByteArrayOutputStreamoutput=newByteArrayOutputStream();
byte[]buf=newbyte[1024];
intnumBytesRead=0;
while((numBytesRead=input.read(buf))!=-1){
output.write(buf,0,numBytesRead);
}
data=output.toByteArray();
output.close();
input.close();
}
catch(FileNotFoundExceptionex1){
ex1.printStackTrace();
}
catch(IOExceptionex1){
ex1.printStackTrace();
}
returndata;
}
另外,如果需要將byte[]存回圖片或轉為String,則:
//byte數組到圖片
publicvoidbyte2image(byte[]data,Stringpath){
if(data.length<3||path.equals(""))return;
try{
=newFileImageOutputStream(newFile(path));
imageOutput.write(data,0,data.length);
imageOutput.close();
System.out.println("MakePicturesuccess,Pleasefindimagein"+path);
}catch(Exceptionex){
System.out.println("Exception:"+ex);
ex.printStackTrace();
}
}
//byte數組到16進制字元串
publicStringbyte2string(byte[]data){
if(data==null||data.length<=1)return"0x";
if(data.length>200000)return"0x";
StringBuffersb=newStringBuffer();
intbuf[]=newint[data.length];
//byte數組轉化成十進制
for(intk=0;k<data.length;k++){
buf[k]=data[k]<0?(data[k]+256):(data[k]);
}
//十進制轉化成十六進制
for(intk=0;k<buf.length;k++){
if(buf[k]<16)sb.append("0"+Integer.toHexString(buf[k]));
elsesb.append(Integer.toHexString(buf[k]));
}
return"0x"+sb.toString().toUpperCase();
}
⑵ 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;
}
⑶ 請教,怎麼用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;
}
⑷ java里怎樣把文件轉換成二進制
轉換文件成為二進制數據並保存的Java代碼:
取出數據並還原文件到本地的java代碼:
[java]view plain//讀取資料庫二進制文件
publicvoidreaderJpg()throwsSQLException
{
connection=connectionManager.getconn();//自己連接自己的資料庫
StringsqlString="selectimagesfromsave_imagewhereid=4";//從資料庫中讀出要還原文件的二進制碼,這里我讀的是自己的資料庫id為4的文件
Filefile=newFile("E:\1.jpg");//本地生成的文件
if(!file.exists())
{
try{
file.createNewFile();
}catch(Exceptione){
e.printStackTrace();
}
}
try{
byte[]Buffer=newbyte[4096*5];
statement=connection.prepareStatement(sqlString);
resultSet=statement.executeQuery();
if(resultSet.next())
{
FileOutputStreamoutputStream=newFileOutputStream(file);
InputStreamiStream=resultSet.getBinaryStream("images");//去欄位用getBinaryStream()
intsize=0;
while((size=iStream.read(Buffer))!=-1)
{
System.out.println(size);
outputStream.write(Buffer,0,size);
}
}
}catch(Exceptione){
e.printStackTrace();
}
}