java怎么實現上傳文件到FTP

蝸牛 互聯網技術資訊 2022-06-24 17 0

這篇“java怎么實現上傳文件到FTP”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“java怎么實現上傳文件到FTP”文章吧。

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

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

/**
?????*?測試上傳
?????*?@param?map?從數據庫獲取到的文件信息?(包含文件路徑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();
?????????????//轉成流
?????????????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?端口號
????
????private?static?final?String?username?=?"ftpName";//FTP?登錄賬號
???
????private?static?final?String?password?=?"ftpPsd";?//FTP?登錄密碼
????
????private?static?final?String?pathname?=?"/";//FTP?工作路徑
????
????
????/**
?????*?上傳文件(可供Action/Controller層使用)
?????*?@param?fileName?上傳到FTP服務器后的文件名稱
?????*?@param?inputStream?輸入文件流
?????*?@return
?????*/
?????public?static?boolean?uploadFile(String?fileName,FileInputStream?inputStream){
?????????
?????boolean?flag?=?false;
?????FTPClient?ftpClient?=?new?FTPClient();
?????//設置超時
?????ftpClient.setConnectTimeout(60*60*1000);
?????//設置編碼
?????ftpClient.setControlEncoding("UTF-8");
?????try?{
?????????//連接FTP服務器
?????????ftpClient.connect(hostname,?port);
?????????//登錄FTP服務器
?????????ftpClient.login(username,?password);
?????????//是否成功登錄FTP服務器????
?????????int?replyCode?=?ftpClient.getReplyCode();
?????????if(!FTPReply.isPositiveCompletion(replyCode)){
?????????????return?flag;
?????????????}
?????????System.out.println("===========登錄FTP成功了==========");
?????????ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
?????????//切換路徑?創建路徑
?????????ftpClient.makeDirectory(pathname);
?????????ftpClient.changeWorkingDirectory(pathname);
?????????ftpClient.enterLocalPassiveMode();
?????????//設置緩沖
?????????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("===========創建文件成功=============="+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時 注意:是否有權限登錄服務器/上傳文件等操作。

默認在瀏覽器輸入自己的ftp地址訪問下看看 ftp://10.xx.xx.xx:端口號 登錄看看

java怎么實現上傳文件到FTP  java 第1張

以上就是關于“java怎么實現上傳文件到FTP”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注蝸牛博客行業資訊頻道。

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:niceseo99@gmail.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

評論

日本韩欧美一级A片在线观看