-
自动扫描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;
-
- public class ScanFtp {
-
- private String serverLocal = "D:/TOOLS/Tomcat 6.0/webapps/BCCCSM/modelforcast/";
-
- private String saveLocal = "D:/modelForcast/";
-
- private void getFTPAdress(){
-
- }
-
-
- private void scan() throws IOException {
- this.getFTPAdress();
- File file = new File(saveLocal + "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");
- 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");
- 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");
- }
- }
- }
-
-
- }
-
-
- public void execute() throws IOException{
- scan();
- }
-
-
- private void saveFile(File file, int type) throws IOException {
- String fileName = file.getName();
-
- 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;
- 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("文件已经存在,不进行存储,可清理当前文件.");
- }
- }
-
-
-
- public static void main(String argv[]) {
- ScanFtp s = new ScanFtp();
- try {
- s.scan();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
-
相关阅读:
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
润新知