zoukankan      html  css  js  c++  java
  • Threading.Tasks.Task多线程 静态全局变量(字典) --只为了记录

          

    --------------------------------------------------------------后台代码------------------------------------------

        public JsonResult ImportPDF(Int64 id)
            {
                try
                {
                    Guid currentGuid = Guid.NewGuid();

                    if (Request.Files["FileData"].HasFile())
                    {
                        HttpPostedFileBase file = Request.Files["FileData"];
                        //if (file.InputStream.Length > 16*1024*1024)
                        //{
                        //    throw new Exception("文件过大,导入不成功!");
                        //}
                        CreateFolder();
                        string path = Server.MapPath("/Ebook");
                        string fileName = "1.pdf";

                        //Directory.CreateDirectory(path);
                        file.SaveAs(string.Format(@"{0}{1}", path, fileName));
                        PDFText(string.Format(@"{0}{1}", path, fileName), id, currentGuid);
                    }

                    return Json(currentGuid.ToString(), JsonRequestBehavior.AllowGet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }

            /// <summary>
            /// 获取导入PDF的进度
            /// </summary>
            /// <returns></returns>
            public JsonResult GetPdfProgress(string guidStr)
            {
                try
                {
                    Guid guid = new Guid(guidStr.Trim('"'));
                    if (pdfProDic.ContainsKey(guid))
                    {
                        return Json(new { guidKey = guidStr, proVal = pdfProDic[guid] }, JsonRequestBehavior.AllowGet);
                    }
                    return Json(new {guidKey = guidStr}, JsonRequestBehavior.AllowGet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            
            //注意要使用静态(字典与GUID为了应对多人同时访问)
            static Dictionary<Guid,int> pdfProDic=new Dictionary<Guid, int>();
            
            public void PDFText(string fileName, Int64 id,Guid guid)
            {
                System.Threading.Tasks.Task.Factory.StartNew(user =>
                {
                    try
                    {
                        pdfProDic.Add(guid,0);
                        Domain.UserModel.User currentUser = user as Domain.UserModel.User;
                        if (user != null)
                        {
                            #region 执行pdf导入数据库
                            //注意加载PDF文件过大会出错
                            PDDocument doc = PDDocument.load(fileName);
                            PDFTextStripper pdfStripper = new PDFTextStripper();


                            short currentPage = GetMaxPageNumber(id);

                            if (currentPage < 10000)
                                currentPage = 10000;

                            float j = 0;
                            int progress = 0;
                            for (int i = 0; i < doc.getNumberOfPages(); i++)
                            {
                                currentPage++;

                                //索引是从0开始,第一页表示0~1
                                pdfStripper.setStartPage(i);
                                pdfStripper.setEndPage(i + 1);
                                String pdfStr = pdfStripper.getText(doc);

                                var target = EntAppFrameWorkContext.Application.ExtenedT<Ebook, Int64, EbookAppExt>().
                                    CreatePage(
                                        id,
                                        currentPage,
                                        pdfStr,
                                        currentUser);
                                j = i+1;
                                progress = (int)((j / doc.getNumberOfPages()) * 100);
                                if (Convert.ToInt32(j) >= doc.getNumberOfPages())
                                {
                                    progress = 100;
                                    //为了性能的提升,这时候进行排序
                                    EntAppFrameWorkContext.Application.ExtenedT<Ebook,Int64,EbookAppExt>().InitEbookPage(id);
                                }
                                pdfProDic[guid] = progress;
                            }
                            doc.close();
                            #endregion
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    
                }, base.CurrentUser);
                
            }

  • 相关阅读:
    (55)ElasticSearch之使用scroll滚动技术实现大数据量搜锁
    (54)ElasticSearch之DocValues解析
    (53)ElasticSearch之如何计算相关度分数
    HDU
    POJ3311 Hie with the Pie
    luoguP2768: 珍珠项链(矩阵乘法优化DP)
    luoguU60884 【模板】动态点分治套线段树
    最小圆覆盖(洛谷 P1742 增量法)
    CodeForces
    HDU
  • 原文地址:https://www.cnblogs.com/ddddai/p/3719796.html
Copyright © 2011-2022 走看看