2019独角兽企业重金招聘Python工程师标准>>>
指定文件夹下的所有文件上传至oracle的blob字段,java实现
package com.huth;import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.OutputStream;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Blob;import java.util.ArrayList;/*** * 采用简单递归方法读取指定目录下的所有固定后缀的文件(文件夹下可能嵌套子文件夹)**/
public class ReadFiles {private ArrayList<File> files; //目录下的所有文件列表private static String prefix = "doc"; //文件后缀public ReadFiles(ArrayList<File> files){this.files = files;}public void readFilesInDir(String dir)throws FileNotFoundException,IOException{try{File folder = new File(dir);//若dir是文件,直接获取文件名if(!folder.isDirectory()){//System.out.println(folder+"是文件");String fileName = dir +"\\"+folder.getName();//System.out.println("fileName="+fileName);System.out.println("prefix="+fileName.substring(fileName.lastIndexOf(".")+1).trim());if(fileName.substring(fileName.lastIndexOf(".")+1).trim().equalsIgnoreCase(prefix)){files.add(new File(fileName));}}//若dir是文件夹,则获取文件夹内的文件列表//若其中还有文件夹,则递归else if(folder.isDirectory()){//System.out.println(folder+"是文件夹");String[] fileLists = folder.list();//文件夹下的文件名和子文件夹名for(int i=0;i<fileLists.length;i++){//System.out.println("fileLists["+i+"]="+fileLists[i]);File file = new File(dir+"\\"+fileLists[i]);if(!file.isDirectory()){//System.out.println(file+"是文件");String fileName = dir +"\\"+file.getName();//System.out.println("fileName="+fileName);System.out.println("prefix="+fileName.substring(fileName.lastIndexOf(".")+1).trim());if(fileName.substring(fileName.lastIndexOf(".")+1).trim().equalsIgnoreCase(prefix)){files.add(new File(fileName));} }else if(file.isDirectory()){readFilesInDir(dir+"\\"+fileLists[i]);}}}}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException ex){ex.printStackTrace();}}private Connection newConnection(String user, String password){Connection con = null;try{Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();con = DriverManager.getConnection("jdbc:oracle:thin:@172.16.10.249:1521:test",user,password);}catch(SQLException e){e.printStackTrace();return null;}catch(ClassNotFoundException exc){exc.printStackTrace();return null;}catch(IllegalAccessException ex){ex.printStackTrace();return null;}catch(InstantiationException exception){exception.printStackTrace();return null;}return con;}//unit testpublic static void main(String[] args){ArrayList<File> files = new ArrayList<File>();ReadFiles read = new ReadFiles(files);Connection conn = read.newConnection("test", "oracle");ResultSet rs;try{read.readFilesInDir("d:/test");System.out.println("files.size()="+files.size());for(int i=0;i<files.size();i++){String fileName = files.get(i).getName();System.out.println("fileNames["+i+"]="+fileName);FileInputStream fin = new FileInputStream(files.get(i));PreparedStatement stmt = conn.prepareStatement("insert into aa values("+fileName+",empty_blob())");rs = stmt.executeQuery("select text from aa where name = "+fileName+" for update");if(rs.next()){Blob blob = rs.getBlob(1);OutputStream out = ((oracle.sql.BLOB)blob).getBinaryOutputStream();byte[] b = new byte[((oracle.sql.BLOB)blob).getBufferSize()];int len = 0;while((len = fin.read(b))!=-1)out.write(b,0,len);fin.close();out.close();rs.close();conn.commit();}}conn.close();}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException ex){ex.printStackTrace();}catch(SQLException exc){exc.printStackTrace();}}}