如何在FTP服务器上传文件和加密 [英] How to upload files and encrypt in FTP server

查看:277
本文介绍了如何在FTP服务器上传文件和加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已编写下面的代码将文件从本地系统复制到ftp服务器,同样我需要选择多个文件并将其上传到服务器中。当我上传文件时,它应该被转换为加密文件,任何建议,我也写了加密代码。



我尝试过:



公共类WebRequestGetExample 
{
public static void Main()
{
//获取用于与服务器通信的对象。
FtpWebRequest request =(FtpWebRequest)WebRequest.Create(ftp://192.1....../ftp.txt); request.Method = WebRequestMethods.Ftp.UploadFile;
//此示例假定FTP站点使用匿名登录。
request.Credentials = new NetworkCredential(username,password);
//将文件内容复制到请求流。
byte [] fileContents;
使用(StreamReader sourceStream = new StreamReader(E:\\Anusha \\\\\ [@])
{
fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd) ());
}
request.ContentLength = fileContents.Length;
using(Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents,0,fileContents.Length);
}
使用(FtpWebResponse response =(FtpWebResponse)request.GetResponse())
{
Console.WriteLine(上传文件完成,状态{0},response.StatusDescription );
}
}
}

解决方案

WebClient 提供简单易用使用FTP上传和下载的方法,请参阅: c# - 从上传文件和下载文件FTP - 堆栈溢出 [ ^ ]

您可以使用.NET中的 System.Security.Cryptography 命名空间进行加密。

这是一篇关于CodeProject加密的好文章: C#中的Swanky加密/解密 [ ^ ]

have written below code to copy file from local system to ftp server , similarly i need to select multiple files and upload them in server . While i upload the files it should be converted as encrypted files, any suggestions, i have written code for encryption also.

What I have tried:

public class WebRequestGetExample  
{  
public static void Main ()  
{  
// Get the object used to communicate with the server.  
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://192.1....../ftp.txt"); request.Method =WebRequestMethods.Ftp.UploadFile;  
// This example assumes the FTP site uses anonymous logon.  
request.Credentials = new NetworkCredential("username", "password");  
// Copy the contents of the file to the request stream.  
byte[] fileContents;  
using (StreamReader sourceStream = new StreamReader("E:\\Anusha\\ftp.txt"))  
{  
fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());  
}  
request.ContentLength = fileContents.Length;  
using (Stream requestStream = request.GetRequestStream())  
{  
requestStream.Write(fileContents, 0, fileContents.Length);  
}  
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())  
{  
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);  
}  
}  
}

解决方案

WebClient offers an easy way to upload and download using FTP, see: c# - Upload file and download file from FTP - Stack Overflow[^]
You can encrypt using the System.Security.Cryptography namespace in .NET.
Here is a good article about encryption on CodeProject: Swanky Encryption/Decryption in C#[^]


这篇关于如何在FTP服务器上传文件和加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