FTP上传文件

package com.cc.bvc.socket.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;

import com.cc.bvc.core.log.Bill99VcLogger;
import com.cc.bvc.model.hibernate.pojo.DzFileBean;
import com.cc.bvc.socket.dao.impl.DzFileDAO;
import com.cc.bvc.socket.service.IFtpUpfileService;
import com.cc.bvc.socket.util.DateUtil;

/**
*<p>title:FTP鏂囦欢涓婁紶瀹炵幇绫?</p>
*<p>Description: </p>
*<p>CopyRight: CopyRight (c) 2009</p>
*<p>Company: 99bill.com</p>
*<p>Create date: Oct 22, 2009</P>
*@author shi_yqiang */
public class FtpUpfileServiceImpl implements IFtpUpfileService {
private String ftpIP = "127.0.0.1";
private int ftpPort = 21;
private String ftpfilePath = "/wf"; //ftp瀛樻斁鏂囦欢鐨勭洰褰?琛ㄧず涓婁紶鐨勬枃浠跺叆鍒版牴鐩綍涓嬬殑wf鏂囦欢澶?
private String loginName; //鐧婚檰鍚?
private String loginpwd; //瀵嗙爜
private sun.net.ftp.FtpClient ftpclient = null;
private static Bill99VcLogger log = Bill99VcLogger.getLogger(FtpUpfileServiceImpl.class);
//private static Logger log = Logger.getLogger(FtpUpfileServiceImpl.class);
private static FtpUpfileServiceImpl instance = null;
private static boolean isStarting = false; //FTP涓婁紶绾跨▼鏄惁寮€鍚姩
private static int dailyTime =0 ; //姣忓ぉ鎵ц浠诲姟鐨勬椂闂?

private FtpUpfileServiceImpl() {
init();//鍒濆鍖栧弬鏁?
}

public static FtpUpfileServiceImpl getInstance() {
if(instance ==null) {
synchronized(FtpUpfileServiceImpl.class){
if(instance==null) {
return new FtpUpfileServiceImpl();
}
}
}
return instance;
}

public void ftpFileToctcc() {
//鍒ゆ柇鏃堕棿鏄惁鍚堟硶
if (!(dailyTime >= 0 && dailyTime <= 24))
throw new IllegalArgumentException("invalid time:" + dailyTime);
if(isStarting) {
return ;
}
//鍒ゆ柇褰撳墠鏃堕棿鏄惁宸茶秴杩囨寚瀹氱殑鏃堕棿
Calendar calendar = Calendar.getInstance();
int currHour = calendar.get(Calendar.HOUR_OF_DAY);
int delayHour = 0;//鍚戝悗寤惰繜鏃堕棿
if (currHour >= 12 && currHour <= 24) {//PM
delayHour = 24 - currHour + dailyTime;
}
if (currHour >= 0 && currHour <= 12) {//AM
delayHour = 12 - currHour + dailyTime + 12;//鍚戝悗寤惰繜鏃堕棿
}
Date firstDate =DateUtil.addSecondBycurrTime(delayHour*60 * 60);
SimpleDateFormat dateformate = new SimpleDateFormat("yyyyMMddHHmmss");
String launchTime = dateformate.format(firstDate);
Timer timer = new Timer();
try {
timer.schedule(new UpfileWorker(), dateformate.parse(launchTime), 1000 * 1 * 60 * 60 * 24);//涓€澶╂墽琛屼竴娆?1000 * 1 * 60 * 60 * 24
} catch (ParseException e) {
log.error("鏃ユ湡瑙f瀽寮傚父锛屽鑷村紓甯哥殑瀛楃涓?"+launchTime);
return ;
}
isStarting = true;
log.info("銆怓TP瀹氭椂涓婁紶浠诲姟宸插惎鍔?瀹氭椂涓婁紶鏃堕棿涓烘瘡澶?+dailyTime+"鐐广€?);
}
private void init() {
Properties conf = new Properties();
try {
String confilePath = FtpUpfileServiceImpl.class.getResource("/").getPath();
confilePath=confilePath.substring(0, confilePath.indexOf("classes"));
confilePath=URLDecoder.decode((confilePath), "utf-8")+"ftpconf.properties";
log.info("銆怓TP閰嶇疆鏂囦欢璺緞銆?{"+confilePath+"}");
conf.load(new FileInputStream(new File(confilePath)));
this.ftpIP = conf.getProperty("ftpip");
this.ftpPort = Integer.parseInt(conf.getProperty("ftpport"));
this.loginName = conf.getProperty("loginName");
this.loginpwd = conf.getProperty("loginPwd");
this.ftpfilePath = conf.getProperty("ftpfilePath");
this.dailyTime = Integer.valueOf(conf.getProperty("dailyexectime"));
log.info("FTP鏈嶅姟鍣↖P:"+ftpIP);
log.info("FTP绔彛:"+ftpPort);
log.info("鐢ㄦ埛鍚?"+loginName);
log.info("瀵嗙爜:"+loginpwd);
log.info("FTP鏂囦欢瀛樻斁璺緞:"+ftpfilePath);
} catch (IOException e) {
log.error("璇诲彇閰嶇疆鏂囦欢鍙戠敓IO娴佸紓甯?, e);
throw new IllegalAccessError("FTP閰嶇疆鏂囦欢寮傚父");
}
}
/**
* 鐢熸垚涓€涓槰鏃ュ璐︽枃浠?
* @return
*/
private static String generateDzFile() {
Date date = DateUtil.addHoursBycurrTime(-24);
SimpleDateFormat dateformate = new SimpleDateFormat("yyyyMMdd");
String yesterdayTime = dateformate.format(date);
List<DzFileBean> list = new DzFileDAO().queryPayListByspecTime(yesterdayTime);
//鐢熸垚涓存椂鏂囦欢
String filePath = Thread.currentThread().getContextClassLoader().getResource(".").getPath();
log.info("涓存椂鏂囦欢璺緞:"+filePath);
String fileName = filePath + "bill99_"+ dateformate.format(date)+ ".dz";
File file = new File(fileName);
try {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
log.error("鍒涘缓鏂囦欢鍙戠敓IO娴佸紓甯?, e);
}
PrintWriter pw = new PrintWriter(file);
String resultStr = "";
//璁$畻鎬婚噾棰?
int totalRecharge = 0;
for (int i = 0; i < list.size(); i++) {
DzFileBean bean = list.get(i);
//鍐欏叆鏂囦欢
resultStr += bean.getSerial() + "|"
+ bean.getDestinationNumber() + "|"
+ bean.getDestinationAttr() + "|"
+ bean.getObjType() + "|"
+ bean.getBalanceTypeId() + "|"
+ bean.getRechargeAmount() + "|"
+ bean.getBalance() +"|"
+ bean.getBalanceUnit() + "|"
+ bean.getEffectiveTime()+"|"
+ bean.getExpirationTime()+"\n";
totalRecharge += Float.valueOf(bean.getRechargeAmount());
}
pw.println(yesterdayTime+"|"+list.size()+"|"+totalRecharge);//鍐欏叆璐﹀崟鏃ユ湡锛岃处鍗曟暟鍙婂厖鍊兼€婚噾棰?
pw.println(resultStr);
pw.flush();
pw.close();
}
} catch (IOException e) {
log.error("IO娴佸紓甯?, e);
}
return file.getAbsolutePath();
}
//鏂囦欢瀹氭椂涓婁紶宸ヤ綔绫?
private class UpfileWorker extends TimerTask {
/**
* 鐧婚檰FTP鏈嶅姟鍣?
* @return true:鐧婚檰鎴愬姛 false:鐧婚檰澶辫触
*/
public boolean logonFTP() {
try {
ftpclient = new FtpClient(ftpIP, ftpPort);
ftpclient.login(loginName, loginpwd);
ftpclient.cd(ftpfilePath); //鍒囨崲宸ヤ綔璺緞
log.info("銆怓TP鐧婚檰鎴愬姛,IP " + ftpIP + ":" + ftpPort + "銆?);
} catch (FtpLoginException e) {
log.info("鏃犳潈闄愯繛鎺?);
return false;
} catch (IOException e) {
log.info("缃戠粶寮傚父");
return false;
}
return true;
}
/**
* 涓婁紶鏂囦欢
* @param fileaname 鍑嗗涓婁紶鐨勬枃浠?
* @return true:鏂囦欢涓婁紶鎴愬姛 false:鏂囦欢涓婁紶澶辫触
*/
public boolean upFile(String filename) {
TelnetOutputStream ftpout = null;
TelnetInputStream ftpin = null;
try {
ftpclient.binary(); //浜岃繘鍒舵ā寮?
//鎴彇鏂囦欢鍚?
String upname=filename.substring(filename.lastIndexOf("bill99"));
log.info("FTP涓婁紶鏂囦欢鍚?"+upname);
ftpout = ftpclient.put(upname);//filenamet鏄笂浼犲埌ftp鏈嶅姟鍣ㄥ悗鐨勬枃浠跺悕
ftpin = new TelnetInputStream(new FileInputStream(filename),false);
byte[] mybytes = new byte[1024];
int bufferSize = 0;
while ((bufferSize = ftpin.read(mybytes, 0, mybytes.length)) != -1) {
ftpout.write(mybytes, 0, bufferSize);
}
ftpin.close();
ftpout.close();
} catch (IOException e) {
log.info("涓婁紶鏂囦欢鏃跺彂鐢熺綉缁滃紓甯?, e);
return false;
} finally {
try {
ftpin.close();
ftpout.close();
ftpclient.closeServer();
} catch (IOException e) {
log.error("閲婃斁FTP杩炴帴鏃跺彂鐢烮O娴佸紓甯?, e);
}
}
log.info("銆愭枃浠朵笂浼犳垚鍔燂紒銆?);
return true;
}
public void run() {
//鐢熸垚鏄ㄦ棩瀵硅处鏂囦欢
boolean isconnected = logonFTP();//鐧婚檰 FTP鏈嶅姟鍣?
if (isconnected) {
String filePath = generateDzFile();
//涓婁紶鏂囦欢
boolean isSuccess = upFile(filePath);
if (!isSuccess) {
log.error(filePath + "涓婁紶澶辫触");
}
} else {
log.error("鐧婚檰FTP鏈嶅姟鍣ㄥけ璐ワ紝IP:"+ftpIP+",鐧婚檰鍚?"+loginName+",鐧婚檰瀵嗙爜:"+loginpwd+"");
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值