128 lines
3.8 KiB
Plaintext
128 lines
3.8 KiB
Plaintext
|
|
<%@ WebHandler Language="C#" Class="getRemoteImage" %>
|
|||
|
|
/**
|
|||
|
|
* Created by visual studio 2010
|
|||
|
|
* User: xuheng
|
|||
|
|
* Date: 12-3-8
|
|||
|
|
* Time: <20><><EFBFBD><EFBFBD>13:33
|
|||
|
|
* To get the Remote image.
|
|||
|
|
*/
|
|||
|
|
using System;
|
|||
|
|
using System.Web;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Text.RegularExpressions;
|
|||
|
|
using System.Net;
|
|||
|
|
using System.IO;
|
|||
|
|
|
|||
|
|
public class getRemoteImage : IHttpHandler
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
public void ProcessRequest(HttpContext context)
|
|||
|
|
{
|
|||
|
|
string savePath = context.Server.MapPath("upload/"); //<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ַ
|
|||
|
|
string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" }; //<2F>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
|||
|
|
int fileSize = 3000; //<2F>ļ<EFBFBD><C4BC><EFBFBD>С<EFBFBD><D0A1><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD>λkb
|
|||
|
|
|
|||
|
|
string uri = context.Server.HtmlEncode(context.Request["upfile"]);
|
|||
|
|
uri = uri.Replace("&", "&");
|
|||
|
|
string[] imgUrls = Regex.Split(uri, "ue_separate_ue", RegexOptions.IgnoreCase);
|
|||
|
|
|
|||
|
|
ArrayList tmpNames = new ArrayList();
|
|||
|
|
WebClient wc = new WebClient();
|
|||
|
|
HttpWebResponse res;
|
|||
|
|
String tmpName = String.Empty;
|
|||
|
|
String imgUrl = String.Empty;
|
|||
|
|
String currentType = String.Empty;
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
for (int i = 0, len = imgUrls.Length; i < len; i++)
|
|||
|
|
{
|
|||
|
|
imgUrl = imgUrls[i];
|
|||
|
|
|
|||
|
|
if (imgUrl.Substring(0, 7) != "http://")
|
|||
|
|
{
|
|||
|
|
tmpNames.Add("error!");
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><>ʽ<EFBFBD><CABD>֤
|
|||
|
|
int temp = imgUrl.LastIndexOf('.');
|
|||
|
|
currentType = imgUrl.Substring(temp).ToLower();
|
|||
|
|
if (Array.IndexOf(filetype, currentType) == -1)
|
|||
|
|
{
|
|||
|
|
tmpNames.Add("error!");
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
res = (HttpWebResponse)WebRequest.Create(imgUrl).GetResponse();
|
|||
|
|
//http<74><70><EFBFBD><EFBFBD>
|
|||
|
|
if (res.ResponseUri.Scheme.ToLower().Trim() != "http")
|
|||
|
|
{
|
|||
|
|
tmpNames.Add("error!");
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
//<2F><>С<EFBFBD><D0A1>֤
|
|||
|
|
if (res.ContentLength > fileSize * 1024)
|
|||
|
|
{
|
|||
|
|
tmpNames.Add("error!");
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤
|
|||
|
|
if (res.StatusCode != HttpStatusCode.OK)
|
|||
|
|
{
|
|||
|
|
tmpNames.Add("error!");
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>mime<6D><65><EFBFBD><EFBFBD>
|
|||
|
|
if (res.ContentType.IndexOf("image") == -1)
|
|||
|
|
{
|
|||
|
|
tmpNames.Add("error!");
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
res.Close();
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|||
|
|
if (!Directory.Exists(savePath))
|
|||
|
|
{
|
|||
|
|
Directory.CreateDirectory(savePath);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//д<><D0B4><EFBFBD>ļ<EFBFBD>
|
|||
|
|
tmpName = DateTime.Now.ToString("yyyy-MM-dd") + "/" + System.Guid.NewGuid() + currentType;
|
|||
|
|
wc.DownloadFile(imgUrl, savePath + tmpName);
|
|||
|
|
tmpNames.Add("upload/" + tmpName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
tmpNames.Add("error!");
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
wc.Dispose();
|
|||
|
|
}
|
|||
|
|
context.Response.Write("{url:'" + converToString(tmpNames) + "',tip:'Զ<><D4B6>ͼƬץȡ<D7A5>ɹ<EFBFBD><C9B9><EFBFBD>',srcUrl:'" + uri + "'}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
private string converToString(ArrayList tmpNames)
|
|||
|
|
{
|
|||
|
|
String str = String.Empty;
|
|||
|
|
for (int i = 0, len = tmpNames.Count; i < len; i++)
|
|||
|
|
{
|
|||
|
|
str += tmpNames[i] + "ue_separate_ue";
|
|||
|
|
if (i == tmpNames.Count - 1)
|
|||
|
|
str += tmpNames[i];
|
|||
|
|
}
|
|||
|
|
return str;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool IsReusable
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|