[英]How to display an image from a database in a JSP using a java servlet?
這是我當前的servlet代碼。 這是我將從數據庫中獲取圖像的地方。
newServlet.java
package LAWS_SERVLETS;
import LAWS_DAOS.LReceiptsDAO;
import LAWS_ENTITIES.Lawyer_Receipt;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Meryl
*/
@WebServlet(name = "newServlet", urlPatterns = {"/newServlet"})
public class newServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
LReceiptsDAO lrDAO = new LReceiptsDAO();
int imageId = Integer.parseInt(request.getParameter("id"));
// Check if ID is supplied to the request.
if (imageId == 0) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
byte[] image = lrDAO.getReceiptFile(imageId);
// Check if image is actually retrieved from database.
if (image == null) {
// Do your thing if the image does not exist in database.
// Throw an exception, or send 404, or show default/warning image, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Init servlet response.
response.reset();
response.setContentType(image.getContentType());
response.setContentLength(image.getContent().length);
// Write image content to response.
response.getOutputStream().write(image.getContent());
}
}
LReceiptsDAO
public byte[] getReceiptFile(int id){
byte[] l = null;
try {
DBConnectionFactory myFactory = DBConnectionFactory.getInstance();
Connection conn = myFactory.getConnection();
PreparedStatement pstmt = conn.prepareStatement("SELECT receipt_filepath FROM lawyer_receipts"
+ "where lawyerreceipt_id = ? ");
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
l = rs.getBytes("receipt_filepath");
}
conn.close();
return l;
} catch (SQLException ex) {
Logger.getLogger(LReceiptsDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
我從此鏈接獲得了servlet代碼,但是在設置init servlet響應部分時似乎出現了錯誤。
我很抱歉提出這個問題。 這是我第一次嘗試從數據庫中獲取圖像斑點,並通過DAO和servlet獲得它,到目前為止,我只看到在jsp中包含Java代碼的代碼。
感謝您的幫助。 謝謝!
嘗試更換
response.setContentType(image.getContentType());
response.setContentLength(image.getContent().length);
response.getOutputStream().write(image.getContent());
至
response.setContentType("image/*");
response.setContentLength(image.length);
response.getOutputStream().write(image);
找不到getContentType()
和getContent()
方法的原因是因為它們不存在。 可變image
的類型為byte[]
-原始字節數組-數組沒有這些方法。 該代碼實際上不應該編譯。
您獲得該代碼的鏈接的image
變量類型為com.example.model.Image
(在該示例中已定義並具有那些方法的類),而不是byte[]
。
順便說一句,您示例中的代碼選擇了一個名為receipt_filepath
的字段,該字段建議該字段將映像的路徑存儲在磁盤上,而不是Blob。 如果是這樣,您應該解析該路徑並從磁盤讀取文件(就像在鏈接到的示例中所做的那樣)。
如果是斑點,那么您實際上也應該將圖像的內容類型存儲在數據庫中。 在這種情況下,您可以執行以下操作:
PreparedStatement pstmt = conn.prepareStatement("SELECT image_blob_field_name, image_content_type_field_name FROM lawyer_receipts"
+ "where lawyerreceipt_id = ? ");
...
byte[] image = rs.getBytes(1);
String contentType = rs.getString(2);
...
然后再:
response.setContentType(contentType);
response.setContentLength(image.length);
response.getOutputStream().write(image);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.