zoukankan      html  css  js  c++  java
  • struts2从数据库中读取mysql的Blob格式图片和sqlserver中image格式图片显示到jsp上

    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"/>
  • 相关阅读:
    生活重心
    做自己才对,想多只会徒增烦恼
    列下计划,一个个实现吧
    公司搬迁
    限制文件的类型
    总结
    mvc mvp mvvm区别
    sessionStorage
    localStorage点击次数存储
    2016.09.01 html5兼容
  • 原文地址:https://www.cnblogs.com/hqr9313/p/2669134.html
Copyright © 2011-2022 走看看