這是偶第一次寫java程序實現公司要求的FTP數據同步
/*
*******************************************************************************************************
Filename: ftp
Author: leetsing(elove)
Create date:
Use: connect to FTP server
Modify date:
Copy right: Magisky Media Technology Co
*******************************************************************************************************
*/
//import cz
//import cz
import
import
import java
import java
import java
import java
import
import java
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 =
String pwd =
String RWFileDir =
//連接ftp服務器
ftp ft = new ftp();
nnect(RWFileDir
//下載文件
if (ft
try {
ft
}catch(IOException e)
{
System
}
}
//上傳文件
if (ft
String sdir = RWFileDir +
File fdir = new File(sdir);
String FileName =
for(int i=
ft
}
//System
//ft
}
//刪除subunsubfromsp目錄下已經上傳的文件文件
// ft
//斷開服務器連接
ft
}
public FtpClient connect(String RWFileDir
{
this
System
try{
aftp = new FtpClient(hostname
aftp
aftp
//aftp
a =
System
}
catch(FtpLoginException e){
a=
System
//return false;
}
catch (IOException e){
a=
System
//return false;
}
catch(SecurityException e)
{
a=
System
//return false;
}
log(RWFileDir
return aftp;
}
public void stop(String RWFileDir)
{
String message =
try {
if(aftp!=null){
aftp
message =
System
log(RWFileDir
}
}
catch(IOException e)
{
message =
System
log(RWFileDir
}
}
public boolean downloadFile(String RWFileDir
boolean result=true;
String message =
if (aftp != null)
{
System
String badfile = filepathname
String badlog = filepathname
String baddir =
if ((pareTo(
baddir =
}
else{
baddir =
}
String strdir =
//System
try{
//FtpClient fc=new FtpClient(
//fc
int ch;
File fi = new File(RWFileDir + baddir + filepathname);
//aftp
RandomAccessFile getFile = new RandomAccessFile(fi
getFile
TelnetInputStream fget=aftp
DataInputStream puts = new DataInputStream(fget);
while ((ch = puts
getFile
}
//s
fget
getFile
//fc
message =
System
log(RWFileDir
}
catch(IOException e){
message =
System
log(RWFileDir
result = false ;
}
}
else{
result = false;
}
return result;
}
public boolean uploadFile(String RWFileDir
boolean result=true;
String message =
if (aftp != null)
{
System
try{
String fg =new String(
int index = filepathname
String filename = filepathname
File localFile = new File(filepathname) ;
RandomAccessFile sendFile = new RandomAccessFile(filepathname
//
sendFile
//改名上傳temp_
filename = filename
outs = aftp
outputs = new DataOutputStream(outs);
while (sendFile
{
ch = sendFile
outputs
}
rename(filename
outs
sendFile
message =
System
log(RWFileDir
}
catch(IOException e){
message =
System
log(RWFileDir
result = false ;
}
}
else{
result = false;
}
return result;
}
public void rename(String oldName
//aftp
File Old = new File(oldName); //oldName
File New = new File(newName); //newName
//aftp
//boolean Old
//System
//System
}
public static void deleFile(String RWFileDir) {
//try {
//取得ReadFile目錄下的txt文件
String sdir = RWFileDir +
File fdir = new File(sdir);
String FileName =
int j = fdir
System
File file;
for(int i=
//刪除subunsubfromsp中的txt文件
FileName = RWFileDir +
file = new File(FileName);
file
System
}
//}
//catch (IOException e) {
// System
// e
//}
}
public void showFileContents(String strdir)
{
StringBuffer buf = new StringBuffer();
try {
aftp
ins= aftp
while ((ch=ins
buf
}
System
ins
}
catch(IOException e)
{
}
}
// 返回當前目錄的所有文件及文件夾
public ArrayList getFileList() throws IOException {
BufferedReader dr = new BufferedReader(new InputStreamReader(aftp
ArrayList al = new ArrayList();
String s =
while ( (s = dr
al
}
return al;
}
public void setPath(String path) throws IOException {
if (aftp == null)
this
else {
aftp
}
}
// 返回當前目錄的文件名稱
public ArrayList getNameList(String RWFileDir) throws IOException {
BufferedReader dr = new BufferedReader(new InputStreamReader(aftp
ArrayList al = new ArrayList();
String s =
while ( (s = dr
al
s = s
isFile(s);
downloadFile(RWFileDir
//String strFileDelF = aftp
File fileDelF=new File(s);
fileDelF
}
return al;
//System
}
// 判斷一行文件信息是否為目錄
public boolean isDir(String line) {
return ( (String) parseLine(line)
}
public boolean isFile(String line) {
return!isDir(line);
}
// 處理getFileList取得的行信息
private ArrayList parseLine(String line) {
ArrayList s
StringTokenizer st = new StringTokenizer(line
while (st
s
}
return s
}
//寫消息日志
public static void log(String RWFileDir
{
String message =
try {
java
java
java
String datestr = df
String datelog = dflog
//String datelog = datestr
//datelog = datelog
//按日期每天生成一個日志文件
FileWriter fwl = new FileWriter(RWFileDir +
PrintWriter outl = new PrintWriter(fwl);
outl
outl
fwl
}catch (IOException e) {
message =
e
log(RWFileDir
System
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26458.html