我的问题是,在我能够在我的jsp中在mysql数据库中显示我的中等blob之后,我无法使用我的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>
谢谢大家。
答案 0 :(得分:0)
对于迟到的回答感到抱歉,但该网站不允许我在8小时内回复。对所有人都不满意。我找到了一个临时解决方案,因为这是出于学术目的,只是没问题。
解决方案是。当我想在ing标签中显示图像时,我总是必须给标签提供网址 有两种可能性:
用于显示图像的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>
当然这只是一个例子。 感谢所有人,抱歉我的英语不好。
答案 1 :(得分:-1)
将图像宽度设置为100%,以便它适合固定长度的div