package com.sample.ftpClient;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import com.baosight.dexpt.DefaultDispatcher;
public class FtpSender {
private FtpClient ftpClient;
private String ftpName="";
private String ftpPassword="";
private String ftpIp="";
private String ftpPath="";
private String localPath;
public void init()
{
Properties props=new Properties();
Class clazz=DefaultDispatcher.class;
ClassLoader clazzLoader=clazz.getClassLoader();
InputStream is=null;
is=clazzLoader.getResourceAsStream("dominoAttach.properties");
try{
props.load(is);
this.ftpIp=props.getProperty("ftp_ip");
this.ftpName = props.getProperty("ftp_name");
this.ftpPassword = props.getProperty("ftp_password");
this.ftpPath = props.getProperty("ftp_path");
this.localPath = props.getProperty("path");
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
if(is!=null)
{
try{
is.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
}
private void connectServer() throws IOException
{
ftpClient = new FTPClient();
ftpClient.connect(this.ftpIp);
boolean blogin = ftpClient.login(this.ftpName, this.ftpPassword);
if(!blogin)
{
System.out.println("连接失败");
ftpClient.disconnect();
ftpClient = null;
}
}
public void closeConnect(){
try{
if(ftpClient!=null){
ftpClient.logout();
ftpClient.disconnect();
}
}catch(Exception e){
e.printStackTrace();
}
}
private String upload(String filename) throws Exception
{
InputStream is = null;
try
{
is = new FileInputStream(new File(this.localPath+filename));
this.ftpClient.storeFile(this.ftpPath+"/"+filename, is);
} catch(Exception e)
{
e.printStackTrace();
}
return "file send success!";
}
public boolean sendFtpFile(String filename)
{
try
{
init();
connectServer();
String ret = upload(filename);
System.out.println("---------ret:"+ret);
closeConnect();
} catch (Exception e)
{
e.printStackTrace();
}
return true;
}
/*********
* 读取domino FTP里的附件,并插入db2到数据库中
*
*/
public void readDominoFtp(String fileguid)
{
FTPFile[] files = null;
String filename ="";
try
{
this.readinit();
this.connectServer();
FTPListParseEngine engine = this.ftpClient.initiateListParsing(this.ftpPath);
files = engine.getFiles();
//files = this.ftpClient.listFiles(this.ftpPath);
for(int i=0;i<files.length;i++)
{
FTPFile file = files[i];
//filename = iso8859togbk(file.getName());
filename = file.getName();
if(filename.indexOf(fileguid)>=0)
{
this.download(filename);
System.out.println("--filename:"+filename);
this.saveFileToDb(iso8859togbk(filename));
//this.deleteLocalFile();
this.ftpClient.deleteFile(this.ftpPath+"/"+iso8859togbk(filename));
}
}
//closeConnect();
} catch (Exception e)
{
e.printStackTrace();
}
}
private static String iso8859togbk(Object obj){
try{
if(obj==null)
return "";
else
return new String(obj.toString().getBytes("iso-8859-1"),"gbk");
}catch(Exception e){
return "";
}
}
public void download(String filename)
{
BufferedOutputStream fos = null;
try
{
File outFile = new File(this.localPath);
if(!outFile.exists()){
outFile.mkdirs();
}
fos = new BufferedOutputStream (new FileOutputStream(this.localPath+iso8859togbk(filename)));
this.ftpClient.retrieveFile(this.ftpPath+"/"+filename,fos);
}catch(Exception e)
{
e.printStackTrace();
}finally{
try{
if(fos!=null)
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}