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

Java實現(xiàn)FTP上傳與下載功能

 更新時間:2022年02月22日 15:45:04   作者:hehuijava  
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)FTP上傳與下載功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java實現(xiàn)FTP上傳與下載的具體代碼,供大家參考,具體內(nèi)容如下

JAVA操作FTP服務(wù)器,只需要創(chuàng)建一個FTPClient即可,所有的操作都封裝在FTPClient中,JDK自帶的有FTPClient(sun.net.ftp.FtpClient),也可以用第三方的FTPClient,一般使用apache的FTPClient(org.apache.commons.net.ftp.FTPClient),本文將使用apache的FTPClient,API都大同小異

關(guān)鍵依賴:commons-net

對常用操作(上傳、下載)封裝成工具類

package com.day0322;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
?* FTP工具類
?* 文件上傳
?* 文件下載
?*/
public class FTPUtil {

? ? private static final Logger log = LoggerFactory.getLogger(FTPUtil.class);

? ? /**
? ? ?* 設(shè)置緩沖區(qū)大小4M
? ? ?**/
? ? private static final int BUFFER_SIZE = 1024 * 1024 * 4;

? ? /**
? ? ?* 本地字符編碼
? ? ?**/
? ? private static String LOCAL_CHARSET = "GBK";

? ? /**
? ? ?* UTF-8字符編碼
? ? ?**/
? ? private static final String CHARSET_UTF8 = "UTF-8";

? ? /**
? ? ?* OPTS UTF8字符串常量
? ? ?**/
? ? private static final String OPTS_UTF8 = "OPTS UTF8";

? ? /**
? ? ?* FTP協(xié)議里面,規(guī)定文件名編碼為iso-8859-1
? ? ?**/
? ? private static final String SERVER_CHARSET = "ISO-8859-1";

? ? private static FTPClient ftpClient = null;

? ? /**
? ? ?* 連接FTP服務(wù)器
? ? ?*/
? ? private static void login(OaFtp oaFtp) {
? ? ? ? ftpClient = new FTPClient();
? ? ? ? try {
? ? ? ? ? ? ftpClient.connect(oaFtp.getIp(), Integer.valueOf(oaFtp.getPort()));
? ? ? ? ? ? ftpClient.login(oaFtp.getName(), oaFtp.getPwd());
? ? ? ? ? ? ftpClient.setBufferSize(BUFFER_SIZE);
? ? ? ? ? ? ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
? ? ? ? ? ? int reply = ftpClient.getReplyCode();
? ? ? ? ? ? if (!FTPReply.isPositiveCompletion(reply)) {
? ? ? ? ? ? ? ? closeConnect();
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? log.error("",e);
? ? ? ? ? ? throw new RuntimeException(e);
? ? ? ? }
? ? }

? ? /**
? ? ?* 關(guān)閉FTP連接
? ? ?*/
? ? private static void closeConnect() {
? ? ? ? if (ftpClient != null && ftpClient.isConnected()) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ftpClient.logout();
? ? ? ? ? ? ? ? ftpClient.disconnect();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? log.error("",e);
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? /**
? ? ?* FTP服務(wù)器路徑編碼轉(zhuǎn)換
? ? ?*
? ? ?* @param ftpPath FTP服務(wù)器路徑
? ? ?* @return String
? ? ?*/
? ? private static String changeEncoding(String ftpPath) {
? ? ? ? String directory = null;
? ? ? ? try {
? ? ? ? ? ? if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(OPTS_UTF8, "ON"))) {
? ? ? ? ? ? ? ? LOCAL_CHARSET = CHARSET_UTF8;
? ? ? ? ? ? }
? ? ? ? ? ? directory = new String(ftpPath.getBytes(LOCAL_CHARSET), SERVER_CHARSET);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? log.error("",e);
? ? ? ? }
? ? ? ? return directory;
? ? }

? ? /**
? ? ?* 改變工作目錄
? ? ?* 如果沒有,則創(chuàng)建工作目錄
? ? ?* @param path
? ? ?*/
? ? private static void changeAndMakeWorkingDir(String path) {
? ? ? ? try {
? ? ? ? ? ? ftpClient.changeWorkingDirectory("/");
? ? ? ? ? ? path = path.replaceAll("\\\\","/");
? ? ? ? ? ? String[] path_array = path.split("/");
? ? ? ? ? ? for (String s : path_array) {
? ? ? ? ? ? ? ? boolean b = ftpClient.changeWorkingDirectory(s);
? ? ? ? ? ? ? ? if (!b) {
? ? ? ? ? ? ? ? ? ? ftpClient.makeDirectory(s);
? ? ? ? ? ? ? ? ? ? ftpClient.changeWorkingDirectory(s);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? log.error("",e);
? ? ? ? ? ? throw new RuntimeException(e);
? ? ? ? }
? ? }

? ? /**
? ? ?* 上傳
? ? ?* @param oaFtp
? ? ?* @param filename
? ? ?* @param dirPath
? ? ?* @param in
? ? ?* @return
? ? ?*/
? ? public static boolean upload (OaFtp oaFtp, String filename, String dirPath, InputStream in) {
? ? ? ? login(oaFtp);
? ? ? ? if (!ftpClient.isConnected()) {
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? boolean isSuccess = false;

? ? ? ? if (ftpClient != null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(OPTS_UTF8, "ON"))) {
? ? ? ? ? ? ? ? ? ? LOCAL_CHARSET = CHARSET_UTF8;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ftpClient.setControlEncoding(LOCAL_CHARSET);
? ? ? ? ? ? ? ? String path = changeEncoding(dirPath);

? ? ? ? ? ? ? ? changeAndMakeWorkingDir(path);
? ? ? ? ? ? ? ? isSuccess = ftpClient.storeFile(new String(filename.getBytes(), SERVER_CHARSET), in);
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? log.error("",e);
? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? closeConnect();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return isSuccess;
? ? }

? ? /**
? ? ?* 下載
? ? ?* @param oaFtp
? ? ?* @param filename
? ? ?* @param dirPath
? ? ?* @param out
? ? ?* @return
? ? ?*/
? ? public static void download (OaFtp oaFtp, String filename, String dirPath, FileOutputStream out) {
? ? ? ? // 登錄
? ? ? ? login(oaFtp);
? ? ? ? if (ftpClient != null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? String path = changeEncoding(dirPath);
? ? ? ? ? ? ? ? changeAndMakeWorkingDir(path);
? ? ? ? ? ? ? ? String[] fileNames = ftpClient.listNames();
? ? ? ? ? ? ? ? if (fileNames == null || fileNames.length == 0) {
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? for (String fileName : fileNames) {
? ? ? ? ? ? ? ? ? ? String ftpName = new String(fileName.getBytes(SERVER_CHARSET), LOCAL_CHARSET);
? ? ? ? ? ? ? ? ? ? if (StringUtils.equals(ftpName,filename)) {
? ? ? ? ? ? ? ? ? ? ? ? InputStream in = ftpClient.retrieveFileStream(fileName);
? ? ? ? ? ? ? ? ? ? ? ? IOUtils.copy(in,out);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? log.error("",e);
? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? closeConnect();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}

測試

1.上傳

2.下載

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java利用for循環(huán)輸出空心三角形、空心菱形和空心矩形的代碼

    Java利用for循環(huán)輸出空心三角形、空心菱形和空心矩形的代碼

    今天小編就為大家分享一篇關(guān)于Java利用for循環(huán)輸出空心三角形、空心菱形和空心矩形的代碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • @Value注入List、數(shù)組、Set、Map問題

    @Value注入List、數(shù)組、Set、Map問題

    這篇文章主要介紹了@Value注入List、數(shù)組、Set、Map問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別

    淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別

    本文主要介紹了淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • java使用poi讀取doc和docx文件的實現(xiàn)示例

    java使用poi讀取doc和docx文件的實現(xiàn)示例

    這篇文章主要介紹了java使用poi讀取doc和docx文件的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • JDBC中PreparedStatement詳解以及應(yīng)用場景實例介紹

    JDBC中PreparedStatement詳解以及應(yīng)用場景實例介紹

    PreparedStatement對象代表的是一個預(yù)編譯的SQL語句,用它提供的setter方法可以傳入查詢的變量,這篇文章主要給大家介紹了關(guān)于JDBC中PreparedStatement詳解以及應(yīng)用場景實例介紹的相關(guān)資料,需要的朋友可以參考下
    2024-02-02
  • JAVA記住密碼功能的實現(xiàn)代碼

    JAVA記住密碼功能的實現(xiàn)代碼

    這篇文章主要介紹了JAVA記住密碼功能的實現(xiàn)代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式

    Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式

    這篇文章主要介紹了Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 使用Mock進(jìn)行業(yè)務(wù)邏輯層Service測試詳解

    使用Mock進(jìn)行業(yè)務(wù)邏輯層Service測試詳解

    這篇文章主要介紹了使用Mock進(jìn)行業(yè)務(wù)邏輯層Service測試詳解,mock是一種模擬對象的技術(shù),用于在測試過程中替代真實的對象,通過mock,我們可以控制被模擬對象的行為和返回值,以便進(jìn)行更加精確的測試,需要的朋友可以參考下
    2023-08-08
  • SpringBoot無法訪問/static下靜態(tài)資源的解決

    SpringBoot無法訪問/static下靜態(tài)資源的解決

    這篇文章主要介紹了SpringBoot無法訪問/static下靜態(tài)資源的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 深入理解java long 存儲時間戳

    深入理解java long 存儲時間戳

    存儲時間打算用時間戳來存儲,打算用long類型來代表時間戳,這篇文章主要介紹了深入理解java long 存儲時間戳,非常具有實用價值,需要的朋友可以參考下
    2018-10-10

最新評論

定南县| 卓尼县| 肥乡县| 青川县| 和顺县| 平利县| 合阳县| 张家界市| 东辽县| 宁武县| 华亭县| 理塘县| 临朐县| 金平| 涡阳县| 镇坪县| 涡阳县| 通城县| 孝义市| 澄江县| 稷山县| 梨树县| 鸡泽县| 灵武市| 额济纳旗| 新河县| 湘潭县| 浏阳市| 楚雄市| 香格里拉县| 剑河县| 革吉县| 道孚县| 肃南| 孝昌县| 合山市| 始兴县| 新昌县| 库伦旗| 阜城县| 蒲江县|