公共无效 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")));