可以实现FTP的链接,获取文件列表,上传文件,下载文件,删除文件,新建文件夹。
FtpServer.java 内存如下:
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 关闭连接
* @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
{
//设置被动模式
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 上传文件绝对路径(如:C://tmp//test.txt)
* @param remoteFile 上传后在服务器上的文件名(如:test.txt)
* @return
*/
public boolean upload(String localFile, String remoteFile)
{
closeConnect();
connectServer(null, _path);
boolean flag = true;
try
{
// 设置以二进制方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 设置被动模式
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 下载在服务器上的文件名
* @param localFile 保存到本地文件绝对路径
* @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);
//检查远程文件是否存在
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 删除在服务器上的文件名
* @return
*/
public boolean delete(String remoteFile)
{
closeConnect();
connectServer(null, _path);
boolean flag = false;
try
{
ftpClient.enterLocalPassiveMode();
//检查远程文件是否存在
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;
}
/** *//**
* 创建目录
*
* @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 阅读(1848)
评论(0) 编辑 收藏 所属分类:
Java