简体   繁体   English

使用Servlet显示Blob字段中的图像

[英]display an image from a blob field using servlet

i created a jsp page that calls another jsp page that show an image took from a blob field in my mysql db: 我创建了一个jsp页面,该页面调用另一个jsp页面,该页面显示了从我的mysql数据库中的blob字段获取的图像:

<img src="blob.jsp"> 

it works. 有用。 But, somewhere in this forum i read that this is not the right way to do it. 但是,在这个论坛的某个地方,我读到这不是正确的方法。 I should, instead, using a servlet this way: 相反,我应该这样使用servlet:

 <img src="servlet_name">

I created a servlet, but it doesent show me the image it shows me this 我创建了一个servlet,但是它确实向我展示了它向我展示的图像

ÿØÿàJFIFHHÿí$Photoshop 3.08BIMí ResolutionHH8BIM FX Global Lighting Anglex8BIMFX Global Altitude8BIMó Print Flags 8BIM Copyright Flag8BIM'Japanese Print Flags 8BIMõColor Halftone SettingsH/fflff/ff¡™š2Z5-8BIMøColor Transfer Settingspÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿèÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿèÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿèÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿè8BIM Layer State8BIM Layer Groups8BIMGuides@@8BIM URL overrides8BIMSlicesuƒD Untitled-1Dƒ8BIMICC Untagged Flag

This is my simple servlet 这是我简单的servlet

package Jeans;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/BlobDisplay")
public class BlobDisplay extends HttpServlet {
    private static final long serialVersionUID = 1L;
    GestioneDB gestioneDB ;


    public BlobDisplay() {
        super();       
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
            {       
        Blob image = null;
        byte[ ] imgData = null ;
        String query = null;
        query = request.getParameter(query);        

        gestioneDB = new GestioneDB();

        ResultSet rs =  gestioneDB.rs("select immagine_principale from news where id ='217'");

            try{
            if (rs.next()) {
            image = rs.getBlob("immagine_principale");
            imgData = image.getBytes(1,(int)image.length());

            response.setContentType("image/jpg");
            OutputStream o = response.getOutputStream();
            o.write(imgData);
            o.flush();
            o.close();
            }
        }
        catch(Exception e){}    
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  

         this.doGet(request,response);
    }

}

Here is a debugging hint for now. 这是目前的调试提示。

JSPs are just "inside out" servlets and they are translated to servlets by the container. JSP只是“由内而外”的servlet,并且它们被容器转换为servlet。 Since your JSP works and your servlet doesn't, why don't you check out (and post) the generated servlet. 由于您的JSP有效,而servlet无效,因此为什么不签出(并发布)生成的servlet。 If you are using tomcat, it would be in a file called blob__jsp.java deep in the work directory . 如果使用的是tomcat,它将位于工作目录深处的名为blob__jsp.java文件中 Then compare the calls and set up of your servlet and the generated servlet. 然后比较调用,并设置您的servlet和生成的servlet。

(My first guess is the content type, but you seem to be setting that) (我的第一个猜测是内容类型,但您似乎正在设置它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM