FTP簡(jiǎn)述
FTP 是File Transfer Protocol(文件傳輸協(xié)議)的英文簡(jiǎn)稱,而中文簡(jiǎn)稱為“文傳協(xié)議”。用于Internet上的控制文件的雙向傳輸。同時(shí),它也是一個(gè)應(yīng)用程序(Application)。基于不同的操作系統(tǒng)有不同的FTP應(yīng)用程序,而所有這些應(yīng)用程序都遵守同一種協(xié)議以傳輸文件。它需要依賴于FTP服務(wù)器(支持FTP協(xié)議的服務(wù)器就是FTP服務(wù)器)。其傳輸方式有兩種:ASCII傳輸方式、二進(jìn)制傳輸模式。
安裝
在網(wǎng)上查了查,F(xiàn)TP的安裝都是用命令安裝,這就要求你的服務(wù)器有網(wǎng)絡(luò)。如果是個(gè)人虛擬機(jī)沒(méi)有網(wǎng),可以修改網(wǎng)絡(luò)模式為net模式,ip改為自動(dòng)模式(Automatic)即可
1、確認(rèn)是否安裝
pgrep vsftpd

2、安裝
yum install vsftpd

3、啟動(dòng) 停止 重啟 卸載
systemctl start vsftpd.service#啟動(dòng)
systemctl status vsftpd.service#重啟
systemctl stop vsftpd.service#停止
/sbin/service vsftpd start
/sbin/service vsftpd restart
/sbin/service vsftpd stop
rpm -e vsftpd# 卸載

4、關(guān)閉防火墻
service iptables stop
5、修改配置
在修改配置之前,我們先看看ftp有哪些配置,ftp的配置在/etc/vsftpd/下

vsftpd的配置,配置文件中限定了vsftpd用戶連接控制配置。
(1):vsftpd.ftpusers:位于/etc/vsftpd目錄下。它指定了哪些用戶賬戶不能訪問(wèn)FTP服務(wù)器,例如root等。
(2):vsftpd.user_list:位于/etc/vsftpd目錄下。該文件里的用戶賬戶在默認(rèn)情況下也不能訪問(wèn)FTP服務(wù)器,僅當(dāng)vsftpd .conf配置文件里啟用userlist_enable=NO選項(xiàng)時(shí)才允許訪問(wèn)。
(3):vsftpd.conf:位于/etc/vsftpd目錄下。來(lái)自定義用戶登錄控制、用戶權(quán)限控制、超時(shí)設(shè)置、服務(wù)器功能選項(xiàng)、服務(wù)器性能選項(xiàng)、服務(wù)器響應(yīng)消息等FTP服務(wù)器的配置。
anonymous_enable=NO #禁止匿名
local_enable=YES #允許本地登錄
write_enable=YES #允許寫(xiě),如需上傳,則必須
llocal_umask=027 #將上傳文件的權(quán)限設(shè)置為:777-local_umask
anon_upload_enable=YES #允許虛擬用戶和匿名用戶上傳
anon_other_write_enable=YES #允許虛擬用戶和匿名用戶修改文件名和刪除文件
dirmessage_enable=YES
xferlog_enable=YES #打開(kāi)日志記錄
connect_from_port_20=YES
xferlog_file=/var/log/vsftpd.log #日志存放位置
xferlog_std_format=YES #標(biāo)準(zhǔn)日志格式
idle_session_timeout=600 #空閑連接超時(shí)
data_connection_timeout=120
ftpd_banner=Welcome to ChinaRise FTP service #歡迎信息
chroot_local_user=NO
chroot_list_enable=YES
#以上兩行將虛擬用戶限制在其目錄下,不能訪問(wèn)其他目錄,或者直接用
chroot_local_user=YES
listen=yes #監(jiān)聽(tīng)/被動(dòng)模式
listen_port=21 #監(jiān)聽(tīng)端口
chroot_list_file=/etc/vsftpd/vsftpd.chroot_list #虛擬用戶名單保存在文件/etc/vsftpd/vsftpd.chroot_list 中
user_config_dir=/etc/vsftpd/vsftpd_user_conf #每個(gè)虛擬用戶名的更加詳細(xì)的培植保存在/etc/vsftpd/vsftpd_user_conf 中
注意
這里ftpusers和user_list的限制用戶列表,通常我們自己本地測(cè)試時(shí)都用root登陸,所以切記注掉這兩個(gè)文件中的root(LZ在這被坑了許久)
6、設(shè)置selinux
vim /etc/selinux/config
#SELINUX=enforcing #注釋掉
#SELINUXTYPE=targeted #注釋掉
SELINUX=disabled #增加

