自动扫描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 ...
随机推荐
- bzoj4034 (树链剖分+线段树)
Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...
- CODEVS3123 a*b problem plus (FFT)
type xh=record x,y:double; end; arr=..] of xh; var n,m:longint; s1,s2:ansistring; a,b,g,w:arr; ch:ch ...
- WPF Step By Step 系列 - 开篇 ·
WPF Step By Step 系列 - 开篇 公司最近要去我去整理出一个完整的WPF培训的教程,我刚好将自己学习WPF的过程和经验总结整理成笔记的方式来讲述,这里就不按照书上面的东西来说了,书本上 ...
- 即使连网了ping也会失败
/*************************************************************************** * 即使连网了ping也会失败 * 说明: * ...
- LeetCode Kth Smallest Element in a BST(数据结构)
题意: 寻找一棵BST中的第k小的数. 思路: 递归比较方便. /** * Definition for a binary tree node. * struct TreeNode { * int v ...
- Dubbo_Admin安装
1.下载dubbo 我上传地址:http://download.csdn.net/detail/liweifengwf/7784901 官方地址:http://code.alibabatech.com ...
- win7 64 下安装ubuntu14.04
win7下安装ubuntu方法: * 使用win7下的自带的分区工具给ubuntu留出磁盘空间:计算机 -> 右键菜单选择管理 -> 选择磁盘管理->选中最后的那个磁盘->右键 ...
- 用python做爬虫的例子
主要就是用了两个库,urllib和BeautifulSoup. 作用是从HTML中解析出解梦的查询词和具体的解释. # -*- coding: utf-8 -*- import urllib, url ...
- leetcode 148. Sort List ----- java
Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...
- leetcode 140. Word Break II ----- java
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...