读取保存在数据库里的图片JSP页面显示无法显示图片
图片链接:::imgsrc="ShowImage?bookname=<%=su.getRequest().getParameter("bookname")%>&autho...
图片链接:::img src="ShowImage?bookname=<%=su.getRequest().getParameter("bookname") %>
&author=<%=su.getRequest().getParameter("author") %>
&publisher=<%=su.getRequest().getParameter("publisher")%>
这个是链接数据库读取图片的类
public class ShowImage extends HttpServlet {
public void service(ServletRequest request,ServletResponse response)
throws ServletException,IOException {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=
DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseNmae=master","sa","123456");
Statement stmt = conn.createStatement();
String sql="select bookImage from book where bookName='"+codeToString(request.getParameter("bookname"))+
"' and author='"+codeToString(request.getParameter("author"))+
"'and publisher='"+
codeToString(request.getParameter("publisher"))+"' ";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
int len = 10*1024*1024;
InputStream in = rs.getBinaryStream("bookImage");
OutputStream toClient = response.getOutputStream();
response.reset();
response.setContentType("image/jpg");//或gif
byte[] P_Buf = new byte[len];
int i;
while((i = in.read(P_Buf)) != -1){
toClient.write(P_Buf, 0, i);
}
in.close();
toClient.flush();
toClient.close();
}
conn.close();
}catch(Exception e){
System.out.println("出现异常: "+e.getMessage());
}
}
//public String codeToString(String str){
//return (new String(s.getBytes("UTF-8")));
//}
private String codeToString(String parameter) {
// TODO Auto-generated method stub
return null;
}
} 展开
&author=<%=su.getRequest().getParameter("author") %>
&publisher=<%=su.getRequest().getParameter("publisher")%>
这个是链接数据库读取图片的类
public class ShowImage extends HttpServlet {
public void service(ServletRequest request,ServletResponse response)
throws ServletException,IOException {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=
DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseNmae=master","sa","123456");
Statement stmt = conn.createStatement();
String sql="select bookImage from book where bookName='"+codeToString(request.getParameter("bookname"))+
"' and author='"+codeToString(request.getParameter("author"))+
"'and publisher='"+
codeToString(request.getParameter("publisher"))+"' ";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
int len = 10*1024*1024;
InputStream in = rs.getBinaryStream("bookImage");
OutputStream toClient = response.getOutputStream();
response.reset();
response.setContentType("image/jpg");//或gif
byte[] P_Buf = new byte[len];
int i;
while((i = in.read(P_Buf)) != -1){
toClient.write(P_Buf, 0, i);
}
in.close();
toClient.flush();
toClient.close();
}
conn.close();
}catch(Exception e){
System.out.println("出现异常: "+e.getMessage());
}
}
//public String codeToString(String str){
//return (new String(s.getBytes("UTF-8")));
//}
private String codeToString(String parameter) {
// TODO Auto-generated method stub
return null;
}
} 展开
2个回答
展开全部
我把你的代码稍微改造了下,我这边是可以显示图片的。代码如下:
数据库操作部分:
package com.database;
import java.io.InputStream;
import java.sql.*;
/**
* @作者 王建明
* @创建日期 13-10-7
* @创建时间 下午12:32
* @版本号 V 1.0
*/
public class DataBaseUtil {
public static InputStream getImageStreamFromDataBase() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn =
DriverManager.getConnection("jdbc:mysql://localhost/quickstart", "root", "123456");
Statement stmt = conn.createStatement();
String sql = "select book_image from tbl_book where id=1 ";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
return rs.getBinaryStream("book_image");
}
} catch (Exception e) {
System.out.println("出现异常: " + e.getMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
servlet部分:
package com.servlet;
import com.database.DataBaseUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @作者 王建明
* @创建日期 13-10-7
* @创建时间 下午12:18
* @版本号 V 1.0
*/
public class ShowImage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream in = DataBaseUtil.getImageStreamFromDataBase();
OutputStream toClient = response.getOutputStream();
response.reset();
response.setContentType("image/jpg");//或gif
int len = 10*1024*1024;
byte[] P_Buf = new byte[len];
int i;
while((i = in.read(P_Buf)) != -1){
toClient.write(P_Buf, 0, i);
}
in.close();
toClient.flush();
toClient.close();
}
}
web.xml中的servlet配置:
<servlet>
<servlet-name>ShowImage</servlet-name>
<servlet-class>com.servlet.ShowImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowImage</servlet-name>
<url-pattern>/showImage</url-pattern>
</servlet-mapping>
页面中加载图片方式:
<img src="showImage" />
希望对你有帮助O(∩_∩)O~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询