當前位置:首頁 » 文件管理 » 噴ftp

噴ftp

發布時間: 2023-08-24 18:42:28

java 實現ftp上傳如何創建文件夾

這個功能我也剛寫完,不過我也是得益於同行,現在我也把自己的分享給大家,希望能對大家有所幫助,因為自己的項目不涉及到創建文件夾,也僅作分享,不喜勿噴謝謝!

interface:
packagecom.sunline.bank.ftputil;

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importorg.apache.commons.net.ftp.FTPClient;

publicinterfaceIFtpUtils{
/**
*ftp登錄
*@paramhostname主機名
*@paramport埠號
*@paramusername用戶名
*@parampassword密碼
*@return
*/
publicFTPClientloginFtp(Stringhostname,Integerport,Stringusername,Stringpassword);
/**
*上穿文件
*@paramhostname主機名
*@paramport埠號
*@paramusername用戶名
*@parampassword密碼
*@paramfpathftp路徑
*@paramlocalpath本地路徑
*@paramfileName文件名
*@return
*/
(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName);
/**
*批量下載文件
*@paramhostname
*@paramport
*@paramusername
*@parampassword
*@paramfpath
*@paramlocalpath
*@paramfileName源文件名
*@paramfilenames需要修改成的文件名
*@return
*/
publicbooleandownloadFileList(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName,Stringfilenames);
/**
*修改文件名
*@paramlocalpath
*@paramfileName源文件名
*@paramfilenames需要修改的文件名
*/
(Stringlocalpath,StringfileName,Stringfilenames);
/**
*關閉流連接、ftp連接
*@paramftpClient
*@parambufferRead
*@parambuffer
*/
publicvoidcloseFtpConnection(FTPClientftpClient,,BufferedInputStreambuffer);
}

impl:
packagecom.sunline.bank.ftputil;

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPFile;
importorg.apache.commons.net.ftp.FTPReply;
importcommon.Logger;

{
privatestaticLoggerlog=Logger.getLogger(FtpUtilsImpl.class);
FTPClientftpClient=null;
Integerreply=null;

@Override
publicFTPClientloginFtp(Stringhostname,Integerport,Stringusername,Stringpassword){
ftpClient=newFTPClient();
try{
ftpClient.connect(hostname,port);
ftpClient.login(username,password);
ftpClient.setControlEncoding("utf-8");
reply=ftpClient.getReplyCode();
ftpClient.setDataTimeout(60000);
ftpClient.setConnectTimeout(60000);
//設置文件類型為二進制(避免解壓縮文件失敗)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//開通數據埠傳輸數據,避免阻塞
ftpClient.enterLocalActiveMode();
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
log.error("連接FTP失敗,用戶名或密碼錯誤");
}else{
log.info("FTP連接成功");
}
}catch(Exceptione){
if(!FTPReply.isPositiveCompletion(reply)){
try{
ftpClient.disconnect();
}catch(IOExceptione1){
log.error("登錄FTP失敗,請檢查FTP相關配置信息是否正確",e1);
}
}
}
returnftpClient;
}

@Override
@SuppressWarnings("resource")
(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName){
booleanflag=false;
ftpClient=loginFtp(hostname,port,username,password);
BufferedInputStreambuffer=null;
try{
buffer=newBufferedInputStream(newFileInputStream(localpath+fileName));
ftpClient.changeWorkingDirectory(fpath);
fileName=newString(fileName.getBytes("utf-8"),ftpClient.DEFAULT_CONTROL_ENCODING);
if(!ftpClient.storeFile(fileName,buffer)){
log.error("上傳失敗");
returnflag;
}
buffer.close();
ftpClient.logout();
flag=true;
returnflag;
}catch(Exceptione){
e.printStackTrace();
}finally{
closeFtpConnection(ftpClient,null,buffer);
log.info("文件上傳成功");
}
returnfalse;
}

@Override
publicbooleandownloadFileList(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName,Stringfilenames){
ftpClient=loginFtp(hostname,port,username,password);
booleanflag=false;
=null;
if(fpath.startsWith("/")&&fpath.endsWith("/")){
try{
//切換到當前目錄
this.ftpClient.changeWorkingDirectory(fpath);
this.ftpClient.enterLocalActiveMode();
FTPFile[]ftpFiles=this.ftpClient.listFiles();
for(FTPFilefiles:ftpFiles){
if(files.isFile()){
System.out.println("=================="+files.getName());
FilelocalFile=newFile(localpath+"/"+files.getName());
bufferRead=newBufferedOutputStream(newFileOutputStream(localFile));
ftpClient.retrieveFile(files.getName(),bufferRead);
bufferRead.flush();
}
}
ftpClient.logout();
flag=true;

}catch(IOExceptione){
e.printStackTrace();
}finally{
closeFtpConnection(ftpClient,bufferRead,null);
log.info("文件下載成功");
}
}
modifiedLocalFileName(localpath,fileName,filenames);
returnflag;
}

@Override
(Stringlocalpath,StringfileName,Stringfilenames){
Filefile=newFile(localpath);
File[]fileList=file.listFiles();
if(file.exists()){
if(null==fileList||fileList.length==0){
log.error("文件夾是空的");
}else{
for(Filedata:fileList){
Stringorprefix=data.getName().substring(0,data.getName().lastIndexOf("."));
Stringprefix=fileName.substring(0,fileName.lastIndexOf("."));
System.out.println("index==="+orprefix+"prefix==="+prefix);
if(orprefix.contains(prefix)){
booleanf=data.renameTo(newFile(localpath+"/"+filenames));
System.out.println("f============="+f);
}else{
log.error("需要重命名的文件不存在,請檢查。。。");
}
}
}
}
}


