C#客户端直接从服务器下载文件到本地

时间:2021-10-28 17:52:52

1、局域网文件下载:

C#客户端直接从服务器下载文件到本地C#客户端直接从服务器下载文件到本地
 1 public class RemoteDownload
 2     {
 3         public static void DownLoad(string addressUrl,string localName)
 4         {
 5             //下载文件
 6             System.Net.WebClient myWebClient = new System.Net.WebClient();
 7             myWebClient.DownloadFile(@"/10.2.0.254/software/01279.lic.txt", "testdownload.txt");            
 8             //下载end
 9         }
10     }
View Code

2、通过web方式,从远程服务器端下载文件:

C#客户端直接从服务器下载文件到本地C#客户端直接从服务器下载文件到本地
 1 public class WebDownload
 2     {
 3         public static void DownLoad(string Url, string FileName)
 4         {
 5             bool Value = false;
 6             WebResponse response = null;
 7             Stream stream = null;
 8 
 9             try
10             {
11                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
12 
13                 response = request.GetResponse();
14                 stream = response.GetResponseStream();
15 
16                 if (!response.ContentType.ToLower().StartsWith("text/"))
17                 {
18                     Value = SaveBinaryFile(response, FileName);
19 
20                 }
21 
22             }
23             catch (Exception err)
24             {
25                 string aa = err.ToString();
26             }
27 
28         }
29 
30         /// <summary>
31         /// Save a binary file to disk.
32         /// </summary>
33         /// <param name="response">The response used to save the file</param>
34         // 将二进制文件保存到磁盘
35         private static bool SaveBinaryFile(WebResponse response, string FileName)
36         {
37             bool Value = true;
38             byte[] buffer = new byte[1024];
39 
40             try
41             {
42                 if (File.Exists(FileName))
43                     File.Delete(FileName);
44                 Stream outStream = System.IO.File.Create(FileName);
45                 Stream inStream = response.GetResponseStream();
46 
47                 int l;
48                 do
49                 {
50                     l = inStream.Read(buffer, 0, buffer.Length);
51                     if (l > 0)
52                         outStream.Write(buffer, 0, l);
53                 }
54                 while (l > 0);
55 
56                 outStream.Close();
57                 inStream.Close();
58             }
59             catch
60             {
61                 Value = false;
62             }
63             return Value;
64         }
View Code

3、从FTP上下载文件:

C#客户端直接从服务器下载文件到本地C#客户端直接从服务器下载文件到本地
 1  public class FtpDownload
 2     {
 3         public static void DownLoad(string FtpPath)
 4         {
 5             /*首先从配置文件读取ftp的登录信息*/
 6             string TempFolderPath = System.Configuration.ConfigurationManager.AppSettings["TempFolderPath"].ToString();
 7             string FtpUserName = System.Configuration.ConfigurationManager.AppSettings["FtpUserName"].ToString();
 8             string FtpPassWord = System.Configuration.ConfigurationManager.AppSettings["FtpPassWord"].ToString();
 9             string LocalFileExistsOperation = System.Configuration.ConfigurationManager.AppSettings["LocalFileExistsOperation"].ToString();
10 
11 
12             Uri uri = new Uri(FtpPath);
13             string FileName = Path.GetFullPath(TempFolderPath) + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(uri.LocalPath);
14 
15             //创建一个文件流
16             FileStream fs = null;
17             Stream responseStream = null;
18             try
19             {
20                 //创建一个与FTP服务器联系的FtpWebRequest对象
21                 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
22                 //设置请求的方法是FTP文件下载
23                 request.Method = WebRequestMethods.Ftp.DownloadFile;
24 
25                 //连接登录FTP服务器
26                 request.Credentials = new NetworkCredential(FtpUserName, FtpPassWord);
27 
28                 //获取一个请求响应对象
29                 FtpWebResponse response = (FtpWebResponse)request.GetResponse();
30                 //获取请求的响应流
31                 responseStream = response.GetResponseStream();
32 
33                 //判断本地文件是否存在,如果存在,则打开和重写本地文件
34 
35                 if (File.Exists(FileName))
36                 {
37                     if (LocalFileExistsOperation == "write")
38                     {
39                         fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite);
40 
41                     }
42                 }
43 
44                 //判断本地文件是否存在,如果不存在,则创建本地文件
45                 else
46                 {
47                     fs = File.Create(FileName);
48                 }
49 
50                 if (fs != null)
51                 {
52 
53                     int buffer_count = 65536;
54                     byte[] buffer = new byte[buffer_count];
55                     int size = 0;
56                     while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0)
57                     {
58                         fs.Write(buffer, 0, size);
59 
60                     }
61                     fs.Flush();
62                     fs.Close();
63                     responseStream.Close();
64                 }
65             }
66             finally
67             {
68                 if (fs != null)
69                     fs.Close();
70                 if (responseStream != null)
71                     responseStream.Close();
72             }
73 
74 
75         }
76     }
View Code