最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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登錄賬號(hào)
* @param password
* FTP登錄密碼
* @param remotePath
* FTP服務(wù)器上的相對(duì)路徑
* @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)過測(cè)試,通不過。
*/
// FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
// conf.setServerLanguageCode("zh");
ftpClient.connect(url, port);
// 如果采用默認(rèn)端口,可以使用ftp.connect(url)的方式直接連接FTP服務(wù)器
ftpClient.login(username, password);// 登錄
// 設(shè)置文件傳輸類型為二進(jìn)制
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 獲取ftp登錄應(yīng)答代碼
reply = ftpClient.getReplyCode();
// 驗(yàn)證是否登陸成功
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文件的上傳(從本機(jī)上傳到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ù)器哪個(gè)路徑下 
* @param addr 地址 
* @param port 端口號(hào) 
* @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");//本機(jī)被傳文件的地址
System.out.println("file : " + file);
t.upload(file);
System.out.println("upload : " + "ok");
}
}

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

相關(guān)文章

  • Spring Bean生命周期之Bean的實(shí)例化詳解

    Spring Bean生命周期之Bean的實(shí)例化詳解

    這篇文章主要為大家詳細(xì)介紹了Spring Bean生命周期之Bean的實(shí)例化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 關(guān)于java.lang.NumberFormatException: null的問題及解決

    關(guān)于java.lang.NumberFormatException: null的問題及解決

    這篇文章主要介紹了關(guān)于java.lang.NumberFormatException: null的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java中jakarta.validation數(shù)據(jù)校驗(yàn)幾個(gè)主要依賴包講解

    Java中jakarta.validation數(shù)據(jù)校驗(yàn)幾個(gè)主要依賴包講解

    在Java開發(fā)中,BeanValidationAPI提供了一套標(biāo)準(zhǔn)的數(shù)據(jù)驗(yàn)證機(jī)制,尤其是通過JakartaBeanValidation(原HibernateValidator)實(shí)現(xiàn),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-09-09
  • 使用AOP攔截Controller獲取@PathVariable注解傳入的參數(shù)

    使用AOP攔截Controller獲取@PathVariable注解傳入的參數(shù)

    這篇文章主要介紹了使用AOP攔截Controller獲取@PathVariable注解傳入的參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • springboot pojo對(duì)象日期屬性的問題

    springboot pojo對(duì)象日期屬性的問題

    這篇文章主要介紹了springboot pojo對(duì)象日期屬性的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Hibernate延遲加載技術(shù)詳解

    Hibernate延遲加載技術(shù)詳解

    這篇文章主要介紹了Hibernate延遲加載技術(shù),結(jié)合實(shí)例形式詳細(xì)分析了Hibernate延遲加載所涉及的各種常用技巧,需要的朋友可以參考下
    2016-03-03
  • SpringBoot實(shí)現(xiàn)PPT格式文件上傳并在線預(yù)覽功能

    SpringBoot實(shí)現(xiàn)PPT格式文件上傳并在線預(yù)覽功能

    本文介紹SpringBoot實(shí)現(xiàn)PPT格式文件上傳并在線預(yù)覽功能,通過上傳接口,可在C盤的tempfile目錄下找到上傳的文件,預(yù)覽時(shí)會(huì)在同級(jí)目錄下創(chuàng)建一個(gè)相同文件名后綴為pdf的文件,每次預(yù)覽會(huì)先查找文件是否存在,存在則直接預(yù)覽,不存在則會(huì)走上面的處理,需要的朋友可以參考下
    2022-02-02
  • java開發(fā)hutool HttpUtil網(wǎng)絡(luò)請(qǐng)求工具使用demo

    java開發(fā)hutool HttpUtil網(wǎng)絡(luò)請(qǐng)求工具使用demo

    這篇文章主要為大家介紹了hutool之HttpUtil網(wǎng)絡(luò)請(qǐng)求工具使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 從Spring源碼解析事務(wù)失效的原因

    從Spring源碼解析事務(wù)失效的原因

    今天帶大家學(xué)習(xí)Spring的相關(guān)知識(shí),文章圍繞著Spring事務(wù)失效的原因等相關(guān)知識(shí)展開,文中有非常詳細(xì)的介紹及圖文示例,需要的朋友可以參考下
    2021-06-06
  • Java異?;A(chǔ)知識(shí)解析

    Java異?;A(chǔ)知識(shí)解析

    這篇文章主要介紹了Java異?;A(chǔ)知識(shí)解析,具有一定借鑒價(jià)值,需要的朋友可以資參考下。
    2017-12-12

最新評(píng)論

砀山县| 行唐县| 珠海市| 临邑县| 建德市| 桦南县| 象州县| 阿瓦提县| 江津市| 日照市| 稻城县| 富蕴县| 陈巴尔虎旗| 满洲里市| 九江市| 石阡县| 攀枝花市| 和硕县| 永寿县| 维西| 清苑县| 敦化市| 上饶县| 城口县| 宣城市| 井冈山市| 东港市| 棋牌| 三台县| 芦溪县| 鄂温| 神农架林区| 千阳县| 大竹县| 邳州市| 朝阳区| 江西省| 黄山市| 防城港市| 榆树市| 乐业县|