c#連接ftp服務器,連接獲取檔案夾的檔案串列成功,下載單個檔案和獲取檔案大小回傳都回傳550,路徑正確,主動被動都失敗,用FTP工具則下載正常,求助
uj5u.com熱心網友回復:
多半還是路徑問題,檢查下大小寫,斜杠,空格之類的uj5u.com熱心網友回復:
配的路徑可以獲取檔案串列,但是路徑+檔案名用來獲取檔案大小和下載都出錯了uj5u.com熱心網友回復:
嘗試用在地址里加上你的用戶名密碼看下
比如
ftp://用戶名:密碼@地址:埠/路徑
uj5u.com熱心網友回復:
用地址打不開,用了ftpclient類,發現執行命令RETR 路徑也是550,但是OpenRead讀檔案就可以讀取檔案,不解!uj5u.com熱心網友回復:
配的路徑可以獲取檔案串列,但是路徑+檔案名用來獲取檔案大小和下載都出錯了 多半還是路徑問題,檢查下大小寫,斜杠,空格之類的
嘗試用在地址里加上你的用戶名密碼看下
比如
ftp://用戶名:密碼@地址:埠/路徑
uj5u.com熱心網友回復:
請問ftp執行下載或獲取檔案流是同一個命令嗎
配的路徑可以獲取檔案串列,但是路徑+檔案名用來獲取檔案大小和下載都出錯了 多半還是路徑問題,檢查下大小寫,斜杠,空格之類的
嘗試用在地址里加上你的用戶名密碼看下
比如
ftp://用戶名:密碼@地址:埠/路徑
下載檔案和獲取檔案流其實是一個東西吧
最終是需要GET命令來實際下載
uj5u.com熱心網友回復:
請問用c# FtpWebRequest類只能獲取到檔案串列,下載就550了,用cmd去get又正常,這是什么原因uj5u.com熱心網友回復:
請問ftp執行下載或獲取檔案流是同一個命令嗎
配的路徑可以獲取檔案串列,但是路徑+檔案名用來獲取檔案大小和下載都出錯了 多半還是路徑問題,檢查下大小寫,斜杠,空格之類的
嘗試用在地址里加上你的用戶名密碼看下
比如
ftp://用戶名:密碼@地址:埠/路徑
下載檔案和獲取檔案流其實是一個東西吧
最終是需要GET命令來實際下載
uj5u.com熱心網友回復:
求教
請問ftp執行下載或獲取檔案流是同一個命令嗎
配的路徑可以獲取檔案串列,但是路徑+檔案名用來獲取檔案大小和下載都出錯了 多半還是路徑問題,檢查下大小寫,斜杠,空格之類的
嘗試用在地址里加上你的用戶名密碼看下
比如
ftp://用戶名:密碼@地址:埠/路徑
下載檔案和獲取檔案流其實是一個東西吧
最終是需要GET命令來實際下載
uj5u.com熱心網友回復:
我有這個類,挺好用的uj5u.com熱心網友回復:
using System;using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace ScheduleTask
{
public class FtpTool
{
private static string ftpUri = ConfigurationManager.AppSettings["FtpUri"];
private static string folderPath = ConfigurationManager.AppSettings["ScheduleTaskFolder"];
private static string ftpFullDirectoryPath = Path.Combine(ftpUri, folderPath);//Ftp存盤檔案所在檔案夾入徑
private static string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"];
private static string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];
public static bool Upload(string fileFullPath)
{
bool isSuccess = false;
FileInfo fileinfo = new FileInfo(fileFullPath);
string uri = Path.Combine(ftpFullDirectoryPath, fileinfo.Name);//Ftp存盤檔案全入徑
if (!MakeDir(ftpFullDirectoryPath)) return isSuccess;//若檔案夾不存在則創建
try
{
var tempPath = Download(uri, fileinfo.Name);
if (tempPath != null)
{
System.Diagnostics.FileVersionInfo fileVersion1 = System.Diagnostics.FileVersionInfo.GetVersionInfo(fileFullPath);
System.Diagnostics.FileVersionInfo fileVersion2 = System.Diagnostics.FileVersionInfo.GetVersionInfo(tempPath);
Version v1 = new Version(fileVersion1.FileVersion != null ? fileVersion1.FileVersion : "0.0");
Version v2 = new Version(fileVersion2.FileVersion != null ? fileVersion1.FileVersion : "0.0");
if (v1 < v2) return isSuccess = true;
DeleteFile(uri);//若ftp存在版本低于當前上傳檔案版本,則洗掉ftp檔案
}
isSuccess = true;
}
catch (Exception ex)
{
throw ex;
}
if (!isSuccess) return false;
else isSuccess = false;
FtpWebRequest ftpReq;
ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftpReq.KeepAlive = false;
ftpReq.Proxy = null;
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.UseBinary = true;
ftpReq.ContentLength = fileinfo.Length;
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int contentLen = 0;
FileStream fs = fileinfo.OpenRead();
Stream stream = null;
try
{
stream = ftpReq.GetRequestStream();
contentLen = fs.Read(buffer, 0, bufferLength);
while (contentLen != 0)
{
stream.Write(buffer, 0, bufferLength);
contentLen = fs.Read(buffer, 0, bufferLength);
}
stream.Close();
fs.Close();
fs.Close();
isSuccess = true;
}
catch (Exception ex)
{
if (stream != null) stream.Close();
if (fs != null) fs.Close();
throw ex;
}
return isSuccess;
}
public static string Download(string fileFullPath, string fileName)
{
var basePath = System.AppDomain.CurrentDomain.BaseDirectory;
var tempDirectory = basePath.Substring(0,basePath.LastIndexOf(@"\")+1)+ "TempDirectory\\";
if (!Directory.Exists(tempDirectory)) Directory.CreateDirectory(tempDirectory);
var tempFileFullPath = tempDirectory + fileName;
if (File.Exists(tempFileFullPath)) File.Delete(tempFileFullPath);
Stream stream = null;
FileStream outPutStream = new FileStream(tempFileFullPath, FileMode.Create);
FtpWebRequest ftpReq = null;
FtpWebResponse ftpres = null;
ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileFullPath));
ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftpReq.KeepAlive = false;
ftpReq.Proxy = null;
ftpReq.Method = WebRequestMethods.Ftp.DownloadFile;
try
{
ftpres = (FtpWebResponse)ftpReq.GetResponse();
stream = ftpres.GetResponseStream();
long cl = ftpres.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = stream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outPutStream.Write(buffer, 0, bufferSize);
readCount = stream.Read(buffer, 0, bufferSize);
}
stream.Close();
outPutStream.Close();
ftpres.Close();
}
catch (Exception ex)
{
tempFileFullPath = null;
if (stream != null) stream.Close();
if (outPutStream != null) outPutStream.Close();
if (ftpres != null) ftpres.Close();
}
return tempFileFullPath;
}
//判斷目錄在ftp中是否存在
public static bool RemoteFtpDirExists(string path)
{
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
reqFtp.UseBinary = true;
reqFtp.KeepAlive = false;
reqFtp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse resFtp = null;
try
{
resFtp = (FtpWebResponse)reqFtp.GetResponse();
FtpStatusCode code = resFtp.StatusCode;//OpeningData
resFtp.Close();
return true;
}
catch (Exception ex)
{
if (resFtp != null)
{
resFtp.Close();
}
return false;
}
}
//在ftp中創建目錄
public static bool MakeDir(string dirName)
{
try
{
bool b = RemoteFtpDirExists(dirName);
if (b)
{
return true;
}
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(dirName));
reqFtp.UseBinary = true;
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFtp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
response.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
public string[] GetFileList(string url, string method)
{
url = ftpUri + url;
string[] downloadFiles;
StringBuilder result = new StringBuilder();
try
{
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFtp.UseBinary = true;
reqFtp.KeepAlive = false;
reqFtp.Method = method;
reqFtp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
WebResponse response = reqFtp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);//中文檔案名
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append(" ");
line = reader.ReadLine();
}
if (result.Length == 0) return null;
result.Remove(result.ToString().LastIndexOf(' '), 1);
reader.Close();
response.Close();
return result.ToString().Split(' ');
}
catch (Exception ex)
{
downloadFiles = null;
return downloadFiles;
}
}
public static void DeleteFile(string fileName)
{
FtpWebRequest ftpreq = (FtpWebRequest)WebRequest.Create(fileName);
ftpreq.UseBinary = true;
ftpreq.Method = WebRequestMethods.Ftp.DeleteFile;
ftpreq.KeepAlive = false;
ftpreq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse resFtp = null;
try
{
resFtp = (FtpWebResponse)ftpreq.GetResponse();
resFtp.Close();
}
catch (Exception ex)
{
if (resFtp != null)
{
resFtp.Close();
}
throw ex;
}
}
public static bool DeleteDirectory(string directoryName)
{
var uri = ftpUri + directoryName;
FtpWebRequest ftpreq = (FtpWebRequest)WebRequest.Create(uri);
ftpreq.UseBinary = true;
ftpreq.Method = WebRequestMethods.Ftp.DeleteFile;
ftpreq.KeepAlive = false;
ftpreq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse resFtp = null;
try
{
resFtp = (FtpWebResponse)ftpreq.GetResponse();
resFtp.Close();
return true;
}
catch (Exception)
{
if (resFtp != null)
{
resFtp.Close();
}
return fals
uj5u.com熱心網友回復:
你好 朋友 請問解決了嗎 download 下載失敗的問題轉載請註明出處,本文鏈接:https://www.uj5u.com/net/228164.html
標籤:C#
