【问题标题】:Why i cant display images BLOB type of MySQL in a JSP file?为什么我不能在 JSP 文件中显示图像 BLOB 类型的 MySQL?
【发布时间】:2017-04-23 06:58:24
【问题描述】:

我有这段代码来显示图像,但它不起作用。不显示图像只是一个白色的小方块,我一直在寻找答案,但我找不到,请有人帮助我。

显示.jsp

<%@ page import="java.sql.*" %> 
<%@ page import='java.io.InputStream' %> 
<%@ page import='java.io.OutputStream' %> 
<% 
String login = "root"; 
String password = ""; 
String url = "jdbc:mysql://localhost/dbimagenes"; 
Connection conn = null; 
Statement statement = null; 
ResultSet rs = null; 
int nBytes=0; 
%> 
<html><style type="text/css"> 
<!-- 
body { 
background-color: #F5f5f5; 
} 
--> 
</style><body> 
    <h1>Imagen desde MySQL</h1>
<table>
    <tr><td>
<% 
Class.forName("com.mysql.jdbc.Driver").newInstance (); 
conn = DriverManager.getConnection(url, login, password); 
statement = conn.createStatement(); 
rs = statement.executeQuery("SELECT imagen FROM t_imagenes where id='2'"); 
try{ 
if(rs.next()){ 
response.setContentType("image/jpeg"); 
InputStream is = rs.getBinaryStream(1); 
OutputStream aux= response.getOutputStream(); 
out.println("jajaja"); 

byte [] buffer = new byte[4096]; 
for (;;) { 
nBytes = is.read(buffer); 
if (nBytes == -1) 
break; 

aux.write(buffer, 0, nBytes); 

} 

is.close(); 
aux.flush(); 
aux.close(); 


}else{ 

throw new SQLException("image not found"); 
} 
rs.close(); 
} catch (SQLException e) { out.println("Imagen no encontrada");} 

out.println("no se muestra"); 
%> 
        </td></tr></table>

<p> Imagen</p> 
<a href="index.html">PRINCIPAL</a>
</body></html>

我将图片保存到mysql的方式是这样的:

插入.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="conexion.jsp" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Insertar</h1>
        <%
        String nombre=request.getParameter("txtnombre");
        String marca=request.getParameter("txtmarca");
        String imagen=request.getParameter("txtimagen");

        if(nombre!=null && marca!=null && imagen!=null){
            String qry ="insert into t_imagenes(nombre,marca,imagen) values('"+nombre+"','"+marca+"','"+imagen+"')";
            sql.executeUpdate(qry);
            out.print("Datos Registrados "
                    + "<a href='index.jsp'>REGRESAR</a>");

        }else{

        %>
        <form name="frmimagenes" method="post" action="insertar.jsp">
           nombre: <input type="text" name="txtnombre"/><br/>
           marca: <input type="text" name="txtmarca"/><br/>
           imagen: <input type="file" name="txtimagen" value="" size="50" /><br/>
           <input type="submit" value="Guardar">
        </form>

        <%}//else%>
    </body>
</html>

【问题讨论】:

    标签: java mysql image jsp blob


    【解决方案1】:

    您需要使用图片标签img以html显示图片(以下代码我没有测试,如有错误请调整)

    <%@ page import="java.sql.*" %> 
    <%@ page import='java.io.InputStream' %> 
    <%@ page import='java.io.OutputStream' %> 
    <%@ page import='java.io.ByteArrayOutputStream' %> 
    <%@ page import='org.apache.commons.codec.binary.Base64' %> 
    
    <% 
    String login = "root"; 
    String password = ""; 
    String url = "jdbc:mysql://localhost/dbimagenes"; 
    Connection conn = null; 
    Statement statement = null; 
    ResultSet rs = null; 
    int nBytes=0; 
    %> 
    <html><style type="text/css"> 
    <!-- 
    body { 
    background-color: #F5f5f5; 
    } 
    --> 
    </style><body> 
        <h1>Imagen desde MySQL</h1>
    <table>
        <tr><td>
    <% 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    conn = DriverManager.getConnection(url, login, password); 
    statement = conn.createStatement(); 
    rs = statement.executeQuery("SELECT imagen FROM t_imagenes where id='2'"); 
    
    String url = "data:image/jpeg;base64,";
    
    try{ 
    if(rs.next()){ 
    InputStream is = rs.getBinaryStream(1); 
    OutputStream aux = new ByteArrayOutputStream();
    out.println("jajaja"); 
    
    byte [] buffer = new byte[4096]; 
    for (;;) { 
    nBytes = is.read(buffer); 
    if (nBytes == -1) 
    break; 
    
    aux.write(buffer, 0, nBytes); 
    
    } 
    
    is.close(); 
    aux.flush(); 
    
    url += new String(Base64.encodeBase64(aux.toByteArray()));
    
    aux.close(); 
    
    }else{ 
    
    throw new SQLException("image not found"); 
    } 
    rs.close(); 
    } catch (SQLException e) { out.println("Imagen no encontrada");} 
    
    out.println("no se muestra"); 
    %> 
    <img src="<%=url%>" />
            </td></tr></table>
    
    <p> Imagen</p> 
    <a href="index.html">PRINCIPAL</a>
    </body></html>
    

    【讨论】:

      猜你喜欢
      • 2010-10-15
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多