首页 > 开发 > 综合 > 正文

从数据库读出的JPG文件的字符流,转换成图片显示

2024-07-21 02:14:31
字体:
来源:转载
供稿:网友


收集最实用的网页特效代码!

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

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();
}

-------------------------------------------------------

再附一个:

从库中读图片显示到页面上的主要源码
 <%@ page contenttype="text/html;charset=iso8859_1"%>
<%@ page import="java.sql.resultset"%>
<jsp:usebean id="query" class="pub.makedb"/>
<%
string sql = "select * from images";
resultset rs = query.executequery(sql);
rs.next();
inputstream in = rs.getbinarystream("image");
servletoutputstream sos = response.getoutputstream();
int len = 0;
byte[] b = new byte[1024];
response.reset();
response.setcontenttype("image/gif");
while((len = in.read(b)) > 0)
sos.write(b,0,len);
in.close();
sos.close();
query.cls();
%>

如果是写到文件只要把sos
定义为dataoutputstream sos = new dataoutputstream(new bufferedoutputstream(new fileoutputstream("/a.gif")));

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表