公共無效 doPost(HttpServletRequest req,HttpServletResponse res)
拋出 ServletException、IOException {
ServletOutputStream out = res.getOutputStream();
語句 dispStmt = null, setStmt = null;
嘗試 {
dispStmt = Con.createStatement();
setStmt = Con.createStatement();
setStmt.executeUpdate("設定文字大小 2048000");
} catch (異常 e) {
out.println("建立語句錯誤:" + e.toString() + "<br>");
}
字串 ls_sql = null;
ls_sql = req.getParameter("ImageSQL");
如果(ls_sql == null)
ls_sql = "";
ls_sql = ls_sql.trim();
if (!ls_sql.equals("")) {
結果集 rs = null;
嘗試 {
rs = dispStmt.executeQuery(ls_sql);
} catch (異常 e) {
System.out.println(
"無法執行語句" + e.toString() + "<br>");
}
嘗試 {
while (rs.next()) {
嘗試 {
res.setContentType(“圖片/jpeg”);
輸入流 = rs.getBinaryStream(1);
int 大小 = is.available();
byte[] bzp = 新位元組[大小];
is.read(bzp);
輸出.write(bzp);
} catch (異常 e) {
System.out.println("寫入映像錯誤:" + e.toString());
}
}
rs.close();
} catch (異常 e) {
System.out.println("無法關閉語句" + e.toString());
}
}
嘗試 {
dispStmt.close();
} catch (異常 e) {
System.out.println("關閉語句錯誤:" + e.toString());
}
關閉();
}
------------------------------------------------- -------------------------------
再附一個:
從庫中讀取圖片顯示到頁面上的主要源碼
<%@ 頁面 contentType="text/html;charset=iso8859_1"%>
<%@ page import="java.sql.ResultSet"%>
<jsp:useBean id="query" class="pub.MakeDB"/>
<%
String sql = "從圖像中選擇*";
結果集 rs = query.executeQuery(sql);
rs.next();
輸入流= rs.getBinaryStream(“圖像”);
ServletOutputStream sos = response.getOutputStream();
int 長度 = 0;
位元組[] b = 新位元組[1024];
響應.重置();
回應.setContentType(“圖像/gif”);
while((len = in.read(b)) > 0)
sos.write(b,0,len);
附寄();
sos.close();
查詢.cls();
%>
如果是寫到文件只要把sos
定義為 DataOutputStream sos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("/a.gif")));