public void doPost(HttpServletRequest req, HttpServletResponse res)
wirft ServletException, IOException {
ServletOutputStream out = res.getOutputStream();
Anweisung dispStmt = null, setStmt = null;
versuchen {
dispStmt = Con.createStatement();
setStmt = Con.createStatement();
setStmt.executeUpdate("set textsize 2048000");
} Catch (Ausnahme e) {
out.println("Create Statement error:" + e.toString() + "
");
}
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;
versuchen {
rs = dispStmt.executeQuery(ls_sql);
} Catch (Ausnahme e) {
System.out.println(
„Unable to Exec Statment“ + e.toString() + „
“);
}
versuchen {
while (rs.next()) {
versuchen {
res.setContentType("image/jpeg");
InputStream ist = rs.getBinaryStream(1);
int size = is.available();
byte[] bzp = neues Byte[Größe];
is.read(bzp);
out.write(bzp);
} Catch (Ausnahme e) {
System.out.println("Wirte image error: " + e.toString());
}
}
rs.close();
} Catch (Ausnahme e) {
System.out.println("Kontoauszug konnte nicht geschlossen werden" + e.toString());
}
}
versuchen {
dispStmt.close();
} Catch (Ausnahme e) {
System.out.println("Close Statement Error: " + e.toString());
}
out.close();
}
------------------------------------------------- ---------------------------------------------
再附一个:
从库中读图片显示到页面上的主要源码
<%@ page contentType="text/html;charset=iso8859_1"%>
<%@ page import="java.sql.ResultSet"%>
<%
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 = neues Byte[1024];
Antwort.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")));