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

java實(shí)現(xiàn)上傳文件到FTP

 更新時(shí)間:2022年06月23日 16:13:37   作者:江湖見(jiàn)  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)上傳文件到FTP,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

需求說(shuō)明:將指定文件上傳到FTP,上傳成功后修改文件名。

獲取文件名及路徑(這里是從數(shù)據(jù)庫(kù)獲取,所以前面的代碼就不CV了)

/**
?? ? * 測(cè)試上傳
?? ? * @param map 從數(shù)據(jù)庫(kù)獲取到的文件信息 (包含文件路徑FILE_PATH 文件類型FILE_TYPE等信息)
?? ? */
?? ? public void testUpdFtpFile(Map<String,Object> map){
?? ??? ? /*上傳附件到FTP*/
?? ??? ? FileInputStream inputStream = null;
?? ??? ? try {
?? ??? ? ? ?//找到要上傳的文件?
?? ??? ? ?? ?String originfilename = "E:\\work\\files\\"+map.get("FILE_PATH").toString();
?? ??? ? ?? ?//轉(zhuǎn)成流
?? ??? ? ?? ?inputStream = new FileInputStream(originfilename);
?? ??? ? ? ?//上傳后的文件名+文件類型?? ??? ??? ??? ??? ??? ?
?? ??? ? ? ?String ftpName = "上傳到FTP后的文件名."+map.get("FILE_TYPE");
?? ??? ? ?? ?boolean updFtpFile = FtpClientFile.uploadFile(ftpName,inputStream);
?? ??? ? ?? ?if(updFtpFile){
?? ??? ? ?? ??? ?//打印下日志
?? ??? ? ?? ??? ?System.out.println(("=======文件已上傳到FTP========"));
?? ??? ? ?? ?}
?? ??? ? } catch (Exception e) {
?? ??? ? ?? ?throw new BusinessException("附件上傳失敗!");
?? ??? ? }
?? ? }

FtpClientFile工具類方法

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
?
import com.google.gson.JsonArray;
?
/**
?* ftp 上傳文件
?* FTPClient commons.net 3.0.1版本
?* @author Lenovo
?*
?*/
public class FtpClientFile {
?? ?
?? ?private static final String hostname = "10.xx.xx.xx" ;//FTP IP
?? ?
?? ?private static final int port = 21;//FTP 端口號(hào)
?? ?
?? ?private static final String username = "ftpName";//FTP 登錄賬號(hào)
? ?
?? ?private static final String password = "ftpPsd"; //FTP 登錄密碼
?? ?
?? ?private static final String pathname = "/";//FTP 工作路徑
?? ?
?? ?
?? ?/**
?? ? * 上傳文件(可供Action/Controller層使用)
?? ? * @param fileName 上傳到FTP服務(wù)器后的文件名稱
?? ? * @param inputStream 輸入文件流
?? ? * @return
?? ? */
?? ? public static boolean uploadFile(String fileName,FileInputStream inputStream){
?? ??? ??
?? ? boolean flag = false;
?? ? FTPClient ftpClient = new FTPClient();
?? ? //設(shè)置超時(shí)
?? ? ftpClient.setConnectTimeout(60*60*1000);
?? ? //設(shè)置編碼
?? ? ftpClient.setControlEncoding("UTF-8");
?? ? try {
?? ??? ? //連接FTP服務(wù)器
?? ??? ? ftpClient.connect(hostname, port);
?? ??? ? //登錄FTP服務(wù)器
?? ??? ? ftpClient.login(username, password);
?? ??? ? //是否成功登錄FTP服務(wù)器?? ?
?? ??? ? int replyCode = ftpClient.getReplyCode();
?? ??? ? if(!FTPReply.isPositiveCompletion(replyCode)){
?? ??? ??? ? return flag;
?? ? ?? ??? ?}
?? ??? ? System.out.println("===========登錄FTP成功了==========");
?? ??? ? ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
?? ??? ? //切換路徑 創(chuàng)建路徑
?? ??? ? ftpClient.makeDirectory(pathname);
?? ??? ? ftpClient.changeWorkingDirectory(pathname);
?? ??? ? ftpClient.enterLocalPassiveMode();
?? ??? ? //設(shè)置緩沖
?? ??? ? ftpClient.setBufferSize(1024 * 1024 * 20);
?? ??? ? //保持連接
?? ??? ? ftpClient.setKeepAlive(true);
?? ??? ? boolean a = ftpClient.storeFile(new String(fileName.getBytes("utf-8"),"iso-8859-1"), inputStream);
?? ??? ? if(a){
?? ??? ??? ? System.out.println("===========創(chuàng)建文件成功=============="+a);
?? ??? ??? ??? ? String fileName2 = fileName+"AAA";
?? ??? ??? ??? ? boolean status = ftpClient.rename(fileName, fileName2);
?? ??? ??? ??? ? if(status)
?? ??? ??? ??? ? System.out.println("===========修改文件名稱成功=============="+status);
?? ??? ??? ? }
?? ??? ? inputStream.close();
?? ??? ? ftpClient.logout();
?? ??? ? flag = true;
?? ? ?? ?} catch (Exception e) {
?? ? ?? ??? ?e.printStackTrace();
?? ? ?? ?} finally{
?? ? ?? ??? ?if(ftpClient.isConnected()){
?? ? ?? ??? ??? ?try {
?? ? ?? ??? ??? ??? ?ftpClient.disconnect();
?? ? ?? ??? ??? ?} catch (IOException e) {
?? ? ?? ??? ??? ??? ?e.printStackTrace();
?? ? ?? ??? ??? ?}
?? ? ?? ??? ?}
?? ? ?? ?}
?? ? ?? ?return flag;
?? ? ?? ?
?? ? }
?? ?
?? ??
?? ? ?? ?
?? ?/* public static void main(String[] args) {
?? ??? ? String originfilename = "C:\\Users\\Lenovo\\Desktop\\xx.txt";
?? ??? ? FileInputStream inputStream;
?? ??? ? try {
?? ??? ??? ?inputStream = new FileInputStream(new File(originfilename));
?? ??? ??? ?boolean a = uploadFile("xx.txt","/104/",inputStream);
?? ??? ??? ?System.out.println("上傳文件成功============"+a);
?? ??? ? } catch (FileNotFoundException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ? }
?? ?}*/
?? ??
?? ??
}

