简体   繁体   English

我想从数据库中检索图像并在JSP中使用显示它 <img> 标签。 我正在使用我的SQL

[英]I want to retrieve image from database and display it in JSP using <img> tag. I am using my sql

I want to retrieve image from database and display it in JSP using img tag. 我想从数据库中检索图像,并使用img标签在JSP中显示它。 I am using mySql. 我正在使用mySql。

public class DisplayImage extends  HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //PrintWriter pw = response.getWriter();
        String connectionURL = "jdbc:mysql://localhost:3306/demodb";;
        java.sql.Connection con=null;
        try{  
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection(connectionURL,"root","0321");  
            Statement st1=con.createStatement();
            ResultSet rs1 = st1.executeQuery("select photo from contacts where  contact_id='2'");
            String imgLen="";
            if(rs1.next()){
                imgLen = rs1.getString(1);
                System.out.println(imgLen.length());
            }  
            rs1 = st1.executeQuery("select photo from contacts where contact_id='2'");
            if(rs1.next()){
                int len = imgLen.length();
                byte [] rb = new byte[len];
                InputStream readImg = rs1.getBinaryStream(1);
                int index=readImg.read(rb, 0, len);  
                System.out.println("index"+index);
                st1.close();
                response.reset();
                response.setContentType("image/jpg");
                response.getOutputStream().write(rb,0,len);
                response.getOutputStream().flush();  
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}    

This Code display image directly. 此代码直接显示图像。 But I want the path. 但是我要走这条路。 So I can display it wherever I want. 所以我可以在任何地方显示它。 Are there any other ways to display this image in a dynamic web page? 还有其他方法可以在动态网页中显示此图像吗?

The path depends on how your servlet is mapped in web.xml (as I don't see any annotations in your code). 路径取决于您的servlet在web.xml中的映射方式(因为我在代码中看不到任何注释)。 The other - but rather discouraged - way is to use data URI. 另一种方法(但不建议使用)是使用数据URI。 See I want to retrieve image from mysql database using servlet and show that image along with that user details in table 请参阅我要使用servlet从mysql数据库中检索图像,并在表中显示该图像以及该用户的详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想将数据库中的多个图像显示到jsp中(我正在映射servlet),所以在jsp中,它将显示在img标签的src中 - I want to display multiple images from database into jsp (i am mapping servlet) so in jsp m gonna display in src of img tag 我想从数据库中检索图像并将其显示在jsp页面中 - I want to retrieve image from the database and display it in a jsp page 当我尝试使用锚标记从我的jsp页面调用servlet页面时,显示错误 - when i am trying to call a servlet page from my jsp page using anchor tag it showing an error 我正在尝试使用Java jsp将数据插入本地计算机的SQL数据库中 - I am trying to insert data in to a SQL database in my local computer using Java jsp 我想从数据库中检索值而不使用listview并在textview领域中显示 - I want to retrieve values from database without using listview and display in textview feilds 如何使用显示标记将要删除/编辑的行中的值从struts2中的jsp传递到动作类? - How to pass the values in the row that i want to delete/edit to the action class from jsp in struts2 using display tag? 我想在jsp中每页显示数据库10的结果 - I want to display results from database 10 per page in jsp 如何使用JSON从远程数据库检索数据并显示在android应用程序xml文件中? - How do i retrieve the data using JSON from the Remote Database and display in my android app xml file? 为什么我无法使用Spring MVC和Hibernate在JSP页面中显示数据库值 - Why I am unable to display database values in jsp page , using spring mvc and hibernate 我正在尝试使用回收站视图从 Firebase 数据库中检索数据,但出现错误 - i am trying to retrieve the data from fire base database using recycler view but i am getting errors in it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM