當前位置:首頁 » 文件傳輸 » 通過ftp創建文件
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

通過ftp創建文件

發布時間: 2022-04-17 04:43:19

『壹』 c# .net ftp方式創建文件夾

//上傳文件

public static Boolean FtpUpload(string ftpPath,string localFile)

{

//檢查目錄是否存在,不存在創建

FtpCheckDirectoryExist(ftpPath);

FileInfo fi = new FileInfo(localFile);

FileStream fs = fi.OpenRead();

long length = fs.Length;

FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpServerIP + ftpPath + fi.Name);

req.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

req.Method = WebRequestMethods.Ftp.UploadFile;

req.ContentLength = length;

req.Timeout = 10 * 1000;

try

{

Stream stream = req.GetRequestStream();

int BufferLength = 2048; //2K

byte[] b = new byte[BufferLength];

int i;

while ((i = fs.Read(b, 0, BufferLength)) > 0)

{

stream.Write(b, 0, i);

}

stream.Close();

stream.Dispose();

}

catch (Exception e)

{

ErrLog(e.Message + e.StackTrace);

return false;

}

finally

{

fs.Close();

req.Abort();

}

req.Abort();

return true;

}

//判斷文件的目錄是否存,不存則創建

public static void FtpCheckDirectoryExist(string destFilePath)

{

string fullDir = FtpParseDirectory(destFilePath);

string[] dirs = fullDir.Split('/');

string curDir = "/";

for (int i = 0; i < dirs.Length; i++)

{

string dir = dirs[i];

//如果是以/開始的路徑,第一個為空

if (dir != null && dir.Length > 0)

{

try

{

curDir += dir + "/";

FtpMakeDir(curDir);

}

catch (Exception)

{}

}

}

}

public static string FtpParseDirectory(string destFilePath)

{

return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));

}

//創建目錄

public static Boolean FtpMakeDir(string localFile)

{

FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpServerIP + localFile);

req.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

req.Method = WebRequestMethods.Ftp.MakeDirectory;

try

{

FtpWebResponse response = (FtpWebResponse)req.GetResponse();

response.Close();

}

catch (Exception)

{

req.Abort();

return false;

}

req.Abort();

return true;

}

『貳』 請問在DOS下進入FTP,用什麼命令可以在FTP裡面新建文件夾(目錄)啊

mkdir
dir-name:在遠程主機中建一目錄
如果你想學習使用進行後台FTP下載,那麼就必須學習FTP指令。
FTP的命令行格式為:
ftp
-v
-d
-i
-n
-g
[主機名]

