Java commons-net FTP服务器文件操作
连接FTP服务器
package ftp;
import org.apache.commons.net.ftp.FTPClient;
public class ftpConnect {
private static final String ftp_server="XXX:XXX:XXX:XXX";
private static final int ftp_port=21;
private static final String ftp_user="sc";
private static final String ftp_passwd="123";
private static final int timeout=5000;
private static final String enconding="utf-8";
public static FTPClient getFTPClient() throws Exception{
FTPClient client=new FTPClient();
client.connect(ftp_server,ftp_port);
client.login(ftp_user,ftp_passwd);
client.setConnectTimeout(timeout);
client.setControlEncoding(enconding);
System.out.println("【FTP连接】状态码"+client.getReplyCode());
return client;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
package ftp;
import org.apache.commons.net.ftp.FTPClient;
public class ftpTest {
public static void main(String[] args) throws Exception{
FTPClient client=ftpConnect.getFTPClient();
System.out.println(client.abort());
client.logout();
}
}
上传文件
package ftp;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class ftpUploadFile {
public static void main(String[] args) throws Exception {
String ftpPath = "/var/ftp/yootkk/";
FTPClient client = ftpConnect.getFTPClient();
client.enterLocalPassiveMode();
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setBufferSize(2048);
if (!client.changeWorkingDirectory(ftpPath)) {
client.makeDirectory(ftpPath);
}
File file = new File("e:"+File.separator+"phto.png");
InputStream is = new FileInputStream(file);
String tempName=ftpPath+file.getName();
String ftpFileName=new String(tempName.getBytes("utf-8"),"ISO-8859-1");
if(client.storeFile(ftpFileName,is)){
System.out.println("FTP文件上传成功!");
}else {
System.out.println("FTP文件上传失败!");
}
System.out.println("【FTP连接】状态码"+client.getReplyCode());
is.close();
client.logout();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
下载文件
package ftp;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class ftpDownloadFile {
public static void main(String[] args) throws Exception {
String ftpPath="/var/ftp/yootk/";
String fileName="phtoee.png";
FTPClient client=ftpConnect.getFTPClient();
client.enterLocalPassiveMode();
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setBufferSize(2048);
if(client.changeWorkingDirectory(ftpPath)){
FTPFile[] files=client.listFiles();
for(FTPFile file:files){
System.out.println(file.getName());
if(file.getName().equalsIgnoreCase(fileName)){
File downFile=new File("e:"+ File.separator+"download_phtoee.png");
OutputStream os=new FileOutputStream(downFile);
boolean flag=client.retrieveFile(new String(file.getName().getBytes("utf-8"),"ISO-8859-1"),os);
os.flush();
os.close();
System.out.println(flag?"文件下载成功!":"文件下载失败!");
break;
}
}
}
client.logout();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
删除文件
package ftp;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class ftpDeleteFile {
public static void main(String[] args) throws Exception {
String filePath="/var/ftp/yootk/";
String fileName="phtoee.png";
FTPClient client=ftpConnect.getFTPClient();
client.enterLocalPassiveMode();
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setBufferSize(2048);
if(client.changeWorkingDirectory(filePath)){
FTPFile[] files=client.listFiles();
for(FTPFile file:files){
if(file.getName().equals(fileName)){
System.out.println(file.getName());
boolean flag=client.deleteFile(new String(file.getName().getBytes("utf-8"),"ISO-8859-1"));
System.out.println(flag?"文件删除成功!":"文件删除失败!");
break;
}
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
移动文件
package ftp;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class ftpMoveFile {
public static void main(String[] args) throws Exception {
String oldPath="/var/ftp/yootk/";
String newPath="/var/ftp/cjmPig/";
String fileName="phto.png";
FTPClient client=ftpConnect.getFTPClient();
client.enterLocalPassiveMode();
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setBufferSize(2048);
if(!client.changeWorkingDirectory(newPath)){
client.makeDirectory(newPath);
}
if(client.changeWorkingDirectory(oldPath)){
FTPFile[] files=client.listFiles();
for(FTPFile file:files){
System.out.println(file.getName());
if(file.getName().trim().equalsIgnoreCase(fileName)){
boolean flag=client.rename(new String(file.getName().getBytes("utf-8"),"ISO-8859-1"),newPath+new String(file.getName().getBytes("utf-8"),"ISO-8859-1"));
System.out.println(flag?"文件移动成功!":"文件移动失败!");
break;
}
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40