import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.sql.*; public class TestBlobToLocal { /** * @description TODO * @param args 将oracle 数据库中的blob字段文件转出至本地 使用jar:ojdbc8-21.0.0 和 orai18n-21.5.0.0 根据报错添加该包 * @return void * @author sayyes * @date 2022/5/19 14:26:53 */ public static void main(String[] args) { String s =null; int error=0; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","root","123"); if(!conn.isClosed()){ System.out.println("Succeeded connecting to the Database!"); } PreparedStatement stmt = conn.prepareStatement("select FILENAME,BlobCONTENT from RS_MENUPUBINFOFILE where FILE_ID ='35ec99d5.1595826b4f4.-7fd4' "); ResultSet rs = stmt.executeQuery(); while(rs.next()){ System.out.println("rs不为空开始处理相关附件"); oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BlobCONTENT"); String file_ =rs.getString("FILENAME"); String filepath ="D:/oldFile/"+file_; InputStream in = blob.getBinaryStream(); // 建立输出流 FileOutputStream file = new FileOutputStream(filepath); int len = (int) blob.length(); byte[] buffer = new byte[len]; // 建立缓冲区 while ((len = in.read(buffer)) != -1) { file.write(buffer, 0, len); } file.close(); in.close(); } }catch(Exception e){ System.out.println(e.getMessage()); } } }
Oracle将blob字段存储文件保存到本地
最新推荐文章于 2024-04-26 16:37:52 发布