当前位置: 技术问答>java相关
如何将数据库中的二进制的流文件还原回来?
来源: 互联网 发布时间:2015-07-11
本文导语: 请各位大侠告诉小弟如何如何将数据库中的二进制的流文件还原回来?小弟不胜感激! | ResultSet rs=stmt.exectuteQuery(sql); if(rs.next()) { response.setContectType("image/gif"); InputStream in=...
请各位大侠告诉小弟如何如何将数据库中的二进制的流文件还原回来?小弟不胜感激!
|
ResultSet rs=stmt.exectuteQuery(sql);
if(rs.next())
{
response.setContectType("image/gif");
InputStream in=rs.getBinaryStream(1);
Outputstream out=response.getOutputStream();
byte buffer[]=new byte[4096];
for( ; ;)
{
int nBytes=in.read(buffer);
if(nBytes==-1)
break;
out.write(buffer,0,nBytes);
}
in.close();
out.flush();
out.close();
}
if(rs.next())
{
response.setContectType("image/gif");
InputStream in=rs.getBinaryStream(1);
Outputstream out=response.getOutputStream();
byte buffer[]=new byte[4096];
for( ; ;)
{
int nBytes=in.read(buffer);
if(nBytes==-1)
break;
out.write(buffer,0,nBytes);
}
in.close();
out.flush();
out.close();
}
|
看Java2核心技术1最后一章,必有收获!!!
|
http://www.csdn.net/expert/topic/83/83096.xml?temp=.1578791
|
生成图片好像是需要特殊格式的,这样那,
private void createImage(String fileLocation)
{
try
{
String flag = makeDirs(fileLocation.substring(0,fileLocation.lastIndexOf("/")));
FileOutputStream fos = new FileOutputStream(fileLocation);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
}
catch(Exception e)
{
System.out.println("CREATE FORECAST IMAGE ERROR...."+e.toString());
}
}
private void createImage(String fileLocation)
{
try
{
String flag = makeDirs(fileLocation.substring(0,fileLocation.lastIndexOf("/")));
FileOutputStream fos = new FileOutputStream(fileLocation);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
}
catch(Exception e)
{
System.out.println("CREATE FORECAST IMAGE ERROR...."+e.toString());
}
}