Thứ Tư, 21 tháng 9, 2016

GET LIST FILE IN SERVER AND TRANFER TO DATABASE


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

public class get_file_list 
{
    /**
     * List all the files and folders from a directory
     * @param directoryName to be listed
     */
    public void listFilesAndFolders(String directoryName){
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            System.out.println(file.getName());
        }
    }
    /**
     * List all the files under a directory
     * @param directoryName to be listed
     */
    public void listFiles(String directoryName)
      throws SQLException
    {
     int ret_code;
     Connection conn = null;
      conn = DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(load_balance=yes)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=e)(failover_mode=(type=select)(method=basic)(retries=32)(delay=4))))", "xx", "xxxxx");
            
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            if (file.isFile())
            {
                System.out.println(file.getName());
                String sql2 = "INSERT INTO dir_list_ecm VALUES (?)";
                System.out.println(conn);
            //PreparedStatement pstmt1 = conn.prepareStatement(sql1);
              PreparedStatement pstmt2 = conn.prepareStatement(sql2);
          
              pstmt2.setString(1, file.getName());
              pstmt2.executeUpdate();
              System.out.println("finis");
               pstmt2.close();
              
            }
        }
        conn.close();
    }
    /**
     * List all the folder under a directory
     * @param directoryName to be listed
     */
    public void listFolders(String directoryName){
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            if (file.isDirectory()){
                System.out.println(file.getName());
            }
        }
    }
    /**
     * List all files from a directory and its subdirectories
     * @param directoryName to be listed
     */
    public void listFilesAndFilesSubDirectories(String directoryName){
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            if (file.isFile()){
                System.out.println(file.getAbsolutePath());
            } else if (file.isDirectory()){
                listFilesAndFilesSubDirectories(file.getAbsolutePath());
            }
        }
    }
    
   
    public static void main (String[] args)  throws SQLException
    {
     get_file_list listFilesUtil = new get_file_list();
        final String directoryLinuxMac ="/backup/ECM_TTQT";
        //Windows directory example
        final String directoryWindows ="D://Works";
        listFilesUtil.listFiles(directoryLinuxMac);
        
    }

}
================================================
Note :  Grant_permission

declare 
begin 
 dbms_java.grant_permission('SYSTEM','java.util.PropertyPermission','*','read');
 dbms_java.grant_permission('SYSTEM','java.net.SocketPermission','*','connect, resolve');
 dbms_java.grant_permission('SYSTEM','java.io.FilePermission','/backup/ECM_TTQT','read');
end;


Không có nhận xét nào:

Đăng nhận xét