可以實(shí)現(xiàn)FTP的鏈接,獲取文件列表,上傳文件,下載文件,刪除文件,新建文件夾。
FtpServer.java 內(nèi)存如下:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


public class FtpServer


{
private static String ip = ""; // IP地址

private static int port = ""; // 端口

private static String username = ""; // 用戶名

private static String password = ""; // 密碼

private FTPClient ftpClient = new FTPClient(); // FTP客戶端

private String _path = null;//FTP文件路徑

public FtpServer() {


}

public void setPath(String path)

{
_path = path;
}

/** *//**
* FTP 連接
* @param path 路徑
* @return
*/
public boolean connectServer(String type, String path)

{
setPath(path);
try

{
ftpClient.connect(ip, port);
//ftpClient.setControlEncoding("UTF-8");
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))

{
if (ftpClient.login(username, password))

{
if (!StringUtil.isBlank(path))

{
createPath(path);
}
return true;
}
}
closeConnect();
}
catch (SocketException e)

{
return false;
}
catch (IOException e)

{

try
{
return false;
}
return false;
}


/** *//**
* FTP 關(guān)閉連接
* @return
*/
public void closeConnect()

{
try

{
if (ftpClient.isConnected())

{
ftpClient.disconnect();
}
}
catch (IOException e)

{

try
{
e.printStackTrace();

} catch (Exception e1)
{
}
}
}


/** *//**
* 獲取文件列表
* @return
*/
public List<String> getFileNameList()

{
List<String> list = new ArrayList<String>();
try

{
//設(shè)置被動(dòng)模式
ftpClient.enterLocalPassiveMode();
String[] strs = ftpClient.listNames();
if (!StringUtil.isBlank(strs))

{
for (int i = 0; i < strs.length; i++)

{
list.add(strs[i]);
}
return list;
}
}
catch (IOException e)

{

try
{
return null;
}
return null;
}


/** *//**
* 上傳文件
* @param localFile 上傳文件絕對(duì)路徑(如:C://tmp//test.txt)
* @param remoteFile 上傳后在服務(wù)器上的文件名(如:test.txt)
* @return
*/
public boolean upload(String localFile, String remoteFile)

{
closeConnect();
connectServer(null, _path);
boolean flag = true;
try

{
// 設(shè)置以二進(jìn)制方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 設(shè)置被動(dòng)模式
ftpClient.enterLocalPassiveMode();
// 文件流傳輸模式
ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
InputStream fis = new FileInputStream(localFile);
flag = ftpClient.storeFile(remoteFile, fis);
fis.close();
}
catch (IOException e)

{
return false;
}
return flag;
}


/** *//**
* 下載文件
* @param remoteFile 下載在服務(wù)器上的文件名
* @param localFile 保存到本地文件絕對(duì)路徑
* @return
*/
public boolean download(String remoteFile, String localFile)

{
closeConnect();
connectServer(null, _path);
boolean flag = true;
try

{
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
//檢查遠(yuǎn)程文件是否存在
FTPFile[] files = ftpClient.listFiles(remoteFile);
if (files.length != 1)

{
return false;
}

OutputStream fos = new FileOutputStream(localFile);
flag = ftpClient.retrieveFile(remoteFile, fos);
fos.close();
}
catch (IOException e)

{
return false;
}
return flag;
}


/** *//**
* 刪除文件
* @param remoteFile 刪除在服務(wù)器上的文件名
* @return
*/
public boolean delete(String
remoteFile)

{
closeConnect();
connectServer(null, _path);
boolean flag = false;
try

{
ftpClient.enterLocalPassiveMode();
//檢查遠(yuǎn)程文件是否存在
for (int i = 0; i < remoteFile.length; i++)

{
FTPFile[] file = ftpClient.listFiles(remoteFile[i]);
if (file.length != 1)

{
return flag;
}
flag = ftpClient.deleteFile(remoteFile[i]);
}
}
catch (IOException e)

{
return false;
}
return flag;
}


/** *//**
* 創(chuàng)建目錄
*
* @param remote
* @return
* @throws IOException
*/
private boolean createPath(String remote) throws IOException

{
String[] rms = remote.split("/");
for (int i = 0; i < rms.length; i++)

{
if (!ftpClient.changeWorkingDirectory(rms[i]))

{
if (ftpClient.makeDirectory(rms[i]))

{
ftpClient.changeWorkingDirectory(rms[i]);
}
}
}
return true;
}

}


posted on 2011-04-19 10:48
Jarry 閱讀(1859)
評(píng)論(0) 編輯 收藏 所屬分類:
Java