- 论坛徽章:
- 0
|
本人原来存储图片的时候都是存储图片的路径,最近想改变下,直接把图片以二进制的形式进行存储,参考了不少资料过后,终于将图象能存储到oracle中,只是在读取的时候碰到些问题,说明如下:我的环境是winxp+jdk1.4.2
1,我首先写了个用来读取并显示图片的servlet,代码如下:
package com.zhy.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import com.zhy.message.Utils.DBConnection;
public class getphoto extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg";
private Connection conn;
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
byte [] buf=null;
try{
String searchSql="select photo from oa.test";
Statement stmt = conn.createStatement();
ResultSet RS = stmt.executeQuery(searchSql);
oracle.sql.BLOB blob = null;
while (RS.next()) {
blob = (oracle.sql.BLOB) RS.getBlob(1);
System.out.println(blob.length());
}
InputStream is = blob.getBinaryStream();
buf=new byte[is.available()];
response.setContentType("image/jpeg" ;
ServletOutputStream out = response.getOutputStream();
out.write(buf);
out.flush();
}catch(Exception e){
e.printStackTrace();
}
}
public void destroy() {
}
}
2,我在web-xml设置了这个servlet,如下:
<servlet>;
<servlet-name>;getphoto</servlet-name>;
<display-name>;getphoto</display-name>;
<description>;getphoto</description>;
<servlet-class>;com.zhy.servlet.getphoto</servlet-class>;
</servlet>;
<servlet-mapping>;
<servlet-name>;getphoto</servlet-name>;
<url-pattern>;/getphoto</url-pattern>;
</servlet-mapping>;
3,我的jsp页面如下:
<%@ page contentType="text/html; charset=gb2312" %>;
<html>;
<head>;
<title>;</title>;
</head>;
<body>;
<h1 align="center">;<font color="#FF0000">;数据库图片展示</font>;</h1>;
&<br>;
<hr>;
<img src="/getphoto">;<br>;
<hr>;
</body>;
</html>;
经过以上的步骤,我在访问把个页面的时候,图片的位置却不显示图片,我查看图片的属性,发现类型为不可用,我不知道问题出在哪了,希望高人能能指点指点! |
|