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

自动扫描FTP文件工具类 ScanFtp.java的更多相关文章

  1. 读取Config文件工具类 PropertiesConfig.java

    package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...

  2. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  3. Java 实现删除文件工具类

    工具代码 package com.wangbo; import java.io.File; /** * 删除目录或文件工具类 * @author wangbo * @date 2017-04-11 1 ...

  4. Java常用工具类---IP工具类、File文件工具类

    package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...

  5. java文件工具类

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  6. java下载文件工具类

    java下载文件工具类 package com.skjd.util; import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  7. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

  8. Android FileUtil(android文件工具类)

    android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...

  9. HTTP 下载文件工具类

    ResponseUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.File; im ...

随机推荐

  1. HDU5619 (费用流)

    Problem Jam's Store (HDU5619) 题目大意 有m个服务员,和n个顾客,给出每个服务员招待每个顾客的时间,每个服务员在同一时间只能服务一个顾客,询问所有顾客完成服务的最少时间. ...

  2. mysql重命名数据表

    命令如下: rename   table oldtablename to newtablename

  3. MySQL单表多字段模糊查询解决方法

    例如现有table表,其中有title,tag,description三个字段,分别记录一条资料的标题,标签和介绍.然后根据用户输入的查询请求,将输入的字串通过空格分割为多个关键字,再在这三个字段中查 ...

  4. 动态数组 - ArrayList

    前言 如果数组的大小要随时间变化,那么数组操作起来就比较麻烦. 在C++中,这种情况要用到动态向量Vector. 而Java中,提供了一种叫做ArrayList的泛型数组结构类型,提供相似的作用. 其 ...

  5. Spring mvc源码url路由-我们到底能走多远系列(38)

    我们到底能走多远系列38 扯淡: 马航的事,挺震惊的.还是多多珍惜身边的人吧. 主题: Spring mvc 作为表现层的框架,整个流程是比较好理解的,毕竟我们做web开发的,最早也经常接触的就是一个 ...

  6. SPAdes

    用后感: 拼个小基因组还好,对于很大的基因组,文库很多的,还是不要用了.服务器768G内存,都不够用.... 主页: http://bioinf.spbau.ru/spades 说明书: http:/ ...

  7. 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  8. Spring AOP 实现功能权限校验功能

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 使用拦截器实现未登录时跳转到登录界面的功能 1 拦截器SecurityInterceptor 2spring-mvcxml拦 ...

  9. MATLAB格式化输出控制 分类: 数学 2015-07-31 23:01 3人阅读 评论(0) 收藏

    MATLAB格式化输出控制 format 默认格式 format short 5字长定点数 format long 15字长定点数 format short e 5字长浮点数 format long ...

  10. docker容器互联

    link方式 http://blog.csdn.net/halcyonbaby/article/details/42112325 通过link方式创建容器,然后我们可以使用被link容器的别名进行访问 ...