我有这个代码来显示图像,但它不起作用。没有显示图像只是一个小白色方块,我一直在寻找答案,我找不到,有人来帮助我,请。
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>
我将图像保存到mysql的方法就是这样:
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>
答案 0 :(得分:0)
您需要使用图片标记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>