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();
}
}
}
}
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();
}
}
}
}
相关文章推荐
- linux iptables ip_conntrack: table full CentOS 7 iptables
- Linux命令之pstree - 以树状图显示进程间的关系
- linux 安装libpcap
- 在CentOS上编写init.d service script
- Linux启动telnet服务,Windows通过telnet远程登录linux服务器
- linux kvm虚拟机使用
- Linux glibc幽灵(GHOST)漏洞检测及修复方案
- Linux YUM或RPM修复 glibc幽灵(GHOST)漏洞
- vmware workstation中克隆(clone)CentOS 6的虚拟机网卡无法启动问题
- Centos快速安装vnc(tigervnc-server)
- Red Hat Enterprise Linux Server release 6.3 实现双网卡绑定
- Linux下配置多路径
- Linux职业发展方向
- 在Vbox上安装linux需要注意的问题 “异常退出错误。”或不识别鼠标的解决办法
- centos 6 安装virtualbox 闪退问题
- Linux 实时流量查看 iftop安装教程和一些参数和命令
- 如何 打包整合linux系统文件夹 用于刷机包等等, 其中包括打包 句号开头 . 开头的文件, 排除系统文件 等
- Linux下使用uuencode发送附件
- linux系统调用和库函数调用的区别
- Linux下chkconfig命令详解