从数据库中查询图片显示在jsp页面上


package demo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import demo.sql.DaoTest;

public class LoadPicture extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
DaoTest dt=new DaoTest();
Connection conn = dt.getConn();

String sql = "select t.content from filetest t where t.name='最终图片测试'";

PreparedStatement ps = null;

ResultSet rs = null;

InputStream is = null;

ServletOutputStream os = null;
OutputStream out=null;

try {

ps = conn.prepareStatement(sql);
rs = ps.executeQuery();

if (rs.next()) {

is = rs.getBinaryStream(1);

}
File file=new File("c://abc.jpg");
out=new FileOutputStream(file);
resp.setContentType("image/jpeg");
os = resp.getOutputStream();

int num;

byte buf[] = new byte[1024];

while ((num = is.read(buf)) != -1) {

os.write(buf, 0, num);

}


} catch (SQLException e) {

e.printStackTrace();

}

try {

is.close();

os.close();

rs.close();

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}
}

}



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<img name="pic" src="<%=basePath+"loadPicture.action"%>"/>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在数据库存储图片的路径,然后在JSP页面上使用SQL查询语句将路径查询出来,最后使用<img>标签将图片显示出来。 以下是一个简单的JSP页面示例代码,用于遍历显示数据库查询出来的图片: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>显示图片</title> </head> <body> <h1>显示图片</h1> <table> <c:forEach var="item" items="${imageList}"> <tr> <td>${item.imageName}</td> <td><img src="${item.imagePath}" alt="${item.imageName}"></td> </tr> </c:forEach> </table> <% Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "root"; conn = DriverManager.getConnection(url, user, password); String sql = "SELECT * FROM image_table"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); List<ImageBean> imageList = new ArrayList<ImageBean>(); while (rs.next()) { ImageBean image = new ImageBean(); image.setImageName(rs.getString("image_name")); image.setImagePath(rs.getString("image_path")); imageList.add(image); } request.setAttribute("imageList", imageList); } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } %> </body> </html> ``` 在上面的示例代码,我们使用了JSTL的<c:forEach>标签遍历了查询出来的图片列表,然后使用<img>标签将图片显示出来。 同时,我们还在JSP页面使用了Java代码,通过JDBC连接到数据库查询图片路径并将结果存储在一个List,最后将List设置为JSP页面的属性,以便在<c:forEach>标签进行遍历和显示。请注意,为了简化示例代码,我们没有对JDBC连接进行异常处理和资源释放,实际开发需要注意这些问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值