欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)FTP文件的上傳和下載功能的實例代碼

 更新時間:2016年11月23日 09:41:56   作者:一個弱者想變強  
FTP 是File Transfer Protocol(文件傳輸協(xié)議)的英文簡稱,而中文簡稱為“文傳協(xié)議”。接下來通過本文給大家實例講解Java實現(xiàn)FTP文件的上傳和下載功能,需要的的朋友一起看看吧

FTP 是File Transfer Protocol(文件傳輸協(xié)議)的英文簡稱,而中文簡稱為“文傳協(xié)議”。用于Internet上的控制文件的雙向傳輸。同時,它也是一個應(yīng)用程序(Application)?;诓煌牟僮飨到y(tǒng)有不同的FTP應(yīng)用程序,而所有這些應(yīng)用程序都遵守同一種協(xié)議以傳輸文件。在FTP的使用當中,用戶經(jīng)常遇到兩個概念:"下載"(Download)和"上傳"(Upload)。"下載"文件就是從遠程主機拷貝文件至自己的計算機上;"上傳"文件就是將文件從自己的計算機中拷貝至遠程主機上。用Internet語言來說,用戶可通過客戶機程序向(從)遠程主機上傳(下載)文件。

  首先下載了Serv-U將自己的電腦設(shè)置為了FTP文件服務(wù)器,方便操作。

1.FTP文件的下載(從FTP服務(wù)器下載到本機)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpApche {
private static FTPClient ftpClient = new FTPClient();
private static String encoding = System.getProperty("file.encoding");
/**
* Description: 從FTP服務(wù)器下載文件
* 
* @Version1.0
* @param url
* FTP服務(wù)器hostname
* @param port
* FTP服務(wù)器端口
* @param username
* FTP登錄賬號
* @param password
* FTP登錄密碼
* @param remotePath
* FTP服務(wù)器上的相對路徑
* @param fileName
* 要下載的文件名
* @param localPath
* 下載后保存到本地的路徑
* @return
*/
public static boolean downFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
boolean result = false;
try {
int reply;
ftpClient.setControlEncoding(encoding);
/*
* 為了上傳和下載中文文件,有些地方建議使用以下兩句代替
* new String(remotePath.getBytes(encoding),"iso-8859-1")轉(zhuǎn)碼。
* 經(jīng)過測試,通不過。
*/
// FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
// conf.setServerLanguageCode("zh");
ftpClient.connect(url, port);
// 如果采用默認端口,可以使用ftp.connect(url)的方式直接連接FTP服務(wù)器
ftpClient.login(username, password);// 登錄
// 設(shè)置文件傳輸類型為二進制
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 獲取ftp登錄應(yīng)答代碼
reply = ftpClient.getReplyCode();
// 驗證是否登陸成功
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
return result;
}
// 轉(zhuǎn)移到FTP服務(wù)器目錄至指定的目錄下
ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
// 獲取文件列表
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftpClient.retrieveFile(ff.getName(), is);
is.close();
}
}
ftpClient.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
/**
* 將FTP服務(wù)器上文件下載到本地
* 
*/
public void testDownFile() {
try {
boolean flag = downFile("10.0.0.102", 21, "admin",
"123456", "/", "ip.txt", "E:/");
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FtpApche fa = new FtpApche();
fa.testDownFile();
}
}

2.FTP文件的上傳(從本機上傳到FTP服務(wù)器)

import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPTest_05 {
private FTPClient ftp;
/** 
* 
* @param path 上傳到ftp服務(wù)器哪個路徑下 
* @param addr 地址 
* @param port 端口號 
* @param username 用戶名 
* @param password 密碼 
* @return 
* @throws Exception 
*/ 
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
/** 
* 
* @param file 上傳的文件或文件夾 
* @throws Exception 
*/ 
private void upload(File file) throws Exception{
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0;i < files.length;i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
}else{
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
public static void main(String[] args) throws Exception{
FTPTest_05 t = new FTPTest_05();
boolean connFlag = t.connect("/", "10.0.0.105", 21, "ls", "123456");
System.out.println("connFlag : " + connFlag);
File file = new File("D:\\test02");//本機被傳文件的地址
System.out.println("file : " + file);
t.upload(file);
System.out.println("upload : " + "ok");
}
}

以上所述是小編給大家介紹的Java實現(xiàn)FTP文件的上傳和下載功能的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Spring 實現(xiàn)自定義監(jiān)聽器案例

    Spring 實現(xiàn)自定義監(jiān)聽器案例

    這篇文章主要介紹了Spring 實現(xiàn)自定義監(jiān)聽器案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Java?分位點(分位值)計算方式

    Java?分位點(分位值)計算方式

    這篇文章主要介紹了Java?分位點(分位值)計算方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 如何使用stream從List對象中獲取某列數(shù)據(jù)

    如何使用stream從List對象中獲取某列數(shù)據(jù)

    這篇文章主要介紹了如何使用stream從List對象中獲取某列數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 教你如何在Intellij IDEA中集成Gitlab

    教你如何在Intellij IDEA中集成Gitlab

    今天來簡單說下,如何在IDEA中集成gitlab項目,默認情況下IDEA中的 VCS => Checkout From Version Control 選項中是沒有g(shù)itlab這一項的,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友參考下吧
    2023-10-10
  • java單例模式實現(xiàn)的方法

    java單例模式實現(xiàn)的方法

    這篇文章主要介紹了如何在JAVA中實現(xiàn)單例模式,文中代碼簡單易懂,供大家參考學習,感興趣的小伙伴可以了解下
    2020-06-06
  • Java字符串常見的操作(比較,查找,替換等)

    Java字符串常見的操作(比較,查找,替換等)

    在Java當中,為字符串類提供了豐富的操作方法,對于字符串,我們常見的操作就是:字符串的比較、查找、替換、拆分、截取以及其他的一些操作,本文就詳細的介紹一下,感興趣的可以了解一下
    2022-01-01
  • 詳解Java實現(xiàn)多種方式的http數(shù)據(jù)抓取

    詳解Java實現(xiàn)多種方式的http數(shù)據(jù)抓取

    本篇文章主要介紹了Java實現(xiàn)多種方式的http數(shù)據(jù)抓取,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。
    2016-12-12
  • Springmvc異常映射2種實現(xiàn)方法

    Springmvc異常映射2種實現(xiàn)方法

    這篇文章主要介紹了Springmvc異常映射2種實現(xiàn)方法以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。,需要的朋友可以參考下
    2020-05-05
  • 單例模式 分析代碼優(yōu)化方法

    單例模式 分析代碼優(yōu)化方法

    這篇文章主要介紹了單例模式 分析代碼優(yōu)化方法,需要的朋友可以參考下
    2015-04-04
  • springboot 使用Spring Boot Actuator監(jiān)控應(yīng)用小結(jié)

    springboot 使用Spring Boot Actuator監(jiān)控應(yīng)用小結(jié)

    本篇文章主要介紹了springboot 使用Spring Boot Actuator監(jiān)控應(yīng)用小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02

最新評論