[英]Why i cant display images BLOB type of MySQL in a JSP file?
I have this code to show the images but it not works. 我有这段代码来显示图像,但它不起作用。 doesnt display the image just a little white square, i've been looking for an answer and i cant find , somebody to help me, please.
没有显示图像,只是一个小小的白色正方形,我一直在寻找答案,我找不到,请有人帮助我。
Show.jsp Show.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>
My way to save the images into mysql is of this way: 我将图像保存到mysql的方式是这样的:
Insert.jsp Insert.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>
you need to use image tag img
to display the image in html (i have not test the following code, please adjust it if it has any error) 您需要使用图片标签
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.