JDBC上传下载文件

    技术2022-05-20  57

    public static void upload() throws Exception{  Class.forName("com.mysql.jdbc.Driver");  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  String sql = "insert into testfile(files) values(?)";  PreparedStatement pst = con.prepareStatement(sql);  pst.setBinaryStream(1, new FileInputStream("F://项目//image//pubBack.png"));  int c = pst.executeUpdate();  System.out.println(c);  System.out.println("finish"); } public static void downLoad()throws Exception{  Class.forName("com.mysql.jdbc.Driver");  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  String sql = "select files from testfile where id = 1";  PreparedStatement pst = con.prepareStatement(sql);  ResultSet rs = pst.executeQuery();  if(rs.next()){   BufferedInputStream bis = new BufferedInputStream(rs.getBinaryStream("files"));   BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D://t.png"));   int i;   while ((i = bis.read())!= -1) {    bos.write(i);   }   bis.close();   bos.close();  }  System.out.println("finish");

     }

    ///数据库中类型

    MySQL的四种BLOB类型类型 大小(单位:字节)TinyBlob 最大 255Blob 最大 65KMediumBlob 最大 16MLongBlob 最大 4Gsql server中、、、、、、、、、、、、

    二进制字符串:

                  binary       image              varbinary


    最新回复(0)