• 自动扫描FTP文件工具类 ScanFtp.java


    1. package com.util;  
    2.   
    3. import java.io.BufferedInputStream;  
    4. import java.io.BufferedOutputStream;  
    5. import java.io.File;  
    6. import java.io.FileInputStream;  
    7. import java.io.FileOutputStream;  
    8. import java.io.IOException;  
    9.   
    10. /** 
    11.  * 自动扫描FTP文件工具类 
    12.  * 需要定时执行 
    13.  */  
    14. public class ScanFtp {  
    15.     //服务器图片路径文件夹  
    16.     private String serverLocal = "D:/TOOLS/Tomcat 6.0/webapps/BCCCSM/modelforcast/";  
    17.     //图片上传文件夹存放路径,文件夹内应包含AGCM CSM ZS 3个子文件夹分别存放需要扫描到tomcat中的图片  
    18.     private String saveLocal = "D:/modelForcast/";  
    19.     /** 
    20.      * 获得远程权限 
    21.      * @return 
    22.      */  
    23.     private void getFTPAdress(){  
    24.         //登陆成功  
    25.     }  
    26.       
    27.     /** 
    28.      * 开始扫描 
    29.      * @throws IOException  
    30.      */  
    31.     private void scan() throws IOException {  
    32.         this.getFTPAdress();  
    33.         File file = new File(saveLocal + "AGCM");  //打开AGCM  
    34.         File[] array = file.listFiles();  
    35.         String fileName;  
    36.         File fileTemp;  
    37.         for(int i = 0; i < array.length; i++){  
    38.             if(array[i].isFile()) {  
    39.                 fileTemp = array[i];  
    40.                 fileName = fileTemp.getName();//取出文件名  
    41.                 if (!fileName.equals("humbs.db")) {  
    42.                     this.saveFile(fileTemp, 1);//分析每一个文件名字并存储  
    43.                     System.out.println(fileName + " saved");  
    44.                 }  
    45.             }     
    46.         }  
    47.           
    48.         file = new File(saveLocal + "CSM");  //打开CSM  
    49.         array = file.listFiles();  
    50.         for(int i = 0; i < array.length; i++){  
    51.             if(array[i].isFile()) {  
    52.                 fileTemp = array[i];  
    53.                 fileName = fileTemp.getName();//取出文件名  
    54.                 if (!fileName.equals("humbs.db")) {  
    55.                     this.saveFile(fileTemp, 2);//分析每一个文件名字并存储  
    56.                     System.out.println(fileName + " saved");  
    57.                 }  
    58.             }     
    59.         }  
    60.           
    61.         file = new File(saveLocal + "ZS");  //打开ZS  
    62.         array = file.listFiles();  
    63.         for(int i = 0; i < array.length; i++){  
    64.             if(array[i].isFile()) {  
    65.                 fileTemp = array[i];  
    66.                 fileName = fileTemp.getName();//取出文件名  
    67.                 if (!fileName.equals("humbs.db")) {  
    68.                     this.saveFile(fileTemp, 3);//分析每一个文件名字并存储  
    69.                     System.out.println(fileName + " saved");  
    70.                 }  
    71.             }     
    72.         }  
    73.           
    74.   
    75.     }  
    76.       
    77.     /** 
    78.      * 开始执行 
    79.      * @throws IOException  
    80.      */  
    81.     public void execute() throws IOException{  
    82.         scan();//开始扫描  
    83.     }  
    84.       
    85.     /** 
    86.      * 按类型存储 
    87.      * @param file 
    88.      * @param type 
    89.      * @throws IOException  
    90.      */  
    91.     private void saveFile(File file, int type) throws IOException {  
    92.         String fileName = file.getName();  
    93.         //类型A C 和 指数3种  
    94.         String year = fileName.substring(1, 5);//获得发布年份  
    95.         String date = fileName.substring(5, 9);//获得发布日期包含月日  
    96.         String var = null;//获得变量名字  
    97.         String dir = serverLocal;//存储目录名字  
    98.         if (type == 1 ) {  
    99.             var = fileName.substring(11, 15);  
    100.             dir = dir + "AGCM/" + var + "/" + year + "/" + date;  
    101.         } else if(type == 2) {  
    102.             var = fileName.substring(11, 15);  
    103.             dir = dir + "CSM/" + var + "/" + year + "/" + date;  
    104.         } else {  
    105.             var = fileName.substring(11, 15);//指数的暂时没处理  
    106.             dir = dir + "ZS/" + var + "/" + year + "/" + date;  
    107.         }  
    108.         //判断是否存在这样的目录没有就自动创建  
    109.         File savePath = new File(dir);  
    110.         if(!savePath.exists()) {  
    111.             savePath.mkdirs();  
    112.         }  
    113.         File saveFile = new File(dir + "/" + fileName);  
    114.         if(!saveFile.exists()){//如果不存在,就存文件  
    115.             FileInputStream fis = null;//这里用本地复制暂时代替FTP  
    116.             FileOutputStream fos =null;  
    117.             BufferedInputStream bis =null;  
    118.             BufferedOutputStream bos =null;    
    119.             int c;  
    120.             fis = new FileInputStream(file);  
    121.             bis = new BufferedInputStream(fis);  
    122.             fos = new FileOutputStream(dir + "/" + fileName);  
    123.             bos = new BufferedOutputStream(fos);  
    124.             while((c = bis.read())!= -1)  
    125.                 bos.write(c);  
    126.             bos.flush();  
    127.             if(bos != null) bos.close();  
    128.             if(bis != null) bis.close();  
    129.             if(fos != null) fos.close();  
    130.             if(fis != null) fos.close();  
    131.         } else {  
    132.             System.out.println("文件已经存在,不进行存储,可清理当前文件.");  
    133.         }  
    134.     }  
    135.       
    136.       
    137.     /** 
    138.      * 测试方法 
    139.      * @param argv 
    140.      * @throws IOException  
    141.      */  
    142.     public static void main(String argv[])  {  
    143.         ScanFtp s = new ScanFtp();  
    144.         try {  
    145.             s.scan();  
    146.         } catch (IOException e) {  
    147.             // TODO Auto-generated catch block  
    148.             e.printStackTrace();  
    149.         }  
    150.     }  
  • 相关阅读:
    Eclipse自动换行插件
    JAVA中super与this的区别
    外网访问PG数据库,如何赋予IP访问权限
    PostgreSQL环境变量与psql命令的替代作用
    \l 的使用
    一次生成任意多行数据的语句
    equals与==的区别
    PostgreSQL 名词理解EXPLAIN VERBOSE
    PG坑爹的数组定义
    【收藏】常用的ftp命令
  • 原文地址:https://www.cnblogs.com/swite/p/5168734.html
Copyright © 2020-2023  润新知