代码如上
完整代码如下:
1 using System; 2 using System.Web; 3 using System.Net; 4 using System.IO; 5 using System.Threading; 6 using System.Collections.Generic; 7 8 9 public class ImageFtpUpload : IHttpHandler 10 { 11 12 HttpRequest Request; 13 HttpResponse Response; 14 HttpServerUtility Server; 15 16 string ftpIp = string.Empty; 17 string ftpPort = "21"; 18 string ftpUsn = string.Empty; 19 string ftpPwd = string.Empty; 20 string ftpServerDomain = string.Empty;//example : http://www.baidu.com 21 22 Random fileNameRndNum = new Random(); 23 24 List<string[]> saveFilePathList = new List<string[]>(); 25 26 public void ProcessRequest(HttpContext context) 27 { 28 Request = context.Request; 29 Response = context.Response; 30 Server = context.Server; 31 32 ftpIp = Request.QueryString["ftpIp"]; 33 ftpPort = Request.QueryString["ftpPort"]; 34 ftpUsn = Request.QueryString["ftpUsn"]; 35 ftpPwd = Request.QueryString["ftpPwd"]; 36 ftpServerDomain = Request.QueryString["ftpDomain"]; 37 38 39 if (IsSafeData(ftpIp, ftpPort, ftpUsn, ftpPwd, ftpServerDomain)) 40 { 41 if (ftpServerDomain.IndexOf("http://") == -1) 42 { 43 ftpServerDomain = "http://" + ftpServerDomain; 44 } 45 46 47 SaveData(); 48 } 49 } 50 51 bool IsSafeData(params string[] arr) 52 { 53 bool boolean = true; 54 for (int i = 0; i < arr.Length; i++) 55 { 56 boolean = boolean && !string.IsNullOrEmpty(arr[i]); 57 } 58 return boolean; 59 } 60 61 62 void SaveData() 63 { 64 string remoteImages = Request.Form["data"]; 65 if (!string.IsNullOrEmpty(remoteImages)) 66 { 67 string[] imgArr = remoteImages.Split(','); 68 if (imgArr.Length > 0) 69 { 70 ThreadPool.SetMaxThreads(5, 5); 71 ManualResetEvent[] manualEvents = new ManualResetEvent[imgArr.Length]; 72 for (int i = 0; i < imgArr.Length; i++) 73 { 74 manualEvents[i] = new ManualResetEvent(false); 75 ThreadPool.QueueUserWorkItem(SaveImageToFtp, new object[] { imgArr[i], manualEvents[i] }); 76 } 77 78 79 WaitHandle.WaitAll(manualEvents); 80 81 } 82 } 83 84 if (saveFilePathList.Count > 0) 85 { 86 Response.Write("{\"status\":\"success\",\"data\":["); 87 for (int i = 0; i < saveFilePathList.Count; i++) 88 { 89 string[] arr = saveFilePathList[i]; 90 if (i > 0) 91 { 92 Response.Write(","); 93 } 94 Response.Write("{\"originalImgSrc\":\"" + arr[0] + "\",\"nowImgSrc\":\"" + arr[1] + "\"}"); 95 } 96 Response.Write("]}"); 97 return; 98 } 99 100 101 102 Response.Write("{\"status\":\"failed\"}"); 103 } 104 105 106 void SaveImageToFtp(object o) 107 { 108 object[] objArr = (object[])o; 109 string imageUrl = objArr[0].ToString(); 110 ManualResetEvent mre = (ManualResetEvent)objArr[1]; 111 112 113 string path = SaveDataToFtp(imageUrl); 114 saveFilePathList.Add(new string[] { imageUrl, ftpServerDomain + path }); 115 mre.Set(); 116 } 117 118 bool FtpExistsFolder(string folderParentPath, string folderName) 119 { 120 bool exists = false; 121 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpIp + ":" + ftpPort + folderParentPath); 122 request.Credentials = new NetworkCredential(ftpUsn, ftpPwd); 123 request.Method = WebRequestMethods.Ftp.ListDirectory; 124 FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 125 using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) 126 { 127 string line = streamReader.ReadLine(); 128 while (!string.IsNullOrEmpty(line)) 129 { 130 131 if (folderName == line) 132 { 133 exists = true; 134 } 135 line = streamReader.ReadLine(); 136 } 137 } 138 139 response.Close(); 140 request.Abort(); 141 142 return exists; 143 } 144 145 void FtpCreateFolder(string folderPath) 146 { 147 Uri uri = new Uri("ftp://" + ftpIp + ":" + ftpPort + folderPath); 148 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); 149 request.Method = WebRequestMethods.Ftp.MakeDirectory; 150 151 request.Credentials = new NetworkCredential(ftpUsn, ftpPwd); 152 FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 153 FtpStatusCode f = response.StatusCode; 154 response.Close(); 155 request.Abort(); 156 } 157 158 void FtpSaveFile(byte[] data, string filePath) 159 { 160 Uri uri = new Uri("ftp://" + ftpIp + ":" + ftpPort + filePath); 161 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); 162 163 request.Method = WebRequestMethods.Ftp.UploadFile; 164 request.Credentials = new NetworkCredential(ftpUsn, ftpPwd); 165 request.ContentLength = data.Length; 166 167 using (Stream requestStream = request.GetRequestStream()) 168 { 169 requestStream.Write(data, 0, data.Length); 170 } 171 172 FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 173 FtpStatusCode f = response.StatusCode; 174 175 176 response.Close(); 177 request.Abort(); 178 } 179 180 181 182 List<int> rndNumList = new List<int>(); 183 int GetRndNum() 184 { 185 int num = fileNameRndNum.Next(0, 1000); 186 if (rndNumList.Contains(num)) 187 { 188 return GetRndNum(); 189 } 190 rndNumList.Add(num); 191 return num; 192 } 193 194 string SaveDataToFtp(string remoteUrl) 195 { 196 197 198 string path = string.Empty; 199 if (!string.IsNullOrEmpty(remoteUrl)) 200 { 201 int dotInx = remoteUrl.LastIndexOf("."); 202 if (dotInx > 0) 203 { 204 string ftpUri = "ftp://" + ftpIp + ":" + ftpPort; 205 206 DateTime now = DateTime.Now; 207 string ext = remoteUrl.Substring(dotInx); 208 string folderName = now.Year + "-" + now.Month; 209 210 string fileName = (now - (new DateTime(2000, 1, 1))).TotalMilliseconds.ToString().Replace(".", string.Empty) + GetRndNum() + ext; 211 path = "/" + folderName + "/" + fileName; 212 213 using (WebClient wc = new WebClient()) 214 { 215 byte[] data = wc.DownloadData(remoteUrl); 216 217 //try 218 //{ 219 if (!FtpExistsFolder("/", folderName)) 220 { 221 FtpCreateFolder("/" + folderName + "/"); 222 } 223 224 FtpSaveFile(data, "/" + folderName + "/" + fileName); 225 226 //}catch{ 227 228 //} 229 } 230 } 231 } 232 233 return path; 234 }