package test;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import pub.utility.InitConfig;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/**
* Ftp Client端程序
* 功能:实现基本的文件上传,下载
* @author 贾栋
* 版本 1.0
* 时间 2007.08.02
*/
public class SimpleFtpClient {
/**
* @param args
*/
private FtpClient ftp = new FtpClient();
private TelnetInputStream tin ;
private TelnetOutputStream tou ;
private String str ;
public void connectFtpServer(String host,int port,String userName,String password) throws IOException{
try {
System.out.println("连接Ftp服务器");
ftp.openServer(host, port);
System.out.println("验证用户");
//ftp.binary();
} catch (IOException e) {
System.out.println("连接Ftp服务器失败!");
this.disConnect();
}
try {
ftp.login(userName, password);
System.out.println("登陆Ftp服务器成功");
ftp.binary();
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.println("用户名/密码错误");
this.disConnect();
}
}
public void disConnect() throws IOException{
System.out.println("Ftp服务器关闭!");
ftp.closeServer();
}
public void upFile(String localFile,String ftpServerFile) throws Exception {
if (ftp != null) {
System.out.println("正在上传文件"+localFile+",请等待.");
File file = new File(localFile);
if(!file.exists()){
System.out.println("错误的文件路径.");
}
try {
StringBuffer sb = new StringBuffer(localFile);
int n = sb.lastIndexOf("\\");
StringBuffer ftpName = sb.delete(0, n+1);
String ftpNew = new String(ftpName);
tou = ftp.put(ftpNew);
FileInputStream fin = new FileInputStream(file);
byte[] b = new byte[1024];
int i = 0;
while ((i = fin.read(b)) != -1) {
tou.write(b, 0, i);
}
fin.close();
tou.close();
System.out.println("上传文件:"+ftpNew+"成功!");
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.println("上传文件" + localFile + "错误!");
e.printStackTrace();
this.disConnect();
}
} else {
System.out.println("");
}
}
public void downFile(String ftpServerFile,String localFilePath){
if (ftp != null) {
System.out.println("正在下载文件"+ftpServerFile+",请等待.");
try {
tin = ftp.get(ftpServerFile);
DataInputStream din = new DataInputStream(tin);
File file = new File(localFilePath + ftpServerFile);
file.createNewFile();
FileOutputStream fot = new FileOutputStream(file);
byte[] b = new byte[1024];
int i = 0;
while ((i = din.read(b)) != -1) {
fot.write(b, 0, i);
}
tin.close();
din.close();
fot.close();
System.out.println("下载文件"+ftpServerFile+"成功!");
System.out.println("下载文件所在目录:"+localFilePath+"");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public void ftpServerAllFileName() throws IOException{
// tin = ftp.list();
// System.out.println(tin);
File file = new File(".");
String[] str = file.list();
for(int i=0;i<file.list().length;i++)
System.out.println("FilePath"+""+ str[i]);
}
public static void main(String[] args) throws Exception {
// TODO 自动生成方法存根
SimpleFtpClient sf = new SimpleFtpClient();
sf.connectFtpServer("192.168.8.154", 21, "ftpuser", "ftpuser");
// sf.upFile("F:\\", "11oracle 9i中文版基础培训教程.rar");
//sf.downFile("\\VIP强化视频\\J2EE 第一期 Hibernate专题\\[VIP][强化]J2EE第一期 Hibernate专题 第二讲 Configuration、SessionFactory、Session.rar", "D:\\jiadong\\jia\\");
/// sf.ftpServerAllFileName();
//sf.ftpServerAllFileName();
//sf.downFile("xiangnian.mp3", "d://");
//sf.upFile("F:\\教程\\oracle 9i中文版基础培训教程.rar", "test1.rar");
sf.disConnect();
}
}
posted on 2007-08-31 12:21
jiadong 阅读(398)
评论(0) 编辑 收藏 所属分类:
OTHERS