1、
1 string serverFilePath ="http://192.168.1.222:9111/Doc/Test.docx"; 2 string serverFileName = string.Empty; 3 int nameIndex = serverFilePath.LastIndexOf("/"); 4 if (nameIndex > 0) 5 { 6 serverFileName = serverFilePath.Substring(nameIndex + 1); 7 } 8 string dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test"); 9 if (!Directory.Exists(dirPath)) 10 { 11 DirectoryInfo directoryInfo = new DirectoryInfo(dirPath); 12 directoryInfo.Create(); 13 } 14 string localPath = Path.Combine(dirPath,serverFileName);
2、
1 public void DownloadFile(string serverFilePath, string localPath) 2 { 3 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverFilePath); 4 WebResponse respone = request.GetResponse(); 5 Stream netStream = respone.GetResponseStream(); 6 using (Stream fileStream = new FileStream(localPath, FileMode.Create)) 7 { 8 byte[] read = new byte[1024]; 9 int realReadLen = netStream.Read(read, 0, read.Length); 10 while (realReadLen > 0) 11 { 12 fileStream.Write(read, 0, realReadLen); 13 realReadLen = netStream.Read(read, 0, read.Length); 14 } 15 netStream.Close(); 16 fileStream.Close(); 17 } 18 19 }