java 如何在servlet中显示来自数据库的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4848819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to display image from database in servlet?
提问by user596427
This is the code have no error
这是代码没有错误
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DisplayBlobServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException {
String photoid = request.getParameter("txtid");
Blob photo = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String query = "select oimage from orderform where cuname = '" + photoid + "'";
ServletOutputStream out = response.getOutputStream();
try {
conn = getMySqlConnection();
} catch (Exception e) {
response.setContentType("text/html");
out.println("<html><head><title>Person Photo</title></head>");
out.println("<body><h1>Database Connection Problem.</h1></body></html>");
return;
}
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
if (rs.next()) {
photo = rs.getBlob(9);
} else {
response.setContentType("text/html");
out.println("<html><head><title>Person Photo</title></head>");
out.println("<body><h1>No photo found for id= 001 </h1></body></html>");
return;
}
response.setContentType("image/gif");
InputStream in = photo.getBinaryStream();
int length = (int) photo.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1) {
System.out.println("writing " + length + " bytes");
out.write(buffer, 0, length);
}
in.close();
out.flush();
} catch (SQLException e) {
response.setContentType("text/html");
out.println("<html><head><title>Error: Person Photo</title></head>");
out.println("<body><h1>Error=" + e.getMessage() + "</h1></body></html>");
return;
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*private static Connection getHSQLConnection() throws Exception {
Class.forName("jdbc.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}*/
public static Connection getMySqlConnection() throws Exception {
String driver = "com.jdbc.mysql.Driver";
String url = "jdbc:mysql://localhost/studio";
String username = "root";
String password = " ";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
/*public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
String username = "username";
String password = "password";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}*/
}
回答by f1sh
You need to tell the user's browser that the content is an image:
您需要告诉用户的浏览器内容是图像:
response.setContentType("image/jpg")
Otherwise the browser will attempt to display the bytestream as text. But since you don't specify which "Error" you are getting, I can't help any further at this point in time.
否则浏览器将尝试将字节流显示为文本。但是由于您没有指定您遇到的是哪个“错误”,因此此时我无能为力。
回答by user1192057
The error is in out.write(photo,0,photo.length);
错误在 out.write(photo,0,photo.length);
The method write(char[], int, int)
in the type PrintWriter
is not applicable for the arguments (Blob, int, long)
write(char[], int, int)
类型中的方法PrintWriter
不适用于参数(Blob, int, long)
回答by Gursel Koca
Well, you dont need to use Blob.inputStream, you could just write image binary data to outputstream as shown below ;
好吧,您不需要使用 Blob.inputStream,您可以将图像二进制数据写入 outputstream,如下所示;
out.write(photo,0,photo.length);
instead of ;
代替 ;
InputStream in = photo.getBinaryStream();
int length = (int) photo.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1) {
System.out.println("writing " + length + " bytes");
out.write(buffer, 0, length);
}
in.close();