java
public class ImageAction extends ActionSupport implements ServletResponseAware{ private static final String URL = "jdbc:mysql://localhost:3306/test?user=root&password=qweqwe1314&useUnicode=true"; private Connection conn = null; private PreparedStatement pstmt = null; private ResultSet rs = null; //private File file = null; private int picID = 1; //private String outfile = "e:/llj/2.jpg"; private ServletOutputStream sout; private HttpServletResponse response; public String execute() throws Exception { // FileOutputStream fos = null; // InputStream is = null; // byte[] Buffer = new byte[4096]; Class.forName("org.gjt.mm.mysql.Driver").newInstance(); conn = (Connection) DriverManager.getConnection(URL); pstmt = (PreparedStatement) conn .prepareStatement("select pic from tmp where id=?"); pstmt.setInt(1, picID); // 传入要取的图片的ID rs = pstmt.executeQuery(); if (rs.next()) { Blob photo = (Blob) rs.getBlob("pic"); InputStream in = photo.getBinaryStream(); response.reset(); sout = response.getOutputStream(); byte[] b = new byte[1024]; int len = in.read(b); while (len != -1) { sout.write(b); len = in.read(b); } sout.flush(); sout.close(); in.close(); } return "success"; }
jsp
<img src="/Entrance/showPic.htm"/>