日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

JDBC BLOB文件存取

作者:__渡己 更新時(shí)間: 2022-07-19 編程語(yǔ)言
  • 寫 區(qū)別
    用流替換占位符
  • 寫 getBinaryStream(列名) 返回InputStream
  • 代碼
 public static void main(String[] args)  {
        String sql_1 ="update test set img = ? where id = ?";
        String sql_2 ="select img from test where id = ?";
        InputStream binaryStream = null;
        try (FileOutputStream fos = new FileOutputStream("animate1.jpg");
             FileInputStream fis = new FileInputStream("C:\\Users\\123\\Desktop\\可愛(ài).jpg");
             Connection connection = JDBCUtil.getConnection();
             PreparedStatement ps = Objects.requireNonNull(connection).prepareStatement(sql_2);
        ) {
//            讀入圖片
//                ps.setObject(1,fis);
//                ps.setObject(2,1);
//                ps.execute();


//            讀出圖片
            ps.setObject(1,1);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                binaryStream =  rs.getBinaryStream("img");
                int len;
                byte[] buffer  = new byte[1024];
                while ((len = binaryStream.read(buffer))!=-1) {
                    fos.write(buffer, 0, len);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(binaryStream!=null)
                    binaryStream.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

補(bǔ)充

  • 默認(rèn)BLOB最大65k 用 MEDIUMBLOB類型最大16M
  • max_allowed_packet = 16M 修改系統(tǒng)變量,不然最大就是最大只能1M
  • new FileOutputStream(“animate1.jpg”)構(gòu)造方法封裝了創(chuàng)建File的匿名類

原文鏈接:https://blog.csdn.net/qq_51682771/article/details/125859730

欄目分類
最近更新