oracle blob字段中存储的是图片,请问怎样用java 代码读取并在jsp上显示
表字段 id int 、name varchar2 、content blob
在此谢过!
表字段 id int 、name varchar2 、content blob
在此谢过!
解决方案 »
- web 项目中的 <a href="c:\a.xls">dd</a> 如何用excel打开?
- 求jspsmartupload包
- 两个按钮····点击跳转两个页面····怎么做?
- 请问struts2 <s:doubleselect 标签怎么让他不换行
- 高手救急:MD5 在JSP和ASP之间的匹配
- 正则表达式.急!
- 急等 大神解决
- 如何把“5a”转换成“%35a”
- 传递参数时中文的处理问题!!!!!!!!!!
- jsp怎么连接access数据库呀
- 各位大哥们,你们好,我的问题是:No Java compiler was found to compile the generated source for the JSP
- String 字符串中特殊字符的查找以及更改的问题.
package test;import java.sql.*;
import java.io.*;
import oracle.sql.*;
public class WriteBlob {
public static void main(String[] args){
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhos:1251:test","test","test");
conn.setAutoCommit(false);
BLOB blob = null;
PreparedStatement pstmt = conn.prepareStatement("insert into javatest values(?,empty_blob())");
pstmt.setString(1,"test.jpg");
pstmt.executeUpdate();
pstmt.close();
pstmt = conn.prepareStatement("select content from javatest where name= ? for update");
pstmt.setString(1,"test.jpg");
ResultSet rset = pstmt.executeQuery();
if (rset.next()) blob = (BLOB) rset.getBlob(1);
String fileName = "d:/2475.jpg";
File f = new File(fileName);
FileInputStream fin = new FileInputStream(f);
System.out.println("file size = " + fin.available());
pstmt = conn.prepareStatement("update javatest set content=? where name=?");
OutputStream out = blob.getBinaryOutputStream();
int count = -1, total = 0;
// byte[] data = new byte[(int)fin.available()];
// fin.read(data);
// out.write(data);
byte[] data = new byte[blob.getBufferSize()]; // 另一种实现方法,节省内存
while ((count = fin.read(data)) != -1) {
total += count;
out.write(data, 0, count);
}
fin.close();
out.close();
pstmt.setBlob(1,blob);
pstmt.setString(2,"fankai");
pstmt.executeUpdate();
pstmt.close();
conn.commit();
conn.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
以上例子可以用来写入文件到BLOGpackage test;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;public class GetBlog {
public static void main(String args[]) throws FileNotFoundException{
java.sql.PreparedStatement ps = null;
java.sql.ResultSet rs = null;
Connection conn=null;
java.io.InputStream in = null;
// java.io.OutputStream out = null;
oracle.sql.BLOB blob = null;
FileOutputStream out = null;
byte[] data;
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1251:test","test","test");
String sqlstring = "select * from javatest where name = ?";
ps = conn.prepareStatement(sqlstring);
ps.setString(1,"test.jpg");
rs = ps.executeQuery();
if(rs.next()){
blob = ((oracle.jdbc.OracleResultSet)rs).getBLOB("content");
// in = blob.getBinaryStream();
in=rs.getBinaryStream("content");
out=new FileOutputStream("C:/aaa.jpg");
int bufferSize = blob.getBufferSize();
data = new byte[1023];
int bytesRead = 0;
while ((bytesRead = in.read(data)) != -1) {
System.out.print(data.length);
out.write(data, 0, bytesRead);
}
ps.clearParameters();
data = null;
in.close();
out.close();
}
}catch(Exception e){
}finally{
try{
in = null;
out = null;
blob = null;
rs = null;
ps =null;
conn = null;
}catch(Exception e){
System.err.println(e);
}
}
}
}这个可以用来获取BLOG数据并生成图片文件.
做些修改就可以写个SEVLET啥的了.