利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


public class FtpTest {


public static void main(String[] args) {
String server = "192.168.1.103";
String user = "admin";
String password = "pass";
boolean error = false;
FTPClient ftp = new FTPClient();
ftp.setControlEncoding("UTF-8"); // 中文支持
ftp.setConnectTimeout(5000); // 超时设置


try {
int reply;
ftp.connect(server);
System.out.println("Connected to " + server + ".");
System.out.println(ftp.getReplyString());


// After connection attempt, you should check the reply code to
// verify success.
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}


ftp.login(user, password);
System.out.println("Login by " + user + ".");
System.out.println(ftp.getReplyString());


reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("Invalid username or password.");
System.exit(1);
} else {
System.out.println(ftp.getSystemName());


FTPClientConfig conf = new FTPClientConfig(ftp.getSystemName()
.split(" ")[0]);
conf.setServerLanguageCode("zh");
ftp.configure(conf);


ftp.enterLocalPassiveMode(); // Use passive mode as default
// because most of us are behind
// firewalls these days.
ftp.setFileType(FTP.BINARY_FILE_TYPE); // 二进制文件支持
}


// transfer files
FTPFile[] files = ftp.listFiles("/Upload");
ftp.changeWorkingDirectory("/Upload");
for (FTPFile file : files) {
String fileName = file.getName();
System.out.println((file.isDirectory() ? "Folder" : (file
.isFile() ? "File" : ""))
+ " - " + fileName);


if (file.isFile()) {
// 读取ftp上指定文件的数据
InputStream is = ftp.retrieveFileStream(fileName);
if (is != null) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("\t" + line);
}
reader.close();
is.close();


System.out.println(ftp.getReplyString());
// Must call completePendingCommand() to finish command.
if (!ftp.completePendingCommand()) {
ftp.logout();
ftp.disconnect();
System.err.println("File transfer failed.");
System.exit(1);
}
}


// 下载ftp上指定文件到本地
File localFile = new File("F:" + File.separator + fileName);
boolean downloaded = ftp.retrieveFile(fileName,
new FileOutputStream(localFile));
System.out.println(downloaded ? "File " + fileName
+ " download success" : "File " + fileName
+ " download failure");
System.out.println(ftp.getReplyString());


// 删除ftp上指定文件
boolean deleted = ftp.deleteFile(fileName);
System.out.println(deleted ? "File " + fileName
+ " delete success" : "File " + fileName
+ " delete failure");
System.out.println(ftp.getReplyString());


System.out.println("--------------------------------");
}
}


// 上传本地文件到ftp指定目录
String uploadFileName = "HFSetup5.4.4.422.exe";
File uploadFile = new File("F:" + File.separator + "TDDOWNLOAD"
+ File.separator + uploadFileName);
boolean uploaded = ftp.storeFile(uploadFileName,
new FileInputStream(uploadFile));
System.out.println(uploaded ? "File " + uploadFileName
+ " upload success" : "File " + uploadFileName
+ " upload failure");
System.out.println(ftp.getReplyString());


// 在ftp上更改文件名、移动文件
boolean moved = ftp.rename("/Upload" + File.separator
+ uploadFileName, "/Success" + File.separator
+ uploadFileName);
System.out.println(moved ? "File " + uploadFileName
+ " move success" : "File " + uploadFileName
+ " move failure");
System.out.println(ftp.getReplyString());
} catch (Exception e) {
error = true;
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException e) {
// do nothing
}
}
System.exit(error ? 1 : 0);
}
}
}

你可能感兴趣的:(apache,file,string,download,server,upload)