7、關(guān)閉selinux
setenforce 0
8、用winSPC鏈接

這里注意,ftp默認(rèn)有兩個(gè)端口20和21,20端口為數(shù)據(jù)傳輸端口,21端口為鏈接控制端口
java操作ftp
準(zhǔn)備jar包
commons-net-1.4.1.jar
jakarta-oro-2.0.8.jar
log4j-1.2.17.jar
FtpInfo.java
/**
* ClassName: FtpInfo
* @author lvfang
* @Desc: TODO
* @date 2017-9-27
*/
public class FtpInfo {
//主機(jī)ip、端口
private String host;
private Integer port;
//用戶名、密碼
private String username;
private String password;
//基礎(chǔ)路徑、 文件路徑、 文件名
private String basePath;
private String filePath;
private String filename;
private String localPath;//下載后保存到本地的路徑
public FtpInfo(){};
//連接對(duì)應(yīng)構(gòu)造函數(shù)
public FtpInfo(String host,Integer port,String username,String password){
this.host = host;
this.port = port;
this.username = username;
this.password = password;
};
//上傳文件對(duì)應(yīng)構(gòu)造函數(shù)
public FtpInfo(String host,Integer port,String username,String password,String basePath,String filePath,String filename){
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.basePath = basePath;
this.filePath = filePath;
this.filename = filename;
};
//下載文件對(duì)應(yīng)構(gòu)造函數(shù)
public FtpInfo(String host,Integer port,String username,String password,String basePath,String filePath,String filename,String localPath){
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.basePath = basePath;
this.filePath = filePath;
this.filename = filename;
this.localPath = localPath;
};
//getter setter方法
}
FtpUtil.java
/**
* ClassName: FtpUtil
* @author lvfang
* @Desc: TODO
* @date 2017-9-28
*/
public class FtpUtil {
private static FTPClient ftp;
private static FtpInfo ftpInfo = null;
/**
* Description: 上傳文件
*
* @param f.host
* FTP服務(wù)器hostname
* @param f.port
* FTP服務(wù)器端口
* @param f.username
* FTP登錄賬號(hào)
* @param f.password
* FTP登錄密碼
* @param f.basePath
* FTP服務(wù)器基礎(chǔ)目錄
* @param f.filePath
* FTP服務(wù)器文件存放路徑。例如分日期存放:/2017/09/28。文件的路徑為basePath+filePath
* @param f.filename
* 上傳到FTP服務(wù)器上的文件名
* @param input
* 輸入流
* @return 成功返回true,否則返回false
*/
public static boolean uploadFile(FtpInfo ftpInfo, InputStream input) {
boolean result = false;
ftp = new FTPClient();
try {
int reply;
ftp.connect(ftpInfo.getHost(), ftpInfo.getPort());// 連接FTP服務(wù)器
// 如果采用默認(rèn)端口,可以使用ftp.connect(host)的方式直接連接FTP服務(wù)器
ftp.login(ftpInfo.getUsername(), ftpInfo.getPassword());// 登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
// 切換到上傳目錄
if (!ftp.changeWorkingDirectory(ftpInfo.getBasePath()
+ ftpInfo.getFilePath())) {
// 如果目錄不存在創(chuàng)建目錄
String[] dirs = ftpInfo.getFilePath().split("/");
String tempPath = ftpInfo.getBasePath();
for (String dir : dirs) {
if (null == dir || "".equals(dir))
continue;
tempPath += "/" + dir;
if (!ftp.changeWorkingDirectory(tempPath)) {
if (!ftp.makeDirectory(tempPath)) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
// 設(shè)置上傳文件的類型為二進(jìn)制類型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();//不加這一次會(huì)出現(xiàn)無(wú)法上傳
// 上傳文件
if (!ftp.storeFile(ftpInfo.getFilename(), input)) {
return result;
}
input.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
/**
* Description: 下載文件
*
* @param f.host
* FTP服務(wù)器hostname
* @param f.port
* FTP服務(wù)器端口
* @param f.username
* FTP登錄賬號(hào)
* @param f.password
* FTP登錄密碼
* @param f.basePath
* 基礎(chǔ)路徑
* @param f.filePath
* 文件路徑
* @param fileName
* 要下載的文件名
* @param localPath
* 下載后保存到本地的路徑
* @return
*/
public static boolean downloadFile(FtpInfo ftpInfo) {
boolean result = false;
ftp = new FTPClient();
try {
int reply;
ftp.connect(ftpInfo.getHost(), ftpInfo.getPort());
// 如果采用默認(rèn)端口,可以使用ftp.connect(host)的方式直接連接FTP服務(wù)器
ftp.login(ftpInfo.getUsername(), ftpInfo.getPassword());// 登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(ftpInfo.getBasePath() + ftpInfo.getFilePath());// 轉(zhuǎn)移到FTP服務(wù)器目錄
ftp.setControlEncoding("GBK");
//ftp.enterLocalPassiveMode();//不加這一次會(huì)出現(xiàn)無(wú)法上傳
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(ftpInfo.getFilename())) {
File localFile = new File(ftpInfo.getLocalPath() + "/"+ ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
/**
* 測(cè)試連接
*
* @param f.host、f.port、f.username、f.password
* @return
* @throws Exception
*/
public static boolean connectFtp(FtpInfo ftpInfo) throws Exception {
ftp = new FTPClient();
boolean flag = false;
int reply;
if (ftpInfo.getPort() == null) {
ftp.connect(ftpInfo.getHost(), 21);
} else {
ftp.connect(ftpInfo.getHost(), ftpInfo.getPort());
}
ftp.login(ftpInfo.getUsername(), ftpInfo.getPassword());
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return flag;
}
// 轉(zhuǎn)入當(dāng)前目錄工作
// ftp.changeWorkingDirectory(ftpInfo.getBasePath()+ftpInfo.getFilePath());
flag = true;
return flag;
}
/**
* 關(guān)閉連接
*/
public static void closeFtp() {
if (ftp != null && ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
// 測(cè)試連接
// FtpInfo ftpInfo = new
// FtpInfo("192.168.**.***",21,"root","密碼");
// System.out.println(FtpUtil.connectFtp(ftpInfo));;
// 上傳測(cè)試
// FileInputStream in=new FileInputStream(new File("D:\\03.png"));
// ftpInfo = new
// FtpInfo("192.168.**.***",21,"root","密碼","/home/images","/2017/09/28","05.jpg");
// System.out.println(FtpUtil.uploadFile(ftpInfo, in));
// 下載
ftpInfo = new FtpInfo("192.168.**.***", 21, "root", "密碼","/home/images", "/2017/09/28", "05.jpg", "d:/");
System.out.println(FtpUtil.downloadFile(ftpInfo));
}
}

相關(guān)文章:
http://blog.csdn.net/wantaway314/article/details/52584531
http://www.cnblogs.com/zc123/p/6394470.html
http://www.cnblogs.com/huzi007/p/4236150.html