簡體   English   中英

來自數據庫的Blob圖像,並在Jsp中顯示

[英]Blob image from Database and show in Jsp

                        <%-- 
                            Document   : Profile
                            Created on : Jun 5, 2016, 1:28:02 AM
                            Author     : User
                        --%>

                        <%@page import="ServletHolder.Database"%>
                        <%@page import="java.sql.*"%>
                        <%@page import="java.io.*"%>

                        <%
                                      response.setHeader("Cache-Control","no-cache");
                                      response.setHeader("Cache-Control","no-store");
                                      response.setDateHeader("Expires", 0);
                                      response.setHeader("Pragma","no-cache");


                                     HttpSession s=request.getSession();



                            try{
                            String a=(String)s.getAttribute("email");
                            String b=(String)s.getAttribute("password");

                            if((a.equals("a") && b.equals("b"))){

                            }else{
                                response.sendRedirect("index.jsp");
                            }
                            }catch(Exception e){
                                response.sendRedirect("index.jsp");
                            }

                            Connection con=Database.getConnection();
                            PreparedStatement pst;
                            ResultSet rs;
                            String c="shakil123@gmail.com";
                            String ah="";
                            Blob img;
                            byte[] imgdata=null;
                            try{

                            String al="select * from `uploadpic` where `email`='"+c+"'";
                            pst=con.prepareStatement(al);
                            rs=pst.executeQuery();

                            if(rs.next()){
                                ah=rs.getString("email");
                                img=rs.getBlob("pic");
                                imgdata=img.getBytes(1, (int)img.length());
                            }

                            }catch(Exception e){
                                ah=e.toString();
                            }

                               response.setContentType("image/gif");
                               OutputStream o = response.getOutputStream();
                             //  o.write(imgdata);
                              // o.flush(); 
                              // o.close();


                        %>

                        <%@page contentType="text/html" pageEncoding="UTF-8"%>
                        <!DOCTYPE html>
                        <html>
                            <head>
                                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                                <title>JSP Page</title>

                            </head>
                            <body><!-- onload="noBack()" onpageshow="noBack()">-->
                                <h1>Profile</h1>

                                <div span="12">

                                    <div sapan="9">

                                        <p><strong> Hello User Welcome!!!</strong></p>
                                        <img src="scrum-chart.jpg" alt="Mountain View" style="width:200px;"><br>
                                        <p><%=ah%><p>

                                            <img src="<%

                                            o.write(imgdata);
                                            o.flush();
                                            o.close();

                                                       %>" alt="Profile Picture" width="200" title="Profile Picture">

                                    </div>
                                    <div sapan="3">

                                        <p><strong> Advertisement</strong></p> 

                                    </div>

                                    </div>

                                <a href="Editprofile.jsp"> Edit Profile</a><br>


                                <form action="Logout" method="post">

                                    <input type="submit" value="Logout">

                                    </form>
                            </body>
                        </html>

但是問題是當我訪問我的個人資料時,它僅顯示數據庫中的圖像。即僅檢索到的圖像顯示在頁面中,但其他代碼不起作用,其他屬性也不顯示在頁面中....

您的代碼無法正常工作,因為您正在混合html頁和圖片中的二進制數據。

您只需發送圖像的二進制數據,而不是圖像的鏈接。

要解決此問題,您將需要創建另一個servlet,該servlet將發送圖像。

您還可以創建base64字符串並使用數據源: 如何以HTML顯示Base64圖像?

此代碼的另一個問題:您可以使用電子郵件查詢參數注入SQL。 我假設您將編輯代碼以將查詢參數email用作SQL參數。 切勿使用帶有不安全參數的字符串連接。 更好地使用PreparedStatement

String al="select * from `uploadpic` where `email`='"+c+"'";

我剛剛看到的更新 ,您使用了會話而不是查詢參數。 但是問題是一樣的。

o.write(imgdata);

顯示這樣的數組[b185 ....

您應該將其轉換為字符串。

創建一個Java類(例如BlobToString),並為其添加一個方法(轉換)以將Blob轉換為String。 你可以用這個

public class BlobToString {
    public String convert(byte[] blob) {
        String str = "";
        //read bytes from ByteArrayInputStream using read method
        if (blob != null && blob.length > 0) {
            for (byte b : blob) {
                str = str + (char) b;
            }
        }
        return str;
     }
}

將Java類BlobToString導入您的JSP

 <%@ page import="my.package.BlobToString" %>

創建一個新實例

 BlobToString blobToString = new BlobToString()

並更換

o.write(imgdata);

通過這條線

o.write(blobToString.convert(imgdata));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM