JAVA中應用FTPClient完成文件上傳下載實例代碼。本站提示廣大學習愛好者:(JAVA中應用FTPClient完成文件上傳下載實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是JAVA中應用FTPClient完成文件上傳下載實例代碼正文
在java法式開辟中,ftp用的比擬多,常常打交道,好比說向FTP辦事器上傳文件、下載文件,本文給年夜家引見若何應用jakarta commons中的FTPClient(在commons-net包中)完成上傳下載文件。
1、上傳文件
道理就不引見了,年夜家直接看代碼吧
/**
* Description: 向FTP辦事器上傳文件
* @Version1.0 Jul 27, 2008 4:31:09 PM by 崔紅保(cuihongbao@d-heaven.com)創立
* @param url FTP辦事器hostname
* @param port FTP辦事器端口
* @param username FTP登錄賬號
* @param password FTP登錄暗碼
* @param path FTP辦事器保留目次
* @param filename 上傳到FTP辦事器上的文件名
* @param input 輸出流
* @return 勝利前往true,不然前往false
*/
publicstaticboolean uploadFile(String url,int port,String username, String password, String path, String filename, 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;
}<pre></pre>
/**
* Description: 向FTP辦事器上傳文件
* @Version1.0 Jul 27, 2008 4:31:09 PM by 崔紅保(cuihongbao@d-heaven.com)創立
* @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,int port,String username, String password, String path, String filename, 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;
}
上面我們寫兩個小例子:
1.將當地文件上傳到FTP辦事器上,代碼以下:
@Test
publicvoid testUpLoadFromDisk(){
try {
FileInputStream in=new FileInputStream(new File("D:/test.txt"));
boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}<pre></pre>
@Test
public void testUpLoadFromDisk(){
try {
FileInputStream in=new FileInputStream(new File("D:/test.txt"));
boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
2.在FTP辦事器上生成一個文件,並將一個字符串寫入到該文件中
@Test
publicvoid testUpLoadFromString(){
try {
InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8"));
boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", input);
System.out.println(flag);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}<pre></pre>
@Test
public void testUpLoadFromString(){
try {
InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8"));
boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", input);
System.out.println(flag);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
2、下載文件
從FTP辦事器下載文件的代碼也很簡略,參考以下:
/**
* Description: 從FTP辦事器下載文件
* @Version. Jul , :: PM by 崔紅保(cuihongbao@d-heaven.com)創立
* @param url FTP辦事器hostname
* @param port FTP辦事器端口
* @param username FTP登錄賬號
* @param password FTP登錄暗碼
* @param remotePath FTP辦事器上的絕對途徑
* @param fileName 要下載的文件名
* @param localPath 下載後保留到當地的途徑
* @return
*/
publicstaticboolean downFile(String url, int port,String username, String password, String remotePath,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;
}<pre></pre>