java把图片转换成二进制流

Python019

java把图片转换成二进制流,第1张

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()//关闭输出流

}

解决方法:int len = fis.read()read 方法加入参数bys,这样才能把fis的内容注入bys里面。

顺便说下,FileInputStream不能正确输出中文,因为这个是按字节输出的,每个中文站2个字节,会出现乱码。

这个简单 你可以先读 读完之后在写出来么

public class BinaryOperation {

public static void main(String args[]){

FileInputStream fis = null

FileOutputStream fos = null

try {

fis = new FileInputStream("d:/图片/chenhl.jpg")

byte[] b = new byte[128]

fos = new FileOutputStream("d:/图片/chenhl 复件.jpg")

while(fis.read(b)!=-1){

fos.write(b)

}

} catch (Exception e) {

e.printStackTrace()

}finally{

try{

if(fis!=null) fis.close()

if(fos!=null) fos.close()

}catch(Exception e){

e.printStackTrace()

}

}

}

}