Java FTPClient完成文件上傳下載。本站提示廣大學習愛好者:(Java FTPClient完成文件上傳下載)文章只能為提供參考,不一定能成為您想要的結果。以下是Java FTPClient完成文件上傳下載正文
在JAVA法式中,常常須要和FTP打交道,好比向FTP辦事器上傳文件、下載文件,本文簡略引見若何應用jakarta commons中的FTPClient(在commons-net包中)完成上傳下載文件。
所用到的jar包有:
commons-net-1.4.1.jar
jakarta-oro.jar
1、上傳文件
文件上傳源代碼
/**
* Description: 向FTP辦事器上傳文件
* @Version1.0
* @param url FTP辦事器hostname
* @param port FTP辦事器端口
* @param username FTP登錄賬號
* @param password FTP登錄暗碼
* @param path FTP辦事器保留目次
* @param filename 上傳到FTP辦事器上的文件名
* @param input 輸出流
* @return 勝利前往true,不然前往false
*/
public static boolean uploadFile(
String url,//FTP辦事器hostname
int port,//FTP辦事器端口
String username, // FTP登錄賬號
String password, //FTP登錄暗碼
String path, //FTP辦事器保留目次
String filename, //上傳到FTP辦事器上的文件名
InputStream input // 輸出流
) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);//銜接FTP辦事器
//假如采取默許端口,可使用ftp.connect(url)的方法直接銜接FTP辦事器
ftp.login(username, password);//登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
以下是文件上傳的測試用例:
/**
* 將當地文件上傳到FTP辦事器上
*
*/
public void testUpLoadFromDisk(){
try {
FileInputStream in=new FileInputStream(new File("D:/test.txt"));
boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 在FTP辦事器上生成一個文件,並將一個字符串寫入到該文件中
*
*/
public void testUpLoadFromString(){
try {
String str = "這是要寫入的字符串!";
InputStream input = new ByteArrayInputStream(str.getBytes("utf-8"));
boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", input);
System.out.println(flag);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
2、文件下載
文件下載源代碼
/**
* Description: 從FTP辦事器下載文件
* @Version1.0
* @param url FTP辦事器hostname
* @param port FTP辦事器端口
* @param username FTP登錄賬號
* @param password FTP登錄暗碼
* @param remotePath FTP辦事器上的絕對途徑
* @param fileName 要下載的文件名
* @param localPath 下載後保留到當地的途徑
* @return
*/
public static boolean downFile(
String url, //FTP辦事器hostname
int port,//FTP辦事器端口
String username, //FTP登錄賬號
String password, //FTP登錄暗碼
String remotePath,//FTP辦事器上的絕對途徑
String fileName,//要下載的文件名
String localPath//下載後保留到當地的途徑
) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
//假如采取默許端口,可使用ftp.connect(url)的方法直接銜接FTP辦事器
ftp.login(username, password);//登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);//轉移到FTP辦事器目次
FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
if(ff.getName().equals(fileName)){
File localFile = new File(localPath+"/"+ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
以下是文件下載的測試用例:
/**
* 將FTP辦事器上文件下載到當地
*
*/
public void testDownFile(){
try {
boolean flag = downFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", "D:/");
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助。