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