C#版本 java类似 流用法略不同
private void WriteFile(byte[] info, string fullpath) { FileStream fs = null; BinaryWriter w = null; try { //string name = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss.ffffff") + ".html"; //string path = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "MN\\" + name); fs = new FileStream(fullpath, FileMode.CreateNew, FileAccess.ReadWrite); // 转换为字节 写入数据 ( 可写入中文 ) //Byte[] info = Encoding.GetEncoding("GB2312").GetBytes(content); // 字节数组 , 字节偏移量 , 最多写入的字节数 w = new BinaryWriter(fs); // 设置要写入的偏移量 fs.Position = fs.Length; fs.Write(info, 0, info.Length); } finally { if (w != null) w.Close(); if (fs != null) fs.Close(); } }
private void imgService(HttpContext context) { HttpRequest request = context.Request; HttpResponse response = context.Response; string url = request.QueryString["url"]; string fileName = url.GetHashCode().ToString()+".gif"; string ipaddress = System.Configuration.ConfigurationManager.AppSettings["ipaddress"]; //string path = System.Configuration.ConfigurationManager.AppSettings["path"]; string savePath = URLUtil.getDomainName(url).Replace(".", "_");//保存的文件夹 //string fileName = url.Substring(url.LastIndexOf("/")+1);//保存的文件名 string savename = context.Server.MapPath("imgs") + "\\" + savePath + "\\" + fileName; if (!File.Exists(savename)) { HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url); webrequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/QVOD, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; webrequest.Headers.Add("Accept-Encoding", "gzip, deflate"); webrequest.Headers.Add("Accept-Language", "zh-cn"); webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; MS-RTC LM 8; Alexa Toolbar)"; webrequest.Headers.Add("UA-CPU", "x86"); webrequest.KeepAlive = true; HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
if (webresponse.StatusCode == HttpStatusCode.OK) { // System.Drawing.Image image = System.Drawing.Image.FromStream(webresponse.GetResponseStream()); Stream st = webresponse.GetResponseStream(); BinaryReader br = new BinaryReader(st); byte[] poto = br.ReadBytes(4096+(int)webresponse.ContentLength); if (!System.IO.Directory.Exists(context.Server.MapPath("imgs") + "\\" + savePath)) { System.IO.Directory.CreateDirectory(context.Server.MapPath("imgs") + "\\" + savePath); }
WriteFile(poto, savename);
/*string ct=webresponse.ContentType.ToLower(); if (ct.IndexOf("gif") > 0) { //image.Save(savename); } else { //image.Save(savename, System.Drawing.Imaging.ImageFormat.Gif);//保存路径 } //image.Dispose();//释放资源 */ } } response.Redirect(ipaddress +savePath+"/"+ fileName);
}
|
请发表评论