各位大侠,我在oracle数据库的表字段blob中存入了一个图像,现在需要通过jsp和数据库的连接把它给调出来,可不知如何调用,据说要用到读取Stream来实现,哪位好心的大哥能告诉我具体的实现方法吗,谢谢!
---------------------------------------------------------------
我不在单位,所以没法帖程序。
大致思路是,用SERVLET
select到一个resultset中,然后用rs.getBinaryStream取得流,然后对这个Stream 作read,而且最好是循环读,直到return -1;表示读完了流,这样得到一个字节(byte)数组,再对respond设置contentType为image/*,然后把字节数组写到respond中,即可。
然后用JSP调此SERVLET,结束。
如有兴趣,星期一贴程序。
---------------------------------------------------------------
希望有点帮助
import javax.servlet.;
import javax.servlet.http.;
import java.io.;
import java.util.;
public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "image/jpeg";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
OutputStream out = response.getOutputStream();
FileInputStream fis = new FileInputStream("e:\\aaa.jpg");
int b = fis.read();
while(b!=-1){
out.write(b);
b = fis.read();
}
fis.close();
}
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
}
楼上说得差不离了,我记得程序好像就是
int b = fis.read();
while(b!=-1){
out.write(b);
b = fis.read();
}
fis.flush();//最好有这步
fis.close();
我来了,我省去了建立数据库连接的部分:
String sql = "select picture from tmp_pic where id=1";
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e);
}
try {
if (rs.next()) {
res.setContentType("image/");
ServletOutputStream sout = res.getOutputStream();
/
//互联网应用时,如果图片较大(>1M),且网速较低,可以考虑使用以下方式
//效果,使JPG从上到下分为数分,慢慢展示,类似ACDSEE
int BUFFER_SIZE=200000;
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[BUFFER_SIZE];
for (int i = in.read(b,0,BUFFER_SIZE); i!=-1;) {
System.out.println("Read "+i+" bytes.");
sout.write(b,0,i);
i=in.read(b,0,BUFFER_SIZE);
}
*/
/下面两行是我用的简单的调用方式,目前没有发现问题,而且效率高,如果不喜欢,可用上面注释的部分代替下面两行代码即可。/
byte [] image =rs.getBytes(1);
sout.write(image);
sout.flush();
sout.close();
}
} catch (Exception e) {
System.out.println(e);
PrintWriter toClient = res.getWriter(); //得到向客户端输出文本的对象
res.setContentType("text/html;charset=gb2312");
toClient.write("无法打开图片!");
toClient.close();
} finally {
stmt.close();
conn.close();
}