只是小人物

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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"/>

 

posted on 2012-09-03 17:19  只是小人物  阅读(924)  评论(0编辑  收藏  举报