上傳文件到FTP時(shí) 注意:是否有權(quán)限登錄服務(wù)器/上傳文件等操作。

默認(rèn)在瀏覽器輸入自己的ftp地址訪問(wèn)下看看 ftp://10.xx.xx.xx:端口號(hào) 登錄看看

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

相關(guān)文章

  • java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段

    java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段

    這篇文章主要為大家詳細(xì)介紹了java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • LeetCode程序員面試題之無(wú)重復(fù)字符的最長(zhǎng)子串

    LeetCode程序員面試題之無(wú)重復(fù)字符的最長(zhǎng)子串

    Java計(jì)算無(wú)重復(fù)字符的最長(zhǎng)子串是一種常見(jiàn)的字符串處理算法,它的目的是找出一個(gè)字符串中無(wú)重復(fù)字符的最長(zhǎng)子串。該算法可以很好地解決一些字符串處理問(wèn)題,比如尋找字符串中重復(fù)字符的位置,以及計(jì)算字符串中無(wú)重復(fù)字符的最長(zhǎng)子串的長(zhǎng)度。
    2023-02-02
  • Java實(shí)現(xiàn)分頁(yè)查詢功能

    Java實(shí)現(xiàn)分頁(yè)查詢功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)分頁(yè)查詢功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • JDBC 程序的常見(jiàn)錯(cuò)誤及調(diào)試方法

    JDBC 程序的常見(jiàn)錯(cuò)誤及調(diào)試方法

    本文是《Java Web開(kāi)發(fā)教程——入門與提高篇(JSP+Servlet)》一書《第9章 JDBC技術(shù)》的補(bǔ)充內(nèi)容。
    2009-06-06
  • org.apache.zookeeper.KeeperException.BadVersionException異常的解決

    org.apache.zookeeper.KeeperException.BadVersionException異常的解

    在使用Apache ZooKeeper進(jìn)行分布式協(xié)調(diào)時(shí),你可能會(huì)遇到org.apache.zookeeper.KeeperException.BadVersionException異常,本文就來(lái)介紹一下解決方法,感興趣的可以了解一下
    2024-03-03
  • SpringCloud2020整合Nacos-Bootstrap配置不生效的解決

    SpringCloud2020整合Nacos-Bootstrap配置不生效的解決

    這篇文章主要介紹了SpringCloud2020整合Nacos-Bootstrap配置不生效的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java實(shí)現(xiàn)圖片對(duì)比功能

    Java實(shí)現(xiàn)圖片對(duì)比功能

    個(gè)人從來(lái)沒(méi)有研究過(guò)圖像學(xué),也沒(méi)看過(guò)什么論文或者相關(guān)文檔,寫這個(gè)完全是靠google和百度,自己寫了個(gè)實(shí)驗(yàn)了下,測(cè)試用例也少,估計(jì)有大BUG的存在,所以看的人權(quán)當(dāng)學(xué)習(xí)交流,切勿生產(chǎn)使用。
    2014-09-09
  • Spring @Async 的使用與實(shí)現(xiàn)的示例代碼

    Spring @Async 的使用與實(shí)現(xiàn)的示例代碼

    本篇文章主要介紹了Spring @Async 的使用與實(shí)現(xiàn)的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Java線程池隊(duì)列LinkedTransferQueue示例詳解

    Java線程池隊(duì)列LinkedTransferQueue示例詳解

    這篇文章主要為大家介紹了Java線程池隊(duì)列LinkedTransferQueue示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • SpringMVC中Json數(shù)據(jù)交互處理示例詳解

    SpringMVC中Json數(shù)據(jù)交互處理示例詳解

    這篇文章主要介紹了SpringMVC中Json數(shù)據(jù)交互處理的相關(guān)資料,分別講解了JSON的基本概念、構(gòu)成要素、數(shù)據(jù)類型、對(duì)象和數(shù)組的表示方法、字符串的轉(zhuǎn)義規(guī)則以及JSON與JavaScript的關(guān)系,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03

最新評(píng)論

全南县| 宁河县| 杭锦后旗| 皮山县| 叙永县| 绵阳市| 柳江县| 武宁县| 德格县| 丹寨县| 肥乡县| 佛冈县| 南充市| 喀什市| 余姚市| 宜君县| 汕头市| 汉中市| 监利县| 遂溪县| 文安县| 河北省| 福建省| 宁津县| 乐业县| 雷波县| 永昌县| 驻马店市| 进贤县| 海晏县| 威宁| 莎车县| 闵行区| 新建县| 治县。| 册亨县| 桐乡市| 神池县| 邹平县| 浦城县| 镶黄旗|