当前位置:早雪网网络学院编程文档Java → 从数据库读出的JPG文件的字符流,转换成图片显示在页面上的相关代码

从数据库读出的JPG文件的字符流,转换成图片显示在页面上的相关代码

减小字体 增大字体 作者:不详  来源:supcode.com收集整理  发布时间:2005-7-22 18:51:32
public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    ServletOutputStream out = res.getOutputStream();
    Statement dispStmt = null, setStmt = null;
    try {
        dispStmt = Con.createStatement();
        setStmt = Con.createStatement();
        setStmt.executeUpdate("set textsize 2048000");
    } catch (Exception e) {
        out.println("Create Statement error:" + e.toString() + "<br>");
    }

    String ls_sql = null;
    ls_sql = req.getParameter("ImageSQL");
    if (ls_sql == null)
        ls_sql = "";
    ls_sql = ls_sql.trim();
    if (!ls_sql.equals("")) {
        ResultSet rs = null;
        try {
            rs = dispStmt.executeQuery(ls_sql);
        } catch (Exception e) {
            System.out.println(
                "Unable to Exec Statment" + e.toString() + "<br>");
        }
        try {
            while (rs.next()) {
                try {
                    res.setContentType("image/jpeg");
                    InputStream is = rs.getBinaryStream(1);
                    int size = is.available();
                    byte[] bzp = new byte[size];
                    is.read(bzp);
                    out.write(bzp);
                } catch (Exception e) {
                    System.out.println("Wirte image error: " + e.toString());
                }
            }
            rs.close();
        } catch (Exception e) {
            System.out.println("Unable to Close Statment" + e.toString());
        }
    }
    try {
        dispStmt.close();
    } catch (Exception e) {
        System.out.println("Close Statement Error: " + e.toString());
    }
    out.close();
}

Tags:数据库,读出,JPG,文件,字符,换成,图片,显示,面上,相关,代码
[数据载入中...] [返回上一页] [打 印]

文章评论评论内容只代表网友观点,与本站立场无关!

  评论人:翻译公司   打分:100 分  发表时间:2007-7-26 17:29:53
· 北京新译通&lt;ahref=http://www.baitong.com.cn&gt;翻译公司&lt;/a&gt;:是经北京市工商局正式注册的...