2015-06-29 52 views
0

中顯示該圖像以及該用戶的詳細信息一切正常。當我刪除檢索圖像的代碼時,我在表格中獲取用戶詳細信息,當我將代碼與打印用戶詳細信息代碼一起檢索圖像時,我只將圖像作爲輸出。 我想打印它們和表格內的其他細節出現。我想使用servlet從mysql數據庫檢索圖像,並在表

代碼如下所示:

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>"); 

    } 

} 

回答

0

你可以的Base64編碼圖像數據,並作爲img標籤,例如的src屬性使用data URI<img src="data:image/png;base64,iVBORw0KGgoAAAANS... (the Base64 encoded image data) ...8bgAAAAASUVORK5CYII=」>

幾點注意事項:

  • 它不是在老IE版本支持(比IE8年齡大了,數據的大小在IE8被限制);
  • 我不認爲以這種方式嵌入動態圖像是一個好主意,正確的方法是將一個單獨的servlet僅用於提供圖像,並在src屬性中使用正常的URI。請參閱上述鏈接頁面中的重要注意事項部分。
相關問題