ftp复制文件 java_用java实现向ftp的文件传输

该博客介绍了一个Java类`FtpSender`,用于连接FTP服务器并实现文件上传、下载和删除操作。通过读取配置文件获取FTP服务器的连接信息,然后调用相应方法进行文件操作。
摘要由CSDN通过智能技术生成

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

{

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值