java读取mysql二进制文件

可参考,核心代码借鉴了:

https://blog.csdn.net/iteye_13098/article/details/82648166

注掉的代码,是保存数据。

-----------------------------------------------------------------------------------------------------

package com.ma;

import java.sql.*;
import java.io.*;

public class Storeblobfile {
    public static void main(String[] args) {
        new Storeblobfile().getaaa();

    }


   void getaaa(){

       String Jdbc_Url = "jdbc:mysql://192.168.xxx.xxx:3306/xxx123?useOldAliasMetadataBehavior=true&useUnicode=true&characterEncoding=utf-8";
       String Jdbc_Username = "xxx";
        String Jdbc_Password = "xxx";

       try{
          // FileInputStream file = new FileInputStream("C:\\shanshui.jpg");
           Class.forName("com.mysql.jdbc.Driver");
           Connection conn = DriverManager.getConnection(Jdbc_Url, Jdbc_Username,Jdbc_Password);
           PreparedStatement ps = conn.prepareStatement("insert into user values(?,?,?)");
//           ps.setString(1,"blob");
//           ps.setInt(2,23);
//           ps.setBinaryStream(3, file, file.available());
        //   ps.executeUpdate();
           Statement stmt = conn.createStatement();
           ResultSet rs = stmt.executeQuery("SELECT BYTES_ FROM `act_ge_bytearray`  where  ID_= 17713");
           while(rs.next()){
               Blob blob = rs.getBlob(1);
               InputStream in = blob.getBinaryStream();
               FileOutputStream fout = new FileOutputStream("D:\\copy.jpg");
               int b = -1;
               while((b=in.read())!=-1){
                   fout.write(b);
               }
           }
       }catch(Exception e){
           System.out.println(e.getMessage());
       }

    }
}

--------------------------------------------------------------------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/qq_34085100/article/details/108735342