package
pub.ftp;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/** */ /**
* FTP Client端调用模块
* 作者:
* 版本:1.0
* 时间:2007.07.30
*/
public class FTPClient ... {
/** *//**
* 类的初始化,建立ftp的连接,用户登录,指定ftp的传输流
* @param host
* @param port
* @param user
* @param psw
*/
// 建立一条与指定主机、指定端口上的FTP服务器的连接
private FtpClient aftp = new FtpClient(); ;
private DataOutputStream outputs ;
private TelnetOutputStream outs;
private TelnetInputStream inps;
public void FTPClass(String host,int port,String user,String psw)...{
try ...{
// 注册到FTP服务器
aftp.openServer(host);
System.out.println("登陆....");
aftp.login(user, psw);
System.out.println("登陆FTP服务器成功!");
aftp.binary();
} catch (IOException e) ...{
System.out.println("连接FTP服务器失败!");
e.printStackTrace();
}
}
/** *//**
* 通过ftp上传文件到服务器上
* @param localFile 本地所要上传的文件
* @param remoteFile 传到服务器上的文件名称
*/
public boolean upFile(String localFile,String remoteFile)...{
boolean result =true;
if(aftp != null)...{
System.out.println("正在上传文件"+localFile+",请等待....");
try...{
File file = new File(localFile);
outs = aftp.put(remoteFile);
FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) ...{
outs.write(bytes, 0, c);
}
outs.close();
in.close();
System.out.println("上传文件"+localFile+"成功!");
System.out.println("上传文件所在目录:"+remoteFile+"");
}catch(Exception e)...{
e.printStackTrace();
System.out.println("上传文件"+localFile+"失败!");
result = false;
}
}else...{
result = false;
}
return result;
}
/** *//**
* 下载FTP服务器上的文件
* @param localFile 本地文件名
* @param remoteFile 远程服务器文件名
*/
public boolean downFile(String remoteFile,String localFile)...{
boolean result =true;
if(aftp != null)...{
System.out.println("正在下载文件"+remoteFile+",请等待....");
try...{
File file = new File(remoteFile);
inps = aftp.get(remoteFile);
aftp.cd("D;/jiadong");
FileInputStream in = new FileInputStream(file);
// RandomAccessFile getFile = new RandomAccessFile(file,"rw");
// getFile.seek(0);
FileOutputStream os = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) ...{
// .write(bytes, 0, c);
os.write(bytes, 0, c);
}
inps.close();
in.close();
os.close();
System.out.println("下载文件"+remoteFile+"成功!");
System.out.println("上传文件所在目录:"+localFile+"");
}catch(Exception e)...{
e.printStackTrace();
System.out.println("下载文件"+remoteFile+"失败!");
result = false;
}
}
return false;
}
/** *//**
* 断开ftp连接
* @throws IOException
*
*/
public void disconnect() throws IOException...{
aftp.closeServer();
System.out.println("FTP服务器连接断开!");
}
// 返回当前目录的所有文件及文件夹
public ArrayList getFileList() throws IOException ...{
BufferedReader dr = new BufferedReader(new InputStreamReader(aftp.list()));
ArrayList al = new ArrayList();
String s = "";
while ( (s = dr.readLine()) != null) ...{
al.add(s);
}
return al;
}
public static void main(String[] args) throws IOException...{
FTPClient ftpClient = new FTPClient();
ftpClient.FTPClass("192.168.8.154", 21, "jiadong", "jiadong");
//ftpClient.upFile("F:/music/想念.mp3","/jia/hehe.mp3");
//ftpClient.upFile("d:/jiadong","D:/workspace/msgfield.xml");
ftpClient.downFile("hehe.xml", "/jia/ruru.xml");
ftpClient.disconnect();
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/** */ /**
* FTP Client端调用模块
* 作者:
* 版本:1.0
* 时间:2007.07.30
*/
public class FTPClient ... {
/** *//**
* 类的初始化,建立ftp的连接,用户登录,指定ftp的传输流
* @param host
* @param port
* @param user
* @param psw
*/
// 建立一条与指定主机、指定端口上的FTP服务器的连接
private FtpClient aftp = new FtpClient(); ;
private DataOutputStream outputs ;
private TelnetOutputStream outs;
private TelnetInputStream inps;
public void FTPClass(String host,int port,String user,String psw)...{
try ...{
// 注册到FTP服务器
aftp.openServer(host);
System.out.println("登陆....");
aftp.login(user, psw);
System.out.println("登陆FTP服务器成功!");
aftp.binary();
} catch (IOException e) ...{
System.out.println("连接FTP服务器失败!");
e.printStackTrace();
}
}
/** *//**
* 通过ftp上传文件到服务器上
* @param localFile 本地所要上传的文件
* @param remoteFile 传到服务器上的文件名称
*/
public boolean upFile(String localFile,String remoteFile)...{
boolean result =true;
if(aftp != null)...{
System.out.println("正在上传文件"+localFile+",请等待....");
try...{
File file = new File(localFile);
outs = aftp.put(remoteFile);
FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) ...{
outs.write(bytes, 0, c);
}
outs.close();
in.close();
System.out.println("上传文件"+localFile+"成功!");
System.out.println("上传文件所在目录:"+remoteFile+"");
}catch(Exception e)...{
e.printStackTrace();
System.out.println("上传文件"+localFile+"失败!");
result = false;
}
}else...{
result = false;
}
return result;
}
/** *//**
* 下载FTP服务器上的文件
* @param localFile 本地文件名
* @param remoteFile 远程服务器文件名
*/
public boolean downFile(String remoteFile,String localFile)...{
boolean result =true;
if(aftp != null)...{
System.out.println("正在下载文件"+remoteFile+",请等待....");
try...{
File file = new File(remoteFile);
inps = aftp.get(remoteFile);
aftp.cd("D;/jiadong");
FileInputStream in = new FileInputStream(file);
// RandomAccessFile getFile = new RandomAccessFile(file,"rw");
// getFile.seek(0);
FileOutputStream os = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) ...{
// .write(bytes, 0, c);
os.write(bytes, 0, c);
}
inps.close();
in.close();
os.close();
System.out.println("下载文件"+remoteFile+"成功!");
System.out.println("上传文件所在目录:"+localFile+"");
}catch(Exception e)...{
e.printStackTrace();
System.out.println("下载文件"+remoteFile+"失败!");
result = false;
}
}
return false;
}
/** *//**
* 断开ftp连接
* @throws IOException
*
*/
public void disconnect() throws IOException...{
aftp.closeServer();
System.out.println("FTP服务器连接断开!");
}
// 返回当前目录的所有文件及文件夹
public ArrayList getFileList() throws IOException ...{
BufferedReader dr = new BufferedReader(new InputStreamReader(aftp.list()));
ArrayList al = new ArrayList();
String s = "";
while ( (s = dr.readLine()) != null) ...{
al.add(s);
}
return al;
}
public static void main(String[] args) throws IOException...{
FTPClient ftpClient = new FTPClient();
ftpClient.FTPClass("192.168.8.154", 21, "jiadong", "jiadong");
//ftpClient.upFile("F:/music/想念.mp3","/jia/hehe.mp3");
//ftpClient.upFile("d:/jiadong","D:/workspace/msgfield.xml");
ftpClient.downFile("hehe.xml", "/jia/ruru.xml");
ftpClient.disconnect();
}
}