最近因为自己个人的一个需求,就是把文件存入数据库中,以方便备份,所以就写了这个类,这个类也不完全原创,有参考网上的一些资料,但通过自己的测试,完全可以实现自己的需求,所以把代码贴出来分享一下!
以下的一些路径、数据库表名、字段名 需要自己修改一下,也可以根据自己的需要改成配置的!这个看自己的啦!还有就是那个jdbc工具类自己改一下!
1:WriteAndReadFile 类
- package com.grg.johnny.work;
- import java.io.*;
- import java.sql.*;
- import oracle.sql.BLOB;
- public class WriteAndReadFile {
- public static void main(String[] args){
- //1:先写入
- String path = "D://oracle2.zip";
- saveFile(path);
- //2:再读出
- //getFile("1");
- }
- /**
- * 写入文件
- * 往blob里插入文件要先插入空值empty_blob(),再进行修改
- * @param filePath
- * @return
- */
- public static boolean saveFile(String filePath) {
- File file = new File(filePath);
- Connection conn = JdbcUtil.getConnection();
- try {
- java.sql.Statement st = conn.createStatement();
- conn.setAutoCommit(false);
- System.out.println("=====================save file begin========================");
- // st.execute("insert into johnny_file values(1,'日常生活',empty_blob(),'这是一个很强大的文件',to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'))");
- st.execute("insert into johnny_file values(2,'日常生活2',empty_blob(),'这是一个很强大的文件test',to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'))");
- ResultSet rs = st
- .executeQuery("select id,file_blob from johnny_file where id=2 for update");
- if (rs.next()) {
- BLOB blob = (BLOB) rs.getBlob("file_blob");
- OutputStream outStream = blob.getBinaryOutputStream();
- InputStream fin = new FileInputStream(file);
- byte[] b = new byte[blob.getBufferSize()];
- int len = 0;
- while ((len = fin.read(b)) != -1) {
- outStream.write(b, 0, len);
- }
- fin.close();
- outStream.flush();
- outStream.close();
- conn.commit();
- conn.close();
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- System.out.println("=====================save file end========================");
- return true;
- }
- /**
- * 读取
- * 读取出的路径根据自己的需要修改
- * @param id
- */
- public static void getFile(String id) {
- Connection conn = JdbcUtil.getConnection();
- java.sql.Statement st;
- try {
- st = conn.createStatement();
- System.out.println("=====================get file begin========================");
- ResultSet rs = st
- .executeQuery("select id,file_blob from johnny_file where id='"
- + id + "'");
- if (rs.next()) {
- BLOB blob = (BLOB) rs.getBlob("file_blob");
- File file = new File("D://oracle.zip");
- FileOutputStream output = new FileOutputStream(file);
- InputStream input = blob.getBinaryStream();
- byte[] buffer = new byte[1024];
- int i = 0;
- while ((i = input.read(buffer)) != -1) {
- output.write(buffer, 0, i);
- }
- }
- System.out.println("=====================get file end========================");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 修改blob内容
- */
- public static void updateblob(String id) {
- Connection conn = JdbcUtil.getConnection();
- Statement stem = null;
- ResultSet rs = null;
- try {
- conn.setAutoCommit(false);
- stem = conn.createStatement();
- System.out.println("=====================update file begin========================");
- rs = stem.executeQuery("select file_blob from johnny_file where id='"
- + id + "' for update");
- if (rs.next()) {
- BLOB blob = (BLOB) rs.getBlob("file_blob");
- OutputStream outStream = blob.getBinaryOutputStream();
- InputStream fin = new FileInputStream("D://ok.zip");
- byte[] b = new byte[blob.getBufferSize()];
- int len = 0;
- while ((len = fin.read(b)) != -1) {
- outStream.write(b, 0, len);
- }
- fin.close();
- outStream.flush();
- outStream.close();
- conn.commit();
- conn.close();
- }
- System.out.println("=====================update file end========================");
- } catch (Exception ex) {
- try {
- conn.rollback();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(ex.getMessage());
- }
- }
- }
2:以下这个是我自己的jdbc工具类
- package com.grg.johnny.work;
- import java.sql.*;
- public class JdbcUtil {
- /**
- * driverName
- * */
- static{
- String driverName="oracle.jdbc.driver.OracleDriver";
- try{
- Class.forName(driverName);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- /**
- * getConnection
- * */
- public static Connection getConnection(){
- String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
- String userName="XXXXX";
- String pwd="XXXXXX";
- Connection con = null;
- try{
- con = DriverManager.getConnection(url,userName,pwd);
- }catch(Exception ee){
- ee.printStackTrace();
- }
- return con;
- }
- /**
- * close
- * */
- public static void close(ResultSet rs,Statement stmt,Connection con){
- try{
- if(rs!=null){rs.close();}
- }catch(Exception ee){
- ee.printStackTrace();
- }
- try{
- if(stmt!=null){stmt.close();}
- }catch(Exception ee){
- ee.printStackTrace();
- }
- try{
- if(con!=null){con.close();}
- }catch(Exception ee){
- ee.printStackTrace();
- }
- }
- }
转载于:https://blog.51cto.com/johnny84/654821