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"/>
  • 相关阅读:
    ado.net
    sql基础
    css样式
    HTML基础加强
    socket网络编程
    网络聊天室
    多线程
    WinForm基础
    使用Maven插件(plugin)MyBatis Generator逆向工程
    SpringBoot使用thymeleaf时候遇到无法渲染问题(404/500)
  • 原文地址:https://www.cnblogs.com/hqr9313/p/2669134.html
Copyright © 2011-2022 走看看