自动扫描FTP文件工具类 ScanFtp.java
- package com.util;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /**
- * 自动扫描FTP文件工具类
- * 需要定时执行
- */
- public class ScanFtp {
- //服务器图片路径文件夹
- private String serverLocal = "D:/TOOLS/Tomcat 6.0/webapps/BCCCSM/modelforcast/";
- //图片上传文件夹存放路径,文件夹内应包含AGCM CSM ZS 3个子文件夹分别存放需要扫描到tomcat中的图片
- private String saveLocal = "D:/modelForcast/";
- /**
- * 获得远程权限
- * @return
- */
- private void getFTPAdress(){
- //登陆成功
- }
- /**
- * 开始扫描
- * @throws IOException
- */
- private void scan() throws IOException {
- this.getFTPAdress();
- File file = new File(saveLocal + "AGCM"); //打开AGCM
- File[] array = file.listFiles();
- String fileName;
- File fileTemp;
- for(int i = 0; i < array.length; i++){
- if(array[i].isFile()) {
- fileTemp = array[i];
- fileName = fileTemp.getName();//取出文件名
- if (!fileName.equals("humbs.db")) {
- this.saveFile(fileTemp, 1);//分析每一个文件名字并存储
- System.out.println(fileName + " saved");
- }
- }
- }
- file = new File(saveLocal + "CSM"); //打开CSM
- array = file.listFiles();
- for(int i = 0; i < array.length; i++){
- if(array[i].isFile()) {
- fileTemp = array[i];
- fileName = fileTemp.getName();//取出文件名
- if (!fileName.equals("humbs.db")) {
- this.saveFile(fileTemp, 2);//分析每一个文件名字并存储
- System.out.println(fileName + " saved");
- }
- }
- }
- file = new File(saveLocal + "ZS"); //打开ZS
- array = file.listFiles();
- for(int i = 0; i < array.length; i++){
- if(array[i].isFile()) {
- fileTemp = array[i];
- fileName = fileTemp.getName();//取出文件名
- if (!fileName.equals("humbs.db")) {
- this.saveFile(fileTemp, 3);//分析每一个文件名字并存储
- System.out.println(fileName + " saved");
- }
- }
- }
- }
- /**
- * 开始执行
- * @throws IOException
- */
- public void execute() throws IOException{
- scan();//开始扫描
- }
- /**
- * 按类型存储
- * @param file
- * @param type
- * @throws IOException
- */
- private void saveFile(File file, int type) throws IOException {
- String fileName = file.getName();
- //类型A C 和 指数3种
- String year = fileName.substring(1, 5);//获得发布年份
- String date = fileName.substring(5, 9);//获得发布日期包含月日
- String var = null;//获得变量名字
- String dir = serverLocal;//存储目录名字
- if (type == 1 ) {
- var = fileName.substring(11, 15);
- dir = dir + "AGCM/" + var + "/" + year + "/" + date;
- } else if(type == 2) {
- var = fileName.substring(11, 15);
- dir = dir + "CSM/" + var + "/" + year + "/" + date;
- } else {
- var = fileName.substring(11, 15);//指数的暂时没处理
- dir = dir + "ZS/" + var + "/" + year + "/" + date;
- }
- //判断是否存在这样的目录没有就自动创建
- File savePath = new File(dir);
- if(!savePath.exists()) {
- savePath.mkdirs();
- }
- File saveFile = new File(dir + "/" + fileName);
- if(!saveFile.exists()){//如果不存在,就存文件
- FileInputStream fis = null;//这里用本地复制暂时代替FTP
- FileOutputStream fos =null;
- BufferedInputStream bis =null;
- BufferedOutputStream bos =null;
- int c;
- fis = new FileInputStream(file);
- bis = new BufferedInputStream(fis);
- fos = new FileOutputStream(dir + "/" + fileName);
- bos = new BufferedOutputStream(fos);
- while((c = bis.read())!= -1)
- bos.write(c);
- bos.flush();
- if(bos != null) bos.close();
- if(bis != null) bis.close();
- if(fos != null) fos.close();
- if(fis != null) fos.close();
- } else {
- System.out.println("文件已经存在,不进行存储,可清理当前文件.");
- }
- }
- /**
- * 测试方法
- * @param argv
- * @throws IOException
- */
- public static void main(String argv[]) {
- ScanFtp s = new ScanFtp();
- try {
- s.scan();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
自动扫描FTP文件工具类 ScanFtp.java的更多相关文章
- 读取Config文件工具类 PropertiesConfig.java
package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- Java 实现删除文件工具类
工具代码 package com.wangbo; import java.io.File; /** * 删除目录或文件工具类 * @author wangbo * @date 2017-04-11 1 ...
- Java常用工具类---IP工具类、File文件工具类
package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...
- java文件工具类
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- java下载文件工具类
java下载文件工具类 package com.skjd.util; import java.io.BufferedInputStream; import java.io.BufferedOutput ...
- Property工具类,Properties文件工具类,PropertiesUtils工具类
Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...
- Android FileUtil(android文件工具类)
android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...
- HTTP 下载文件工具类
ResponseUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.File; im ...
随机推荐
- HDU 1080
http://acm.hdu.edu.cn/showproblem.php?pid=1080 二维最长公共子序列 #include <iostream> #include <cstd ...
- Inno Setup 插件大全
Inno Setup 插件大全 这是我收集到的目前网上最全的插件之一,里面的每个插件,都有详细的脚本示例来讲解该插件的具体用法.另外,下载了我公开的脚本的朋友,也有可能会被提示缺少文件,如果缺 ...
- [转载]Magento 店铺多语言设置
本文以扩展中文包为例: 首先进入自己 Magento 后台 系统 -> 管理商店(System -> Manage Stores) 单击 “创建店铺视图”(Create Store Vie ...
- magento memcache缓存配置
在app/etc/local.xml <global>配置段中添加 cache段配置 <config> <global> <install> <d ...
- pcr free library 介绍
一句话:illumina的建库方法,建库时间段,质量还好... the adapters are different in the PCR-free kit compared to the stand ...
- 用Rprofile文件配置打开时R的设置
R中经常会使用一些命令,而且需要重复输入,非常麻烦.如果能够一打开R就直接使用会方便很多. 通过配置一个.Rprofile文件,可以达到我们的目的. 注:本文仅适用于Mac # 创建一个.Rprofi ...
- 搭建一个免费的,无限流量的Blog----github Pages和Jekyll入门
喜欢写Blog的人,会经历三个阶段. 第一阶段,刚接触Blog,觉得很新鲜,试着选择一个免费空间来写. 第二阶段,发现免费空间限制太多,就自己购买域名和空间,搭建独立博客. 第三阶段,觉得独立博客的管 ...
- UVa 10810 - Ultra-QuickSort
题目大意:给出一个数列,每次交换相邻数字,求排成递增序的最少交换次数. 分析:求逆序数,合并排序 #include<cstdio>#include<cstring>#inclu ...
- Spring MVC 使用拦截器优雅地实现权限验证功能
在上一篇 SpringAOP 实现功能权限校验功能 中虽然用AOP通过抛异常,请求转发等勉强地实现了权限验证功能,但感觉不是那么完美,应该用拦截器来实现才是最佳的,因为拦截器就是用来拦截请求的,在请求 ...
- sgu548 Dragons and Princesses 贪心+优先队列
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=548 题目意思: 有一个骑士,要经过n个房间,开始在第一个房间,每个房间里面有龙或者 ...