博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将图片以Blob格式存入数据库,再通过Servlet显示到界面
阅读量:7267 次
发布时间:2019-06-29

本文共 2639 字,大约阅读时间需要 8 分钟。

1:为了方便测试,直接将1.png图片存入到数据库中.

public static void storePicBlog() throws FileNotFoundException, SQLException, IOException{        Connection conn = JdbcUtil.getConnection();        File f = new File("1.jpg");        FileInputStream fis = new FileInputStream(f);                String sql = "insert into pic_test(id,zp) values(?,?)";                PreparedStatement ps = conn.prepareStatement(sql);                ps.setString(1, StringUtil.getUUID());        ps.setBinaryStream(2, fis, (int)f.length());                ps.executeUpdate();                fis.close();        ps.close();        JdbcUtil.release(conn);    }

2:进行读取操作

public static InputStream getPicInputStream(){        String id = "f304733361e243779b2340afe20e62bf";                 Connection conn = JdbcUtil.getConnection();                  PreparedStatement ps = null;                  ResultSet rs = null;                  InputStream is = null;                  String sql = "select zp from pic_test where id= ?";                 try {                        ps = conn.prepareStatement(sql);                        ps.setString(1, id);                        rs = ps.executeQuery();                        if(rs.next()){                is = rs.getBlob("zp").getBinaryStream();            }                    } catch (SQLException ex) {            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);        }finally{            try {                if(null!=ps){                    ps.close();                }            } catch (SQLException ex) {                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);            }            try {                if(null!=rs){                    rs.close();                }            } catch (SQLException ex) {                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);            }            JdbcUtil.release(conn);        }                         return is;    }

3:测试Servlet,通过浏览器访问此Servlet,一定要设置:

response.setContentType("image/jpeg");浏览器中才可以将图片显示出来
protected void processRequest(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("image/jpeg");                InputStream is = Test.getPicInputStream();                if(null!=is){                       OutputStream os = response.getOutputStream();                        int len;                        byte buf[] = new byte[1024];                        while((len=is.read(buf))!=-1){                os.write(buf, 0, len);            }                        is.close();            os.close();        }            }

 

转载地址:http://rvycm.baihongyu.com/

你可能感兴趣的文章
不错的视频播放flash:flowplayer
查看>>
Rational Rose
查看>>
Webbrowser控件中屏蔽弹出脚本错误对话框
查看>>
error: The following untracked working tree files would be overwritten by merge: 错误解决
查看>>
Perl 输出颜色
查看>>
Lambda表达式常用代码示例
查看>>
UML用例图
查看>>
[NHibernate]NHibernate.Tool.hbm2net
查看>>
Java中的集合框架
查看>>
2019年6月14日 Web框架之Django_07 进阶操作(MTV与MVC、多对多表三种创建方式、前后端传输数据编码格式contentType、ajax、自定义分页器)...
查看>>
Vue 爬坑之路(一)—— 使用 vue-cli 搭建项目
查看>>
dView实现checkbox全选反选(自带的ShowSelectCheckBOx)并获取选中checkbox对应的值集合...
查看>>
[HNOI 2016] 树
查看>>
JAVA中的Session和Cookie【转】
查看>>
16.构造函数和析构函数
查看>>
iOS - 选取相册中iCloud云上图片和视频的处理
查看>>
从零开始学架构二 架构设计流程
查看>>
日记:八月份的总结
查看>>
Liunx 特殊权限 suid sgid t
查看>>
Memcache的安装和使用【转】
查看>>