網站首頁 編程語言 正文
1.通過用FTP進行上傳文件,首先要實現建立FTP連接,一般建立FTP連接,需要知道FTP配置有關的信息。一般要在Bean中建立一個ServiceFileInfo.cs文件進行記錄,一般需要FTP地址、登錄用戶名和登錄密碼。然后通過其他頁面進行訪問讀取。代碼樣式如下:
class ServiceFileInfo
{
// service1
public static string txtFilePath = @"ftp://12.128.128.01/FileName/";
//userid & password
public static string txtUID = "username";
public static string txtPWD = "password";
}
2.通過主方法讀取Bean文件下面的的ServiceFileInfo.cs文件的信息,去實現建立FTP連接。這里還需要清楚的知道你上傳文件的路徑(Path)和文件名稱(FileName)。根據這些信息主方法去調用寫著Bean中的另外一個ftpOperation.cs 文件(這個.cs文件中主要寫一些關于FTP的操作方法),進行FTP訪問操作。
- 主方法調用FTP操作代碼
ExecutionResult exeRes = this.ftpOperation.UploadFile(textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txt為文件的后綴名
- ?Bean文件中ftpOperation.cs文件關于FTP操作的方法
public ExecutionResult UploadFile(string vIMSPath, string vUID, string vPassword, string vLocalPath)
{
ExecutionResult result = new ExecutionResult();
result = connectState(vIMSPath, vUID, vPassword, vLocalPath);//調用下面代碼方法
if (result.Status)
{
File.Delete(vLocalPath);
}
return result;
}
connectState()方法
public static ExecutionResult connectState(string vIMSPath, string vUID, string vPassword, string fileName)
{
string operater = "";
bool Flag = false;
ExecutionResult result;
result = new ExecutionResult();
lock (lockObj)
{
try
{
operater = "Connet to FTP";
FTPOperation ftp = new FTPOperation(new Uri(vIMSPath), vUID, vPassword);
operater = "Upload file";
Flag = ftp.UploadFile(fileName, Path.GetFileName(fileName), true);
if (Flag)
{
result.Status = true;
result.Message = "Send to server OK";
}
}
catch (Exception ex)
{
result.Status = false;
result.Anything = "Mail";
result.Message = operater + ":" + ex.Message;
}
}
return result;
}
- UploadFile()方法
public bool UploadFile(string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile)
{
bool result;
try
{
bool flag = !this.IsValidFileChars(RemoteFileName) || !this.IsValidFileChars(Path.GetFileName(LocalFullPath)) || !this.IsValidPathChars(Path.GetDirectoryName(LocalFullPath));
if (flag)
{
throw new Exception("非法文件名或目錄名!");
}
bool flag2 = File.Exists(LocalFullPath);
if (!flag2)
{
throw new Exception("本地文件不存在!");
}
FileStream fileStream = new FileStream(LocalFullPath, FileMode.Open, FileAccess.Read);
byte[] array = new byte[fileStream.Length];
fileStream.Read(array, 0, (int)fileStream.Length);
fileStream.Close();
result = this.UploadFile(array, RemoteFileName, OverWriteRemoteFile);
}
catch (Exception ex)
{
this.ErrorMsg = ex.ToString();
throw ex;
}
return result;
}
public bool UploadFile(byte[] FileBytes, string RemoteFileName)
{
bool flag = !this.IsValidFileChars(RemoteFileName);
if (flag)
{
throw new Exception("非法文件名或目錄名!");
}
return this.UploadFile(FileBytes, RemoteFileName, false);
}
public bool UploadFile(byte[] FileBytes, string RemoteFileName, bool OverWriteRemoteFile)
{
bool result;
try
{
bool flag = !this.IsValidFileChars(RemoteFileName);
if (flag)
{
throw new Exception("非法文件名!");
}
bool flag2 = !OverWriteRemoteFile && this.FileExist(RemoteFileName);
if (flag2)
{
throw new Exception("FTP服務上面已經存在同名文件!");
}
this.Response = this.Open(new Uri(this.Uri.ToString() + RemoteFileName), "STOR");
Stream requestStream = this.Request.GetRequestStream();
MemoryStream memoryStream = new MemoryStream(FileBytes);
byte[] array = new byte[1024];
int num = 0;
for (;;)
{
int num2 = memoryStream.Read(array, 0, array.Length);
bool flag3 = num2 == 0;
if (flag3)
{
break;
}
num += num2;
requestStream.Write(array, 0, num2);
}
requestStream.Close();
this.Response = (FtpWebResponse)this.Request.GetResponse();
memoryStream.Close();
memoryStream.Dispose();
FileBytes = null;
result = true;
}
catch (Exception ex)
{
this.ErrorMsg = ex.ToString();
throw ex;
}
return result;
}
原文鏈接:https://www.cnblogs.com/wml-it/p/12881936.html
相關推薦
- 2022-04-05 MAC中執行.sh腳本,/bin/sh^M: bad interpreter:解決辦法
- 2022-08-06 C語言實現簡單登錄操作_C 語言
- 2022-03-30 ASP.NET?Core使用JWT自定義角色并實現策略授權需要的接口_實用技巧
- 2022-09-24 python?繪制3D圖案例分享_python
- 2023-08-01 el-table 拖拽列寬出現空白列問題
- 2022-08-17 R語言學習VennDiagram包繪制韋恩圖示例_R語言
- 2022-08-11 Python操作數據庫之數據庫編程接口_python
- 2022-08-26 C++超集C++/CLI模塊的基本語法_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支