这是偶第一次写java程序实现公司要求的FTP数据同步所有原代码如下以供各位参考并提出宝贵意见!另外俺还有两个功能没有实现有请高手指点一二如何删除FTP服务器上已经下载过的文件?如何将上传的文件改名?上传时在文件名前加temp_上传完毕后需要把文件名改回来如何使用openPassiveDataConnection()方法将ftp连接更改为主动连接因为要从unix系统配置的ftp服务器下载文件的话就需要主动连接
/*
*******************************************************************************************************
Filename: ftpjava
Author: leetsing(elove)
Create date:
Use: connect to FTP serverthen upload and download file
Modify date: add to upload file
add to download file
Copy right: Magisky Media Technology CoLtd
*******************************************************************************************************
*/
//import czdhlio*;
//import czdhlftp*;
import ftp*;
import *;
import javaapplet*;
import javaio*;
import javaioIOException;
import javautilStringTokenizer;
import ftpFtpClient;
import javautilArrayList;
public class ftp extends Applet
{
FtpClient aftp;
DataOutputStream outputs ;
TelnetInputStream ins;
TelnetOutputStream outs;
int ch;
public String a;
String hostname=;
private String path = /;
public static void main(String[] args)
{
String hostname = ;
int port = ;
String uid = lee;
String pwd = lee;
String RWFileDir = D:\\smsftp\\;//文件目录
//连接ftp服务器
ftp ft = new ftp();
nnect(RWFileDirhostnameportuidpwd);
//下载文件
if (ftaftp != null){
try {
ftgetNameList(RWFileDir);
}catch(IOException e)
{
Systemoutprintln(下载文件出错+e);
}
}
//上传文件
if (ftaftp != null){
String sdir = RWFileDir + subunsubfromsp\\;
File fdir = new File(sdir);
String FileName = ;
for(int i=;i FileName = sdir + (fdirlist());
ftuploadFile(RWFileDirFileName);
}
//Systemoutprintln(成功上传的文件);
//ftshowFileContents(subunsubfromsp\\);
}
//删除subunsubfromsp目录下已经上传的文件文件
// ftdeleFile(RWFileDir);
//断开服务器连接
ftstop(RWFileDir);
}
public FtpClient connect(String RWFileDirString hostnameint portString uidString pwd)
{
thishostname = hostname;
Systemoutprintln(正在连接+hostname+请等待);
try{
aftp = new FtpClient(hostnameport);
aftplogin(uidpwd);
aftpbinary();
//aftpopenPortDataConnection();
a = 连接主机:+hostname+成功!;
Systemoutprintln(a);
}
catch(FtpLoginException e){
a=登陆主机:+hostname+失败!请检查用户名或密码是否正确+e;
Systemoutprintln(a);
//return false;
}
catch (IOException e){
a=连接主机:+hostname+失败!请检查端口是否正确+e;
Systemoutprintln(a);
//return false;
}
catch(SecurityException e)
{
a=无权限与主机:+hostname+连接!请检查是否有访问权限+e;
Systemoutprintln(a);
//return false;
}
log(RWFileDira);
return aftp;
}
public void stop(String RWFileDir)
{
String message = ;
try {
if(aftp!=null){
aftpcloseServer();
message = 与主机+hostname+连接已断开!;
Systemoutprintln(message);
log(RWFileDirmessage);
}
}
catch(IOException e)
{
message = 与主机+hostname+断开连接失败!+e;
Systemoutprintln(message);
log(RWFileDirmessage);
}
}
public boolean downloadFile(String RWFileDirString filepathname){
boolean result=true;
String message = ;
if (aftp != null)
{
Systemoutprintln(正在下载文件+filepathname+请等待);
String badfile = filepathnamesubstring(filepathnamelength()filepathnamelength());
String badlog = filepathnamesubstring(filepathnamelength()filepathnamelength());
String baddir = ;
if ((pareTo(bad) != ) && (pareTo(badlog) != )){
baddir = subunsubtosp\\;
}
else{
baddir = bad\\;
}
String strdir = subunsubtosp\\;
//Systemoutprintln(RWFileDir + baddir + filepathname);
try{
//FtpClient fc=new FtpClient();
//fclogin(leelee);
int ch;
File fi = new File(RWFileDir + baddir + filepathname);
//aftpcd(strdir);
RandomAccessFile getFile = new RandomAccessFile(firw);
getFileseek();
TelnetInputStream fget=aftpget(strdir+filepathname);
DataInputStream puts = new DataInputStream(fget);
while ((ch = putsread()) >= ) {
getFilewrite(ch);
}
//sdelete();
fgetclose();
getFileclose();
//fccloseServer();
message = 下载+filepathname+文件到+baddir +目录成功!;
Systemoutprintln(message);
log(RWFileDirmessage);
}
catch(IOException e){
message = 下载+filepathname+文件到+baddir +目录失败!+e;
Systemoutprintln(message);
log(RWFileDirmessage);
result = false ;
}
}
else{
result = false;
}
return result;
}
public boolean uploadFile(String RWFileDirString filepathname){
boolean result=true;
String message = ;
if (aftp != null)
{
Systemoutprintln(正在上传文件+filepathname+请等待);
try{
String fg =new String(\\subunsubfromsp\\);
int index = filepathnamelastIndexOf(fg);
String filename = filepathnamesubstring(index+);
File localFile = new File(filepathname) ;
RandomAccessFile sendFile = new RandomAccessFile(filepathnamer);
//
sendFileseek();
//改名上传temp_
filename = filenamesubstring()+temp_+filenamesubstring(filenamelength());
outs = aftpput(filename);
outputs = new DataOutputStream(outs);
while (sendFilegetFilePointer() < sendFilelength() )
{
ch = sendFileread();
outputswrite(ch);
}
rename(filenamesubstring(filenamelength())filenamesubstring(filenamelength()));
outsclose();
sendFileclose();
message = 上传+filepathname+文件成功!;
Systemoutprintln(message);
log(RWFileDirmessage);
}
catch(IOException e){
message = 上传+filepathname+文件失败!+e;
Systemoutprintln(message);
log(RWFileDirmessage);
result = false ;
}
}
else{
result = false;
}
return result;
}
public void rename(String oldNameString newName){
//aftprenameTo(oldNamenewName);
File Old = new File(oldName); //oldName
File New = new File(newName); //newName
//aftprenameTo(New);
//boolean OldrenameTo(File newName);
//Systemoutprintln(Old);
//Systemoutprintln(New);
}
public static void deleFile(String RWFileDir) {
//try {
//取得ReadFile目录下的txt文件
String sdir = RWFileDir + subunsubfromsp\\;
File fdir = new File(sdir);
String FileName = ;
int j = fdirlist()length;
Systemoutprintln(sdir+目录下要删除的文件数+fdirlist()length);
File file;
for(int i=;i {
//删除subunsubfromsp中的txt文件
FileName = RWFileDir + subunsubfromsp\\ + (fdirlist())[];
file = new File(FileName);
filedelete();
Systemoutprintln(已经成功删除+FileName+文件!);
}
//}
//catch (IOException e) {
// Systemoutprintln(删除txt文件错误!);
// eprintStackTrace();
//}
}
public void showFileContents(String strdir)
{
StringBuffer buf = new StringBuffer();
try {
aftpcd(strdir);
ins= aftplist();
while ((ch=insread())>=){
bufappend((char)ch);
}
Systemoutprintln(buftoString());
insclose();
}
catch(IOException e)
{
}
}
// 返回当前目录的所有文件及文件夹
public ArrayList getFileList() throws IOException {
BufferedReader dr = new BufferedReader(new InputStreamReader(aftplist()));
ArrayList al = new ArrayList();
String s = ;
while ( (s = drreadLine()) != null) {
aladd(s);
}
return al;
}
public void setPath(String path) throws IOException {
if (aftp == null)
thispath = path;
else {
aftpcd(path);
}
}
// 返回当前目录的文件名称
public ArrayList getNameList(String RWFileDir) throws IOException {
BufferedReader dr = new BufferedReader(new InputStreamReader(aftpnameList(subunsubtosp\\)));
ArrayList al = new ArrayList();
String s = ;
while ( (s = drreadLine()) != null) {
aladd(s);
s = ssubstring(slength());
isFile(s);
downloadFile(RWFileDirs);
//String strFileDelF = aftpnameList(subunsubtosp\\);
File fileDelF=new File(s);
fileDelFdelete();
}
return al;
//Systemoutprintln(aladd(s));
}
// 判断一行文件信息是否为目录
public boolean isDir(String line) {
return ( (String) parseLine(line)get())indexOf(d) != ;
}
public boolean isFile(String line) {
return!isDir(line);
}
// 处理getFileList取得的行信息
private ArrayList parseLine(String line) {
ArrayList s = new ArrayList();
StringTokenizer st = new StringTokenizer(line );
while (sthasMoreTokens()) {
sadd(stnextToken());
}
return s;
}
//写消息日志
public static void log(String RWFileDirString msg)
{
String message = ;
try {
javatextDateFormat df = new javatextSimpleDateFormat(yyyyMMdd HH:mm:ss);
javatextDateFormat dflog = new javatextSimpleDateFormat(yyyyMMdd);
javautilDate date = new javautilDate() ;
String datestr = dfformat(new javautilDate()) ;
String datelog = dflogformat(new javautilDate()) ;
//String datelog = datestrsubstring();
//datelog = datelogreplace( );
//按日期每天生成一个日志文件
FileWriter fwl = new FileWriter(RWFileDir + CMSSftp+datelog+logtrue);
PrintWriter outl = new PrintWriter(fwl);
outlprintln(datestr + + msg);
outlclose();
fwlclose();
}catch (IOException e) {
message = 写log文件错误!+e;
eprintStackTrace();
log(RWFileDirmessage);
Systemoutprintln(message);
}
}
}