其中
-v
顯示遠程伺服器的所有響應信息;
-n
限制ftp的自動登錄,即不使用;.n
etrc文件;
-d
使用調試方式;
-g
取消全局文件名。
FTP使用的內部命令如下(中括弧表示可選項):
1.![cmd[args>:在本地機中執行交互shell,exit回到ftp環境,如:!ls*.zip
2.$
macro-ame[args]:
執行宏定義macro-name。
3.account[password]:
提供登錄遠程系統成功後訪問系統資源所需的補充口令。
4.append
local-file[remote-file]:將本地文件追加到遠程系統主機,若未指定遠程系統文件名,則使用本地文件名。
5.ascii:使用ascii類型傳輸方式。
6.bell:每個命令執行完畢後計算機響鈴一次。
7.bin:使用二進制文件傳輸方式。
8.bye:退出ftp會話過程。
9.case:在使用mget時,將遠程主機文件名中的大寫轉為小寫字母。
10.cd
remote-dir:進入遠程主機目錄。
11.cp:進入遠程主機目錄的父目錄。
12.chmod
mode
file-name:將遠程主機文件file-name的存取方式設置為mode,如:chmod
777
a.out。
13.close:中斷與遠程伺服器的ftp會話(與open對應)。
14.cr:使用asscii方式傳輸文件時,將回車換行轉換為回行。
15.delete
remote-file:刪除遠程主機文件。
16.debug[debug-value]:設置調試方式,
顯示發送至遠程主機的每條命令,如:deb
up
3,若設為0,表示取消debug。
17.dir[remote-dir][local-file]:顯示遠程主機目錄,並將結果存入本地文件
18.disconnection:同close。
19.form
format:將文件傳輸方式設置為format,預設為file方式。
20.get
remote-file[local-file]:
將遠程主機的文件remote-file傳至本地硬碟的local-file。
21.glob:設置mdelete,mget,mput的文件名擴展,預設時不擴展文件名,同命令行的-g參數。
22.hash:每傳輸1024位元組,顯示一個hash符號(#)。
23.help[cmd]:顯示ftp內部命令cmd的幫助信息,如:help
get。
24.idle[seconds]:將遠程伺服器的休眠計時器設為[seconds]秒。
25.image:設置二進制傳輸方式(同binary)。
26.lcd[dir]:將本地工作目錄切換至dir。
27.ls[remote-dir][local-file]:顯示遠程目錄remote-dir,
並存入本地文件local-file。
28.macdef
macro-name:定義一個宏,遇到macdef下的空行時,宏定義結束。
29.mdelete[remote-file]:刪除遠程主機文件。
30.mdir
remote-files
local-file:與dir類似,但可指定多個遠程文件,如
:mdir
*.o.*.zipoutfile

31.mget
remote-files:傳輸多個遠程文件。
32.mkdir
dir-name:在遠程主機中建一目錄。
33.mls
remote-file
local-file:同nlist,但可指定多個文件名。
34.mode[modename]:將文件傳輸方式設置為modename,
預設為stream方式。
35.modtime
file-name:顯示遠程主機文件的最後修改時間。
36.mput
local-file:將多個文件傳輸至遠程主機。
37.newer
file-name

『叄』 如何在ftp上建立自己的文件夾

如果你有許可權操作ftp的話,那麼就像操作windows一樣簡單。右鍵-新建文件夾。
不然就得求救網路管理員了

『肆』 如何在FTP里新建文件夾

確定許可權沒有問題?那你試試拉個本地文件夾上去,復制進去個文件夾跟新建個文件夾原理是一樣的

『伍』 win7 ftp怎麼建立文件夾

在系統中任何的文件夾都可以藉助於ftp服務成為共享文件夾,建立文件夾只需要在磁碟位置右鍵選擇」新建文件夾「即可,ftp共享的設置方法可以參考如下操作:

1、在win7上使用ftp服務,需要先安裝iis服務,打開 "控制面板",選擇"程序" -> "打開或關閉Windows資源",在彈出的窗體里找到 「Internet信息服務」,展開後選擇「Ftp伺服器",然後點擊"確定",此時Windows開始更新功能資源列表。

2、然後,自己在IIS管理器中添加FTP站點

在彈出的窗口,選擇ftp共享目錄即可。

『陸』 怎麼在FTP建立文件夾

你要在根目錄里增加的。一般是www 或者wwwroot 或者是你域名的文件夾。還有要已bbs blog作為文件夾名,因為有的主機提供商是禁止使用這兩個的。如果還不成功就咨詢下你的服務提供商,看是不是沒有新建文件夾的許可權。

『柒』 FTP怎麼遠程新建文件夾

將ftp 連上點右鍵新建文件夾即可
或者將文件嘉建好用ftp船上去

『捌』 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);*/
}
}

『玖』 怎樣在windows系統下使用ftp登陸linux創建文件

"景安網路-快雲VPS"為您解答:
首先,你得配置好ftp帳號許可權,比如有創建文件許可權(詳細設置可以網路搜「VPS教程」去「景安網路」支持的vps技術教程站看,裡面有相關圖文教程);
然後,用flashfxp等軟體登錄伺服器,關閉selinux,被動模式連接。
或者你可以用CMD命令行形式登錄:lftp
用戶名:密碼@ftp地址:傳送埠(默認21)連接成功,再用創建文件命令touch(當然帳號必須有創建文件的許可權)

『拾』 批處理通過ftp在伺服器上新建文件夾和文件

批處理實現你要的功能需要兩個文件
舉例
第一個:
在c盤新建一個文本文件名為 1.txt,寫入以下空行之間的內容

open 192.168.1.241
root
abcde
rmdir 1
mkdir 1
by

其中第一行open後面的地址為你的ftp伺服器的ip
第二行為登錄用戶名,第三行為登錄對應密碼
第四行為要執行的命令,rmdir是刪除名為1的目錄
第五行為要執行的命令,mkdir是新建名為1的目錄
第六行為退出ftp。
注意,你要執行什麼命令,可以在密碼之後,by之前任意添加行。
第二個文件為bat文件:
寫入以下空行之間的內容

ftp -s:1.txt

執行該bat則自動登錄到你指定的ftp伺服器,創建目錄,注意,rmdir是一個刪除的範例,那句可以不要。
如果要新建文件,可在本地新建2.txt後,使用put的方式變通
在1.txt中密碼後,by前加入

asc
put 2.txt

執行bat後就會在伺服器上新建2.txt,asc為指定以文本方式傳送文件,如果是2進制文件,請換成bin