当前位置: 技术问答>java相关
怎么样把BLOB从数据库中取出?(马上给分)
来源: 互联网 发布时间:2017-04-14
本文导语: 各位帮帮忙,怎么样吧存在数据库中的PDF文档取出并存放在本地? 还有通知BROWER用什么APPLICATION打开它? 给个实例看看. 分不够再加 先谢谢了! | 我的主要思路是将它先取出来,存到本地某文件中,然后再...
各位帮帮忙,怎么样吧存在数据库中的PDF文档取出并存放在本地?
还有通知BROWER用什么APPLICATION打开它?
给个实例看看.
分不够再加
先谢谢了!
还有通知BROWER用什么APPLICATION打开它?
给个实例看看.
分不够再加
先谢谢了!
|
我的主要思路是将它先取出来,存到本地某文件中,然后再读到网页上。
具体操作如下:
下为我的一段代码,上个星期刚用上的。
public String getResult() throws Exception
{
con.setAutoCommit(false);
stmt = con.createStatement();
rs=stmt.executeQuery(sql);
File f=new File("e:\temp.txt");
if(rs.next())
{
Blob blob=rs.getBlob(5);
in =((oracle.sql.BLOB) blob).getBinaryStream();
int bufferSize=(int)((oracle.sql.BLOB) blob).getBufferSize();
out=new BufferedOutputStream(new FileOutputStream(f),bufferSize);
byte[] b=new byte[bufferSize];
int count=in.read(b,0,bufferSize);
while(count!=-1)
{
out.write(b,0,count);
count=in.read(b,0,bufferSize);
}
out.close();
out=null;
in.close();
in=null;
cMessage="写文件完毕!";
}
if(cMessage==null)
{cMessage="写文件出错!";con.rollback();}
return cMessage;
}
你可以再从文件中读到你需要的地方。或者直接将文件名以参数形式读进来。然后写到文件中
具体操作如下:
下为我的一段代码,上个星期刚用上的。
public String getResult() throws Exception
{
con.setAutoCommit(false);
stmt = con.createStatement();
rs=stmt.executeQuery(sql);
File f=new File("e:\temp.txt");
if(rs.next())
{
Blob blob=rs.getBlob(5);
in =((oracle.sql.BLOB) blob).getBinaryStream();
int bufferSize=(int)((oracle.sql.BLOB) blob).getBufferSize();
out=new BufferedOutputStream(new FileOutputStream(f),bufferSize);
byte[] b=new byte[bufferSize];
int count=in.read(b,0,bufferSize);
while(count!=-1)
{
out.write(b,0,count);
count=in.read(b,0,bufferSize);
}
out.close();
out=null;
in.close();
in=null;
cMessage="写文件完毕!";
}
if(cMessage==null)
{cMessage="写文件出错!";con.rollback();}
return cMessage;
}
你可以再从文件中读到你需要的地方。或者直接将文件名以参数形式读进来。然后写到文件中
|
Insert a RTF-Document into Database and get it out again:
Column type is IMAGE:
Example-program: WriteTextToDB.java , DisplayDocumentFromDatabase.java
http://www.jalice.net/writetexttodb.htm
http://www.jalice.net/DisplayDocumentFromDatabaseOverview.html
Column type is IMAGE:
Example-program: WriteTextToDB.java , DisplayDocumentFromDatabase.java
http://www.jalice.net/writetexttodb.htm
http://www.jalice.net/DisplayDocumentFromDatabaseOverview.html
|
给你个全的,即存为文件,又回写到客户端的Browser上。
自己根据需要取舍吧。
private void showPdf(HttpServletResponse response,Blob blob,String fileName) {
try {
// Open a stream to read the Blob data
InputStream blobStream = blob.getBinaryStream();
// Open a file stream to save the Blob data
FileOutputStream fileOutStream = new FileOutputStream(fileName);
//Get a outputStreat from resposne
response.setContentType("application/pdf");
OutputStream out = response.getOutputStream();
// Read from the Blob data input stream, and write to the file output
// stream
byte[] buffer = new byte[10]; // buffer holding bytes to be transferred
int nbytes = 0; // Number of bytes read
while( (nbytes = blobStream.read(buffer)) != -1 ) // Read from Blob stream
{
fileOutStream.write(buffer, 0, nbytes); // Write to file stream
out.write(buffer, 0, nbytes); // Write to response outputStream
}
// Flush and close the streams
out.flush();
out.close();
fileOutStream.flush();
fileOutStream.close();
blobStream.close();
} catch( Exception ex ) { // Trap SQL and IO errors
System.out.println(" Error in retrieving and saving data!");
System.out.println("n" + ex.toString());
}
}
自己根据需要取舍吧。
private void showPdf(HttpServletResponse response,Blob blob,String fileName) {
try {
// Open a stream to read the Blob data
InputStream blobStream = blob.getBinaryStream();
// Open a file stream to save the Blob data
FileOutputStream fileOutStream = new FileOutputStream(fileName);
//Get a outputStreat from resposne
response.setContentType("application/pdf");
OutputStream out = response.getOutputStream();
// Read from the Blob data input stream, and write to the file output
// stream
byte[] buffer = new byte[10]; // buffer holding bytes to be transferred
int nbytes = 0; // Number of bytes read
while( (nbytes = blobStream.read(buffer)) != -1 ) // Read from Blob stream
{
fileOutStream.write(buffer, 0, nbytes); // Write to file stream
out.write(buffer, 0, nbytes); // Write to response outputStream
}
// Flush and close the streams
out.flush();
out.close();
fileOutStream.flush();
fileOutStream.close();
blobStream.close();
} catch( Exception ex ) { // Trap SQL and IO errors
System.out.println(" Error in retrieving and saving data!");
System.out.println("n" + ex.toString());
}
}
|
先取出:ResultSet.getBytes(int columnIndex)或者getBlob(String colName)
再用BufferedOutputStream(new FileOutputStream).write(byte[] b)写就可以了。
很简单。
再用BufferedOutputStream(new FileOutputStream).write(byte[] b)写就可以了。
很简单。
|
用Entity Bean,在Jbuilder里面能够自动识别出来是byte[]类型的,可以把对象序列化成byte[]存到数据库中去,任何实现Serialized接口的类都可以。
|
oracle.sql.CLOB clob1=oracle.sql.CLOB.empty_lob();
if (rset1.next())
{
clob1 = (oracle.sql.CLOB)rset1.getClob(i);
}
if (rset1.next())
{
clob1 = (oracle.sql.CLOB)rset1.getClob(i);
}