示例#1
0
        public Crawler Fetch()
        {
            if (!IsExternalIPAddress(this.SourceUrl))
            {
                State = "INVALID_URL";
                return(this);
            }


            var request = HttpWebRequest.Create(this.SourceUrl);

            request.Method = "GET";
            using (var response = request.GetResponse())
            {
                //if (response.StatusCode != HttpStatusCode.OK)
                //{
                //    State = "Url returns " + response.StatusCode + ", " + response.StatusDescription;
                //    return this;
                //}
                if (response.ContentType.IndexOf("image") == -1)
                {
                    State = "Url is not an image";
                    return(this);
                }
                var    sourceUrl = Path.GetFileName(this.SourceUrl);
                string fileExt   = Path.GetExtension(sourceUrl).ToLower();//扩展名
                if (string.IsNullOrEmpty(fileExt))
                {
                    var strs = response.ContentType.ToLower().Split('/');
                    if (strs.Length == 2)
                    {
                        fileExt = "." + strs[1];
                    }
                }

                var    upload   = new Common.Upload();
                string filePath = upload.getFilePath("catcher");
                string fileName = upload.getFileName(fileExt);
                try
                {
                    var stream = response.GetResponseStream();
                    if (fileExt.Equals(".gif"))//gif文件
                    {
                        using (FileStream writer = new FileStream(Common.Fetch.getMapPath(filePath + fileName), FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            byte[] buff = new byte[512];
                            int    c    = 0; //实际读取的字节数
                            while ((c = stream.Read(buff, 0, buff.Length)) > 0)
                            {
                                writer.Write(buff, 0, c);
                            }
                            ServerUrl = filePath + fileName;
                            State     = "SUCCESS";
                        }
                    }
                    else
                    {
                        ServerUrl = upload.saveImage(Image.FromStream(stream), fileName, filePath);
                        State     = "SUCCESS";
                    }
                    stream.Dispose();
                }
                catch (Exception e)
                {
                    State = "抓取错误:" + e.Message;
                }
                return(this);
            }
        }
示例#2
0
        public override string Process()
        {
            var folder = "news";

            if (!string.IsNullOrEmpty(UploadConfig.Folder))
            {
                folder = UploadConfig.Folder;
            }

            if (UploadConfig.Base64)
            {
                var uploadFileName  = UploadConfig.Base64Filename;
                var uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
                try
                {
                    using (MemoryStream ms = new System.IO.MemoryStream(uploadFileBytes))
                    {
                        var    img      = Image.FromStream(ms);
                        var    upload   = new Common.Upload();
                        string filePath = upload.getFilePath(folder);
                        string fileName = upload.getFileName(Path.GetExtension(UploadConfig.Base64Filename));
                        Result.Url   = upload.saveImage(img, fileName, filePath);
                        Result.State = UploadState.Success;
                    }
                }
                catch (Exception e)
                {
                    Result.State        = UploadState.FileAccessError;
                    Result.ErrorMessage = e.Message;
                }
            }
            else
            {
                string uploadFileName = null;
                var    file           = Request.Files[UploadConfig.UploadFieldName];
                uploadFileName = file.FileName;

                if (!CheckFileType(uploadFileName))
                {
                    Result.State = UploadState.TypeNotAllow;
                    return(WriteResult());
                }

                Result.OriginFileName = uploadFileName;

                try
                {
                    var upload   = new bitcms.Common.Upload();
                    var fileInfo = upload.save(folder, file, true);

                    Result.Url   = fileInfo.Path;
                    Result.State = UploadState.Success;
                }
                catch (Exception ex)
                {
                    Result.State        = UploadState.NetworkError;
                    Result.ErrorMessage = ex.Message;
                    return(WriteResult());
                }
            }
            return(WriteResult());
        }