如何以PDF格式显示图像,该图像是从MySql数据库中检索的:JSP和iText

时间:2015-05-31 22:50:30

标签: mysql jsp pdf jdbc itext

我在mysql中将图像存储在mediumblob中。 我想检索该图像并将其放在itext pdf中。

我有manange可以解决blob数据类型问题。它不适用于中等斑点。

检索完图像后,将其存储在blob数据类型中。之后我尝试将其转换为字节。

    HttpSession session=request.getSession();
    String name="";
    String birthdate="";
    String address="";
    String gender="";
    String doctor="";
    String disease="";
    String pregnant="";
    String nursing="";
    String pan="";
    String smoking="";
    String allergies="";
    String medicines="";
    String treatment="";
        String email=session.getAttribute("email").toString();
        session.setAttribute("email",email);
    Blob image0 = null;
        Blob image1 = null;
        Blob image2 = null;
        Blob image3 = null;
        Blob image4 = null; 
        byte[] imageBytes = null;
        byte[] imageBytes1= null;
        byte[] imageBytes2= null;
        byte[] imageBytes3= null;
        byte[] imageBytes4= null;

    try
    {

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dentistree","root", "tiger");
        Statement st = con.createStatement();
        Statement st1 = con.createStatement();
        Statement st2=con.createStatement();

        ResultSet rs;
        ResultSet rs1;
        ResultSet rs2;

        String query="select * from patient where email='"+email+"'";
        String query1="select * from casehistory where email='"+email+"'";
        String query2="select * from sessionmedia where email='"+email+"'";

        rs=st.executeQuery(query);
        rs1=st1.executeQuery(query1);
        rs2=st2.executeQuery(query2);
        while(rs.next())
        {
            name=rs.getString(1);
            birthdate=rs.getString(3);
            address=rs.getString(4);
            gender=rs.getString(6);
        }
        while(rs1.next())
        {
            doctor=rs1.getString(2);
            disease=rs1.getString(3);
            pregnant=rs1.getString(4);
            nursing=rs1.getString(5);
            pan=rs1.getString(6);
            smoking=rs1.getString(7);
            allergies=rs1.getString(8);
            medicines=rs1.getString(9);
            treatment=rs1.getString(10);  
            if(pregnant.equals("nopreg"))
            {
                pregnant="No";
            }
            else
            {
                pregnant="Yes";
            }
            if(nursing.equals("nonuture"))
            {
                nursing="No";
            }
            else
            {
                nursing="Yes";
            }
            if(pan.equals("nopan"))
            {
                pan="No";
            }
            else
            {
                pan="Yes";
            }
            if(smoking.equals("yessmoke"))
            {
                smoking="Yes";
            }
            else
            {
                smoking="No";
            }

        }
        while(rs2.next())
        {
            image0= rs.getBlob(3);
            image1= rs.getBlob(4);
            image2= rs.getBlob(5);
            image3= rs.getBlob(6);
            image4= rs.getBlob(7);

        }
        imageBytes = image0.getBytes(1, (int) image0.length());
        Image image00=Image.getInstance(imageBytes);
        image00.scaleAbsolute(300,300);

        Document document = new Document();

        PdfWriter.getInstance(document, response.getOutputStream());
        Image image = Image.getInstance("C:\\Users\\Abhishek\\Desktop\\Capture.png");

        document.open();

        document.add(image);
        document.add(new Paragraph("Doctor Name:    Dr.Seema Maurya"));
        document.add(Chunk.NEWLINE );
        document.add(new Paragraph("Name:"+"            "+name));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(new Paragraph("Date of Birth:"+"  "+birthdate));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(new Paragraph("Address:"+"         "+address));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(new Paragraph("Email-ID:"+"        "+email));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(new Paragraph("Gender:"+"          "+gender));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(Chunk.NEWLINE );
        document.add(new Paragraph("Diseases:"+"        "+disease));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(Chunk.NEWLINE );
        document.add(new Paragraph("Pregnant:"+"        "+pregnant));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(new Paragraph("Nursing:"+"          "+nursing));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(new Paragraph("Pan-Chewing:"+""+pan));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(new Paragraph("Smoking:"+"         "+smoking));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(new Paragraph("Medicines:"+"       "+medicines));
        document.add(new Paragraph(""));
        document.add(new Paragraph(""));
        document.add(Chunk.NEWLINE );
        document.add(new Paragraph("Allergies:"+"       "+allergies));
        document.add(Chunk.NEWLINE );
        document.add(new Paragraph("Session Images:"));
        document.add(Chunk.NEWLINE );
        document.add(image00);
        document.add(Chunk.NEWLINE );



        document.close();
    } 
    catch (DocumentException de) {
        throw new IOException(de.getMessage());
    }
    catch(Exception e)
    {

    }
}
}

0 个答案:

没有答案