自动扫描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 ...
随机推荐
- esayui-datagrid的使用
第一步:在页面上引入easyui的jQuery链接 <script src="../../Scripts/easyUI/jquery-1.7.2.min.js" type=& ...
- 第二个Sprint冲刺第二天
讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 任务:解决了第二个Sprint冲刺第一天遇到的错误. 燃尽图: 遇到的问题: 解决之后: 开发感悟:最近一直在写代码,都很少外出活动了,不知不觉 ...
- oracle注意事项
企业管理器system登陆时必须使用normal模式
- I.MX6 SHT20 Linux 驱动移植
/*********************************************************************** * I.MX6 SHT20 Linux 驱动移植 * ...
- postgresql存储二进制大数据文件
如果想把整个文件或图片存储在数据表的一个字段内,该字段可以选择二进制类型,然后将文件按二进制存储起来,文本文件也可以存在text字段内. 示例如下: 二进制类型bytea的操作(在最大值内,有内存限制 ...
- spring和redis的整合-超越昨天的自己系列(7)
超越昨天的自己系列(7) 扯淡: 最近一直在慢慢多学习各个组件,自己搭建出一些想法.是一个涉猎的过程,慢慢意识到知识是可以融汇贯通,举一反三的,不过前提好像是研究的比较深,有了自己的见解.自认为学习 ...
- vimrc配置文件_version1.0_+pathogen, taglist, wordcomplete插件说明
为了表示对Ruchee的感谢,首先这是Ruchee的个人网站:http://www.ruchee.com/index.html,他的以前很多的代码都放到Git里面了,里面有链接. 看了整整一天,刚开始 ...
- Oracle数据库Linux下的导出EXP
先转一篇 ================================我是分割线================================ 时间:2013-06-22 13:48来源:未知 ...
- PocketSphinx语音识别和turtlebot的语音控制--18
摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.首先安装 PocketSphinx 语音识别: $ sudo apt--pocketsphi ...
- ubuntu14.04 and ros indigo install kinect driver--16
摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 今日多次测设ros indigo install kinect driver ,提示各种失败,然 ...