@Override
publicvoidcloseFtpConnection(FTPClientftpClient,,BufferedInputStreambuffer){
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(null!=bufferRead){
try{
bufferRead.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(null!=buffer){
try{
buffer.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}


publicstaticvoidmain(String[]args)throwsIOException{
Stringhostname="xx.xxx.x.xxx";
Integerport=21;
Stringusername="edwftp";
Stringpassword="edwftp";
Stringfpath="/etl/etldata/back/";
StringlocalPath="C:/Users/Administrator/Desktop/ftp下載/";
StringfileName="test.txt";
Stringfilenames="ok.txt";
FtpUtilsImplftp=newFtpUtilsImpl();
/*ftp.modifiedLocalFileName(localPath,fileName,filenames);*/
ftp.downloadFileList(hostname,port,username,password,fpath,localPath,fileName,filenames);
/*ftp.uploadLocalFilesToFtp(hostname,port,username,password,fpath,localPath,fileName);*/
/*ftp.modifiedLocalFileName(localPath);*/
}
}

② PTF的具體製作方法

我簡單的說下,首先要准備的:Serv-U FTP

1、建立第一個本地FTP伺服器(第一次運行程序,它會彈出設置向導窗口,根據提示操作),在需要輸入域名的地方寫入你的域名,沒有就隨便填個

2、既然我們已經建立好FTP伺服器,那麼我們就應該可以用FTP客戶端軟體來嘗試登錄。
怎麼辦呢?其實很簡單,我們不用上網,就可以測試。我們知道,不上網時,本地機的IP地址默認就為127.0.0.1,FTP埠號為21。打開FTP客戶端軟體,我就用FlashFXP來說明,打開快速連接,填入相應內容
3、Serv-U管理員中的各項設置。

以上我簡單說的,多了您也不會看完的撒。設置好了管理員中的各項設置,就可以使用了!

SERV-U FTP的歡迎詞設置
讓你的SERV-U FTP看起來更專業一點
現在很多的朋友都用SERV-U做個人FTP的伺服器,有關如何使用SERV-U架設伺服器的文章很多了,這兒我就不多說了。不過大家不知道注意到了沒有,當你登陸很多FTP的時候,會顯示一些歡迎信息,比如說顯示你的IP,告訴你目前有多少人在使用FTP,帶寬是多少等等。。。看起來就比較的專業樣子。其實你自己也是可以做的,SERV-U這個軟體本身就有這個功能。下面我就說明以下如何在自己的FTP裡面加上這些信息。

第一、先建立一個文本文件,隨便取一個名字。我們這兒就取message.txt吧。

第二、這個這個文本文件裡面加上這些文字

註:以上為區域網FTP架設方法!

③ 大哥 ,採用ftpclient.storeFile(String, Inputstream)將流寫到伺服器,沒報錯但伺服器上沒有文件,這個問

FTP協議有兩種工作方式:PORT方式和PASV方式,中文意思為主動式和被動式。 PORT(主動)方式的連接過程是:客戶端向伺服器的FTP埠(默認是21)發送連接請 求,伺服器接受連接,建立一條命令鏈路。當需要傳送數據時,客戶端在命令鏈路上用PORT 命令告訴伺服器:「我打開了XXXX埠,你過來連接我」。於是伺服器從20埠向客戶端的 XXXX埠發送連接請求,建立一條數據鏈路來傳送數據。 PASV(被動)方式的連接過程是:客戶端向伺服器的FTP埠(默認是21)發送連接請 求,伺服器接受連接,建立一條命令鏈路。當需要傳送數據時,伺服器在命令鏈路上用PASV 命令告訴客戶端:「我打開了XXXX埠,你過來連接我」。於是客戶端向伺服器的XXXX埠 發送連接請求,建立一條數據鏈路來傳送數據。

我當時因為連的是別人的伺服器,那邊換了伺服器就出現這問題,後來我通過FTPClient有一個ftpclient.enterLocalPassiveMode()方法,設置後就沒有這問題了,不知道你是不是跟我一樣

熱點內容
阿里雲伺服器鏡像市場 發布:2025-02-01 09:46:04 瀏覽:524
任子行伺服器管理口默認地址 發布:2025-02-01 09:42:58 瀏覽:995
設備作為FTP客戶端時 發布:2025-02-01 09:35:07 瀏覽:935
安卓如何登錄ios明日之後 發布:2025-02-01 09:31:59 瀏覽:305
怎麼查看手機存儲卡 發布:2025-02-01 09:31:51 瀏覽:340
java知識點總結 發布:2025-02-01 09:08:32 瀏覽:684
如何在手機版給伺服器加光影 發布:2025-02-01 09:02:14 瀏覽:727
簡單神器安卓系統的哪個好 發布:2025-02-01 09:00:48 瀏覽:354
社保卡密碼如何異地改密碼 發布:2025-02-01 08:57:22 瀏覽:33
什麼安卓平板最好能開120幀 發布:2025-02-01 08:55:58 瀏覽:380