【问题标题】:Blob image from Database and show in Jsp来自数据库的 Blob 图像并在 Jsp 中显示
【发布时间】:2016-06-08 16:25:50
【问题描述】:
                        <%-- 
                            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>

但问题是,当我访问我的个人资料时,它只显示数据库中的图像。只有检索到的图像显示在页面中,但其他代码不起作用,其他属性未显示在页面中.. ..

【问题讨论】:

  • 你能clarify你对问题的解释以及预期与实际结果吗?我暂时不明白。
  • 当我加载这个页面时,这个页面只包含来自数据库的图像。但是没有添加更多的东西,比如段落,另一张图片,但这个页面没有显示。

标签: java image jsp


【解决方案1】:

您的代码无法运行,因为您将 html 页面和图像中的二进制数据混合在一起。

您只需发送图像的二进制数据,而不是图像的链接。

要解决此问题,您需要创建另一个 servlet,它将发送图像。

您也可以创建一个 base64 字符串并使用数据源:How to display Base64 images in HTML?

此代码的另一个问题:您可以使用电子邮件查询参数注入 SQL。我假设您将编辑代码以使用查询参数 email 作为 SQL 参数。切勿使用带有不安全参数的字符串连接。更好地使用 PreparedStatement

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

更新 我刚刚看到,您使用的是会话而不是查询参数。但问题是一样的。

【讨论】:

    【解决方案2】:

    线

    o.write(imgdata);
    

    显示一个这样的数组 [b185....

    您应该将其转换为字符串。

    创建一个 Java 类(例如 BlobToString )并为其添加一个方法(convert)以将 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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      相关资源
      最近更新 更多