您的位置:首页 > 运维架构 > Linux

Linux 下ftp上传和下载文件

2016-04-13 10:31 239 查看
使用

FTPClient

/**

连接ftp

*/

public class FtpRemote{

FTPClient ftpClient;

public void connect(){

ftpClient= new FTPClient();

ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器

ftpClient.login(ftpUserName,
ftpPassword);// 登陆FTP服务器

ftpClient .setType(FTPTransferType.BINARY);//文件传输格式

ftpClient.setConnectMode(connectMode==0?
FTPConnectMode.PASV : FTPConnectMode.ACTIVE);//连接模式

}

/**

上传文件到ftp

localFilePath 为本地文件全路径

remoteFileName 为上传到服务器后的文件名称

remotePath 为上传文件的ftp路径

*/

public boolean putFile(String localFilePath, String remoteFileName, String remotePath){

try

{

connect();

ftpClient.chdir(remotePath);///进入到远程文件夹之中

ftpClient.put(localFileName, remoteFileName);

return true;

}

catch (IOException e)

{

e.printStackTrace();

return false;

}

catch (FTPException e)

{

e.printStackTrace();

return false;

}

finally{

if (ftpClient!= null) {

ftpClient.quit();

}

}

}

/**

下载文件到本地

localFilePath 为本地文件全路径

remoteFileName 为ftp服务器的文件名称

remotePath 为下载文件的ftp路径

*/

public boolean getFile(String localFilePath, String remotePath, String remoteFileName){

try

{

connect();

ftpClient.chdir(remotePath);//进入到远程文件夹之中

ftpClient.get(localFilePath, remoteFileName);

return true;

}

catch (IOException e)

{

e.printStackTrace();

return false;

}

catch (FTPException e)

{

e.printStackTrace();

return false;

}

finally{

if (ftpClient!= null) {

ftpClient.quit();

}

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: