转载

Windows Server2012上使用Nginx做文件服务器

由于项目中用到了大量的文件上传和删除,考虑到安全的因素,所以整体的思路是使用FTP从主服务器把文件资源上传到文件服务器上。

Windows Server2012上使用Nginx做文件服务器

FTP上传到服务器的代码如下(简单附加一下,具体的网上很多)

public static void UploadFile(FileInfo fileInfo, string hostname, string username,   string password) {   string target;   string targetDir = DateTime.Now.ToString("yyyy-MM-dd");   //创建文件目录   MakeDir(targetDir,hostname,username,password);   target = Guid.NewGuid().ToString();   string URL = "FTP://" + hostname + "/" + targetDir + "/" + target;   FtpWebRequest ftp = GetRequest(URL, username, password);   ftp.Method = WebRequestMethods.Ftp.UploadFile;   ftp.UseBinary = true;   ftp.UsePassive = true;   ftp.ContentLength = fileInfo.Length;   const int BufferSize = 2048;   byte[] content=new byte[BufferSize];   int dataRead;   using (FileStream fs=fileInfo.OpenRead())   {     try     {       using (Stream rs = ftp.GetRequestStream())       {         do         {           dataRead = fs.Read(content, 0, BufferSize);           rs.Write(content, 0, dataRead);         } while (!(dataRead < BufferSize));         rs.Close();       }     }     catch (Exception)     {       throw;     }     finally     {       fs.Close();       Console.WriteLine("上传成功");     }     ftp = GetRequest(URL, username, password);     ftp.Method = System.Net.WebRequestMethods.Ftp.Rename; //改名     ftp.RenameTo =target+ fileInfo.Name.Substring(fileInfo.Name.IndexOf('.'));     try     {       ftp.GetResponse();     }     catch (Exception ex)     {       ftp = GetRequest(URL, username, password);       ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; //删除       ftp.GetResponse();       throw ex;     }     finally     {       //fileinfo.Delete();     }     // 可以记录一个日志  "上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." );     ftp = null;   } }

[1]  [2] [3]  下一页

正文到此结束
Loading...