gpt4 book ai didi

html - 我想使用 servlet 从 mysql 数据库检索图像,并在表中显示该图像以及该用户详细信息

转载 作者:行者123 更新时间:2023-11-29 22:17:14 25 4
gpt4 key购买 nike

一切都运转良好。当我删除用于检索图像的代码时,我会在表中获取用户详细信息,当我将检索图像的代码与打印用户详细信息的代码一起放置时,我只会将图像作为输出。我想打印它们并在表格内显示其他详细信息。

代码如下:

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
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;

/**
* Servlet implementation class Welcome
*/
public class Welcome extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Welcome() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream out =response.getOutputStream();
response.setContentType("text/html");
String uniroll =(String) request.getAttribute("UROLL");


out.println("<html>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"Bootstrap/bootstrap.min.css\">");
out.println(" <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">");
out.println("<body>");
out.println("<div class='container'>");
out.println("<h3>Welcome</h3> ");
try{

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college_record","root","20wasadk");
String query="SELECT * FROM students_detail WHERE University_Roll=?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, uniroll);
ResultSet rs =ps.executeQuery();

while(rs.next()){
String imgLen="";
imgLen=rs.getString(11);
int len=imgLen.length();

byte[] rb =new byte[len];
InputStream readImg =rs.getBinaryStream(11);
int index=readImg.read(rb, 0, len);


out.println("<section class='col-md-6' id=\"signupbox\" >");
out.println("<table class='table table-hover'>");
out.println("<tr><th>First Name</th><td>"+ rs.getString(1)+"</td></tr>");
out.println("<tr><th>Last Name</th><td>"+ rs.getString(2)+"</td></tr>");
out.println("<tr><th>Sex</th><td>"+ rs.getString(3)+"</td></tr>");
out.println("<tr><th>Father's Name</th><td>"+ rs.getString(4)+"</td></tr>");
out.println("<tr><th>Class Roll</th><td>"+ rs.getString(5)+"</td></tr>");
out.println("<tr><th>University Roll</th><td>"+ rs.getString(6)+"</td></tr>");
out.println("<tr><th>Branch</th><td>"+ rs.getString(7)+"</td></tr>");
out.println("<tr><th>Contact No</th><td>"+ rs.getString(8)+"</td></tr>");
out.println("<tr><th>Permanent Address</th><td>"+ rs.getString(9)+"</td></tr>");
out.println("<tr><th>Password</th><td>"+ rs.getString(10)+"</td></tr>");
out.println("<tr><th>Index</th><td>"+index +"</td></tr>");

ps.close();
response.reset();
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();

}



out.println("<table>");
}

catch(ClassNotFoundException ce){
ce.printStackTrace();
}
catch(SQLException se){
se.printStackTrace();
}
out.println("<section >");
out.println("<div>");
out.println("</body>");
out.println("</html>");

}

}

最佳答案

您可以对图像数据进行 Base64 编码并使用 data URIsrc img 的属性标签,例如<img src="data:image/png;base64,iVBORw0KGgoAAAANS... (the Base64 encoded image data) ...8bgAAAAASUVORK5CYII=”> .

一些注释:

  • 老IE版本不支持(IE8以下,IE8中数据大小有限制);
  • 我认为以这种方式嵌入动态图像不是一个好主意,正确的方法是有一个单独的 servlet 来提供图像,并在 src 中使用普通的 URI。属性。请参阅上述链接页面中的重要说明部分。

关于html - 我想使用 servlet 从 mysql 数据库检索图像,并在表中显示该图像以及该用户详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31110939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com