從資料庫讀出的JPG檔案的字元流,轉換成圖片顯示

來源:互聯網
上載者:User
資料|資料庫|顯示|轉換

從資料庫讀出的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")));



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。