public void ProcessRequest(HttpContext context) { //并不知道如何用参数获得文件只能用索引的方式,反正就一张图 context.Response.ContentType = "text/plain"; var file = context.Request.Files[0]; string tags = context.Request["tags"]; string pictreSummary = context.Server.HtmlEncode(context.Request["summary"]); List<String> Tags = new List<String>(); foreach (var item in tags.Split(new char[] { ',' })) { Tags.Add(item.Trim()); } //去除重复的标签 HashSet<string> hsTag = new HashSet<string>(Tags); #region 用户登入校验 if (context.Session["current_user"] == null) { PictureUploadError(context, 1); return; } User user = context.Session["current_user"] as User; #endregion #region 用户是否被禁止上传图片 if (user.UserStatus == 1) { PictureUploadError(context, 4); return; } #endregion #region 文件类型校验 string fileName = file.FileName; string extName = Path.GetExtension(fileName); if (!(extName == ".jpeg" || extName == ".jpg" || extName == ".bmp" || extName == ".png" || extName == ".gif")) { PictureUploadError(context, 2); return; } #endregion #region 文件大小校验 if (file.ContentLength > IMAGE_SIZE_LIMIT) { PictureUploadError(context, 3); return; } #endregion #region 保存文件 #region 保存到磁盘 //大小图所在的文件夹的相对路径 string bigRelativePathDir = BIG_IMAGE_SAVE_PATH + DateTime.Now.ToString("yyyyMMdd"); string smallRelativePathDir = BIG_IMAGE_SAVE_PATH + "SmallImg_" + DateTime.Now.ToString("yyyyMMdd"); //大,小图所在的文件夹绝对路径 string bigPathDir = context.Request.MapPath("../" + bigRelativePathDir); string smallPathDir = context.Request.MapPath("../" + smallRelativePathDir); //创建文件夹 if (!Directory.Exists(bigPathDir)) { Directory.CreateDirectory(bigPathDir); Directory.CreateDirectory(smallPathDir); } //算出文件的MD5 string fileMD5Name = GetMD5FromFile(file.InputStream); string fileExt = Path.GetExtension(file.FileName); //大,小图所在的相对路径 string bigImgRelativePath = bigRelativePathDir + "/" + fileMD5Name + fileExt; string smallImgRelativePath = smallRelativePathDir + "/" + "small_" + fileMD5Name + ".jpg"; //大,小图所在的绝对路径 string bigImgPath = context.Request.MapPath("../" + bigImgRelativePath); string smallImgPath = context.Request.MapPath("../" + smallImgRelativePath); //保存 file.SaveAs(bigImgPath); PictureProcessHelper.MakeThumbnail(bigImgPath, smallImgPath, 400, 0, "W"); #endregion #region 保存到数据库 #region 获取标签对象,判断标签是否已经存在了,不存在则创建 Picture.BLL.TagBLL tagBll = new Picture.BLL.TagBLL(); List<Picture.Model.TagModel> tagModelList = new List<Picture.Model.TagModel>(); Picture.Model.TagModel tempModel = null; foreach (var item in hsTag) { tempModel = tagBll.QuerySingle(new { TagName = item }); if (tempModel == null) { int tagId = tagBll.Insert(new Picture.Model.TagModel() { TagName = item }); tagModelList.Add(new Picture.Model.TagModel() { TagName = item, TId = tagId }); } else { tagModelList.Add(tempModel); } tempModel = null; } #endregion #region 更新索引 foreach (var item in tagModelList) { IndexManager.tagIndex.Add(item); } #endregion #region 保存图片 Picture.BLL.PictureInfoBLL pictureBll = new Picture.BLL.PictureInfoBLL(); int width = 0; int height = 0; PictureProcessHelper.GetPictureShap(bigImgPath, out width, out height); Picture.Model.PictureInfoModel model = new Picture.Model.PictureInfoModel() { CollectCount = 0, Height = height, Width = width, ImgSummary = pictreSummary, LargeImgPath = bigImgRelativePath, UId = user.UId, UploadDate = DateTime.Now }; int pId = pictureBll.Insert(model); #endregion #region 保存图片与标签的关系 Picture.BLL.TagImgRelationBLL bllTIR = new Picture.BLL.TagImgRelationBLL(); foreach (var item in tagModelList) { bllTIR.Insert(new Picture.Model.TagImgRelationModel() { ImgId = pId, TagId = item.TId }); } #endregion #endregion #endregion context.Response.Write(JSONHelper.ToJSONString(new { isUpload = true })); }
/// <summary> /// 从索引库中检索关键字 /// </summary> private void SearchFromIndexData() { string indexPath = Context.Server.MapPath("IndexData"); FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory()); IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); //搜索条件 PhraseQuery query = new PhraseQuery(); //把用户输入的关键字进行分词 foreach (string word in Picture.Utility.SplitContent.SplitWords(Request["SearchKey"])) { query.Add(new Term("summary", word)); } //query.Add(new Term("content", "C#"));//多个查询条件时 为且的关系 query.SetSlop(100); //指定关键词相隔最大距离 //TopScoreDocCollector盛放查询结果的容器 TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); searcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器 //TopDocs 指定0到GetTotalHits() 即所有查询结果中的文档 如果TopDocs(20,10)则意味着获取第20-30之间文档内容 达到分页的效果 ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs; //展示数据实体对象集合 var pictureInfoModel = new List<Picture.Model.PictureInfoModel>(); for (int i = 0; i < docs.Length; i++) { int docId = docs[i].doc;//得到查询结果文档的id(Lucene内部分配的id) Document doc = searcher.Doc(docId);//根据文档id来获得文档对象Document Picture.Model.PictureInfoModel picture = new Picture.Model.PictureInfoModel(); picture.ImgSummary = doc.Get("summary"); //book.ContentDescription = doc.Get("content");//未使用高亮 //搜索关键字高亮显示 使用盘古提供高亮插件 //book.ContentDescription = Picture.Utility.SplitContent.HightLight(Request.QueryString["SearchKey"], doc.Get("content")); picture.PId= Convert.ToInt32(doc.Get("id")); pictureInfoModel.Add(picture); } Repeater1.DataSource = pictureInfoModel; Repeater1.DataBind(); }