Моя проблема в том, что после того, как я смог отобразить свой средний blob в базе данных mysql в моем jsp, я не могу поместить изображение с помощью css.
Я хочу разместить его внутри div, который имеет фиксированную ширину и высоту.
<%@page import="java.io.OutputStream"%>
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Statement stmnt = null;
ResultSet rs = null;
OutputStream o = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/uShare";
String username = "root";
String password = "";
PreparedStatement ps;
Blob image = null;
byte[] imgData = null;
try {
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection(url, username, password);
stmnt = con.createStatement();
rs = stmnt.executeQuery("select foto from utilizadores where idUtilizador = 13");
if (rs.next()) {
image = rs.getBlob("foto");
imgData = image.getBytes(1, (int) image.length());
out.println("encontrou imagem");
}
//mostra
response.setContentType("image/gif");
o = response.getOutputStream();
o.write(imgData);
%>
<div style="float: right; width: 20%; height: 20%;">
<%
o.flush();
%>
</div>
<%
o.close();
stmnt.close();
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
%>
</body>
</html>
Спасибо вам за все.
Извините за поздний ответ, но сайт не позволяет мне ответить в течение 8 часов. Спасибо всем. Я нашел временное решение, и потому, что это для академических целей, все в порядке.
Решение есть. Когда я хочу отображать изображение в теге ing, мне всегда нужно указать тег url.
Есть две возможности:
Jsp для отображения изображения:
<%@page import="java.io.OutputStream"%>
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<link href="css/master.css" rel="stylesheet" type="text/css">
</head>
<body>
<%
Statement stmnt = null;
ResultSet rs = null;
OutputStream o = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/uShare";
String username = "root";
String password = "";
PreparedStatement ps;
Blob image = null;
byte[] imgData = null;
try {
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection(url, username, password);
stmnt = con.createStatement();
rs = stmnt.executeQuery("select foto from utilizadores where idUtilizador = 13");
if (rs.next()) {
image = rs.getBlob("foto");
imgData = image.getBytes(1, (int) image.length());
out.println("encontrou imagem");
}
//mostra
response.setContentType("image/jpeg");
o = response.getOutputStream();
o.write(imgData);
o.flush();
response.flushBuffer();
stmnt.close();
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
%>
</body>
</html>
И тогда конечный пользователь jsp
<%@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>
<img src="testeImagem.jsp" alt="foto" style="width: 200px; height: 200px">
</body>
</html>
Конечно, это всего лишь пример. Спасибо всем, и извините за мой плохой английский.
установите ширину изображения как 100%, чтобы она соответствовала фиксированной длине div