當前位置:首頁 » 文件管理 » 遍歷ftp的目錄

遍歷ftp的目錄

發布時間: 2025-05-10 07:35:00

㈠ c# 如何遍歷ftp文件夾下的文件

ftp文件是一樣的
只不過路徑的格式不一樣而已

這個文件太長了 給我信箱給你發過去吧
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;

namespace zhangyuk.net.csdn.blog.ftpclient
{
/// <summary>
/// FTP Client
/// </summary>
public class FTPClient
{
#region 構造函數
/// <summary>
/// 預設構造函數
/// </summary>
public FTPClient()
{
strRemoteHost = " ";
strRemotePath = " ";
strRemoteUser = " ";
strRemotePass = " ";
strRemotePort = 21;
bConnected = false;
}

/// <summary>
/// 構造函數
/// </summary>
/// <param name= "remoteHost "> </param>
/// <param name= "remotePath "> </param>
/// <param name= "remoteUser "> </param>
/// <param name= "remotePass "> </param>
/// <param name= "remotePort "> </param>
public FTPClient( string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort )
{
strRemoteHost = remoteHost;
strRemotePath = remotePath;
strRemoteUser = remoteUser;
strRemotePass = remotePass;
strRemotePort = remotePort;
Connect();
}
#endregion

#region 登陸
/// <summary>
/// FTP伺服器IP地址
/// </summary>
private string strRemoteHost;
public string RemoteHost
{
get
{
return strRemoteHost;
}
set
{
strRemoteHost = value;
}
}
/// <summary>
/// FTP伺服器埠
/// </summary>
private int strRemotePort;
public int RemotePort
{
get
{
return strRemotePort;
}
set
{
strRemotePort = value;
}
}
/// <summary>
/// 當前伺服器目錄
/// </summary>
private string strRemotePath;
public string RemotePath
{
get
{
return strRemotePath;
}
set
{
strRemotePath = value;
}
}
/// <summary>
/// 登錄用戶賬號
/// </summary>
private string strRemoteUser;
public string RemoteUser
{
set
{
strRemoteUser = value;
}
}
/// <summary>
/// 用戶登錄密碼
/// </summary>
private string strRemotePass;
public string RemotePass
{
set
{
strRemotePass = value;
}
}

/// <summary>
/// 是否登錄
/// </summary>
private Boolean bConnected;
public bool Connected
{
get
{
return bConnected;
}
}
#endregion

㈡ 請問一下,java中有沒直接判斷ftp上文件夾下是否存在某文件的方法通過遍歷文件夾的方式判斷太耗內存了

第一個種方法 :
org.apache.commons.net.ftp.* 看這個目錄下是否有你要的方法
第二種方法:

package com.soft4j.log4j;

import java.io.IOException;

import sun.net.ftp.FtpClient;

public class FtpTest
{
static String middle_ftpServer = "10.103.2.250";
static String middle_user = "ora9iftp";
static String middle_password = "ftp";
static String middle_dir = "/image/NWKPHOTO/Middle/2009/3";

public static void main(String[] args)
{
FtpClient ftpClient = new FtpClient();
try
{
ftpClient.openServer(middle_ftpServer);
ftpClient.login(middle_user, middle_password);
FtpTest ft = new FtpTest();
ft.isDirExist(ftpClient, middle_dir);
} catch (IOException e)
{
e.printStackTrace();
}

}

/** 判斷Ftp目錄是否存在,如果不存在則創建目錄 */
public void isDirExist(FtpClient ftpClient, String dir)
{
try
{
ftpClient.cd(dir); //想不到什麼好辦法來判斷目錄是否存在,只能用異常了(比較笨).請知道的告訴我一聲`
} catch (IOException e1)
{
ftpClient.sendServer("MKD " + dir + "\r\n");
try
{
ftpClient.readServerResponse();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}

㈢ java遍歷ftp文件夾時,在FTPFile ff[] = ftpClient.listFiles()處一直提示空指針異常錯誤,是怎麼回事。

boolean success = false; 
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
//如果採用默認埠,可以使用ftp.connect(url)的方式直接連接FTP伺服器
ftp.login(username, password);//登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);//轉移到FTP伺服器目錄
FTPFile[] fs = ftp.listFiles();

㈣ ftp遍歷目錄的問題

你這個ftp是用的都是同一個對象,每次遞歸workfolder都被update掉了。。。。當然會不繼續遍歷,改成每次都Generate一個New的ftp吧。
補充:
本來你ftp指向目錄A,後來進入遞歸被改成指向A/B了,你說遍歷還能准確嗎?
還有個方法就是每次遞歸回來調用:
ftp.changeWorkingDirectory(ftpPath);
把Path給設置回來。不過不確定這個方法穩定,可以先Try一下。
public void listFilesDir(String path) throws IOException{
String ftpPath = path;
ftp.changeWorkingDirectory(ftpPath);
FTPFile[] files = ftp.listFiles();
for(FTPFile ff:files){
if(!ff.isDirectory()){
System.out.println("文件:" + ff.getName());
}
else{
if(!ff.getName().startsWith(".")){
ftpPath = ff.getName() + "/";
System.out.println("目錄 " + ff.getName() + " 下的文件文件或目錄:");
ftp.changeWorkingDirectory(ftpPath);
listFilesDir(ftpPath);
ftp.changeWorkingDirectory(path);
}
}
}
}
再補充:
中文的話試試看用GBK。。。Java項目的編碼記得也要一樣的。

㈤ C#如何實現FTP在伺服器上遍歷文件夾

.../Image這個主文件夾是一定的,然後摟住是想在這個文件夾下創建文件夾
事先,你已經知道你所要創建的文件夾的名稱,可以這么做
if (!(Directory.Exists(path+文件名))//用這個方法判斷是否有這個文件夾。比你去遍歷文件夾要效率的多
Directory.CreateDirectory(path+文件名);//不存在則創建

php如何遍歷指定文件夾,獲取所有文件列表並生成下載鏈接

試編寫代碼如下:

<?php

$dir="D:/WWW/ftp";//指定的路徑
$sitepath='http://localhost/ftp/';
//遍歷文件夾下所有文件
if(false!=($handle=opendir($dir))){
echo"$dir目錄下的文件列表:<BR/>";
$i=0;
while(false!==($file=readdir($handle))){
if($file!="."&&$file!=".."&&!is_dir($dir.'/'.$file)){
echo'<ahref="'.$sitepath.$file.'">'.$file.'</a><br/>';
}
}
//關閉句柄
closedir($handle);
}

?>


代碼中需要提示的是:


如果是運行於互聯網上,需要考慮文件的訪問安全性。


運行截圖:


熱點內容
43魔獸世界POR腳本 發布:2025-05-10 10:06:15 瀏覽:730
群輝外網訪問nas 發布:2025-05-10 10:05:35 瀏覽:471
ftp記錄傳輸文件 發布:2025-05-10 09:56:53 瀏覽:560
社保的查詢密碼是什麼 發布:2025-05-10 09:51:49 瀏覽:619
php獲取ip的函數 發布:2025-05-10 09:43:48 瀏覽:152
金立怎麼刷機解鎖密碼 發布:2025-05-10 09:43:45 瀏覽:761
陶藝訪問學者 發布:2025-05-10 09:43:43 瀏覽:880
英語編譯和翻譯過程 發布:2025-05-10 09:34:35 瀏覽:344
搜狐視頻上傳視頻 發布:2025-05-10 09:34:35 瀏覽:647
共享存儲linux 發布:2025-05-10 09:34:32 瀏覽:153