ftp操做

package cn.tisson.icsp.task;java


import java.io.BufferedInputStream;spring

import java.io.BufferedReader;服務器

import java.io.File;this

import java.io.FileInputStream;spa

import java.io.InputStreamReader;.net

import java.util.Calendar;code

import java.util.ResourceBundle;server


import org.springframework.beans.factory.annotation.Autowired;ip

import org.springframework.scheduling.annotation.Scheduled;get

import org.springframework.stereotype.Service;


import cn.tisson.icsp.ftp.common.FileUtil;

import cn.tisson.icsp.service.ObstacleSvc;

import cn.tisson.icsp.service.TimeTaskService;

import cn.tisson.icsp.utils.FtpApche;

import cn.tisson.icsp.utils.GetFileCharsetUtils;

/**

 * 嚴障信息

 * @author Administrator

 *

 */

@Service("obstacleTask")

public class ObstacleTask {

public static ResourceBundle RESOURCE_BUNDLE = 

ResourceBundle.getBundle("cn.tisson.icsp.commonConfig");

protected String ftp_server_ip=RESOURCE_BUNDLE.getString("ftp_server_ip");

protected int ftp_server_port=new Integer(RESOURCE_BUNDLE.getString("ftp_server_port"));

protected String ftp_server_username=RESOURCE_BUNDLE.getString("ftp_server_username");

protected String ftp_server_password=RESOURCE_BUNDLE.getString("ftp_server_password");


@Autowired

TimeTaskService timeTaskService;

@Autowired

ObstacleSvc obstacleSvc;

@Scheduled(cron="0 0/1 * * * ?")

//@Scheduled(cron="0 30 8 * * ?")

public void run() {

System.out.println("=====ObstacleTask======");

try {

int lockCount=this.timeTaskService.findUnLockTimeLockJob("Obstacle_LOCK");

if(lockCount>0){//判斷鎖是否在解鎖狀態

timeTaskService.updateLockTimeLockJob("Obstacle_LOCK");//鎖定

String date = null;

       Calendar cal = Calendar.getInstance();

       cal.add(Calendar.DAY_OF_MONTH, -1);

       date = "" + cal.get(Calendar.YEAR);

       date += cal.get(Calendar.MONTH) >= 9 ? (cal.get(Calendar.MONTH) + 1) : ("0" + (cal.get(Calendar.MONTH) + 1));

       date += cal.get(Calendar.DAY_OF_MONTH) >= 10 ? cal.get(Calendar.DAY_OF_MONTH) : ("0" + cal.get(Calendar.DAY_OF_MONTH));

       date="20151115";

       String filename = "gz_obstacle_"+date+".txt";//要down的文件

                //FTP服務器hostname  端口   登陸名         密碼 

FtpApche.downFile(ftp_server_ip, ftp_server_port, ftp_server_username, ftp_server_password,

//FTP服務器上的相對路徑    文件名          下載後保存到本地的路徑

               "/predeal", filename, System.getProperty("user.dir"));

// String code = GetFileCharsetUtils.getFilecharset((System.getProperty("user.dir") + File.separator + filename));

//ANSI文件用GBK打開

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(System.getProperty("user.dir") + File.separator + filename), "GBK"));

       String line = null;

       int i = 1;

       while ((line = br.readLine()) != null) {

        String[] cols = line.split("※");

        if(i!=1){

        Long l=Long.parseLong(cols[8]);

        this.obstacleSvc.saveObstacle(cols[0], cols[2], cols[3], cols[4], cols[5], cols[6], cols[7], l);

        System.out.println("插入嚴障信息");

        }

        i++;

       }

       br.close();

       FileUtil.fileDelete(System.getProperty("user.dir") + File.separator + filename);

timeTaskService.updateUnLockTimeLockJob("Obstacle_LOCK");//解鎖

}

}catch (Exception e) {

e.printStackTrace();

timeTaskService.updateUnLockTimeLockJob("Obstacle_LOCK");//解鎖

}

}

}

相關文章
相關標籤/搜索