public static void addId(int num, long pdfStreamId, int type, String path) { try { List <PdfVal> idMap = getIdMapingList(); PdfVal p = new PdfVal(); p.num = num; p.id = pdfStreamId; p.type = type; p.path = path; idMap.Add(p); string[] txtArray = new string[idMap.Count]; int index = 0; foreach (var item in idMap) { txtArray[index] = item.num + "$" + item.id + "$" + item.type + "$" + item.path; index++; } File.WriteAllLines(@"idmapping.txt", txtArray); } catch (Exception ex) { Console.WriteLine("插入数据:" + ex.Message); } }
public static List <PdfVal> getIdMapingList() { List <PdfVal> pdfValList = new List <PdfVal>(); string[] idArray = File.ReadAllLines(@"idmapping.txt"); foreach (String id in idArray) { String[] numArray = id.Split('$'); if (numArray.Length == 4) { PdfVal p = new PdfVal(); p.num = int.Parse(numArray[0]); p.id = long.Parse(numArray[1]); p.type = int.Parse(numArray[2]); p.path = numArray[3]; pdfValList.Add(p); } } return(pdfValList); }
//将需要OCR成功的文件复制到指定的文件中 public void Execute(IJobExecutionContext context) { try { DirectoryInfo sucessFileFolder = new DirectoryInfo(PathUtil.ocrSuccessDestFilePath); //获取ocr成功目录中所有的文件 FileInfo[] list = sucessFileFolder.GetFiles(); foreach (FileInfo ocrFile in list) { Console.WriteLine("准备上传文件..."); String fileName = Path.GetFileName(ocrFile.FullName); String fileDirectory = Path.GetDirectoryName(ocrFile.FullName); String tempFullName = Path.Combine(fileDirectory, fileName + ".temp"); try { File.Move(ocrFile.FullName, tempFullName); //如果能够重命名,说明文件没有被占用 File.Move(tempFullName, ocrFile.FullName); //重新把文件名命名回去 Console.WriteLine("重命名成功" + fileName); } catch (Exception ex) { Console.WriteLine("重命名失败,下次重命名:" + ex.Message + "-" + fileName); continue; } PdfStream pdfStream = new PdfStream(); pdfStream.program_name = SystemConstant.PROGRAMNAME; //将状态设置为ocr完成 2 //查看该文件是否执行失败 String num = Path.GetFileNameWithoutExtension(fileName); if (num.Contains(SystemConstant.ERROR_STRING)) { pdfStream.ocr_flag = OcrConstant.OCR_FAIL; num = num.Replace(SystemConstant.ERROR_STRING, ""); } else { pdfStream.ocr_flag = OcrConstant.OCR_FINISH; } PdfVal pv = null; try { pv = getPdfValByNum(num); if (pv == null) { Console.WriteLine("获取id失败,请坚持该条数据" + fileName); File.Delete(ocrFile.FullName); File.Delete(Path.Combine(PathUtil.needOcrDestFilePath, fileName)); //TxtUtil.removeId(int.Parse(num)); if (fileName.Contains(SystemConstant.ERROR_STRING)) { String errorFileName = fileName.Replace(SystemConstant.ERROR_STRING, ""); Console.WriteLine("删除错误的文件名:" + errorFileName); File.Delete(Path.Combine(PathUtil.needOcrDestFilePath, errorFileName)); } Console.WriteLine("删除needOcr文件" + Path.Combine(PathUtil.needOcrDestFilePath, fileName)); continue; } } catch (Exception ex) { Console.WriteLine("获取id失败,请坚持该条数据" + fileName); File.Delete(ocrFile.FullName); File.Delete(Path.Combine(PathUtil.needOcrDestFilePath, fileName)); //TxtUtil.removeId(int.Parse(num)); if (fileName.Contains(SystemConstant.ERROR_STRING)) { String errorFileName = fileName.Replace(SystemConstant.ERROR_STRING, ""); Console.WriteLine("删除错误的文件名:" + errorFileName); File.Delete(Path.Combine(PathUtil.needOcrDestFilePath, errorFileName)); } Console.WriteLine("删除needOcr文件" + Path.Combine(PathUtil.needOcrDestFilePath, fileName)); continue; } String remotePath; //微信文章跟其他类型的报告路径不一样 if (pv.type == SystemConstant.ARTICLE_TYPE) { remotePath = Path.Combine(SystemConstant.NEW_ARTICLE_UPLOAD_PATH, getAbsolutePdfPath(pv.path)); } else { remotePath = Path.Combine(SystemConstant.NEW_UPLOAD_PATH, getAbsolutePdfPath(pv.path)); } Console.WriteLine(remotePath); //如果是excel直接上传,如果是PDF执行下面的操作 if (fileName.Contains(SystemConstant.EXCEL_EXT)) { //上传 bool uploadExcelIsSuccess = Ftp.upload(ocrFile.FullName, Path.ChangeExtension(remotePath, "xlsx")); if (uploadExcelIsSuccess) { File.Delete(Path.Combine(PathUtil.needOcrDestFilePath, fileName)); File.Delete(ocrFile.FullName); } continue; } bool uploadPdfIsSuccess = Ftp.upload(ocrFile.FullName, remotePath); if (!uploadPdfIsSuccess) { continue; } Console.WriteLine("上传成功。。。" + pv.id); List <PdfStream> pdfStreamList = new List <PdfStream>(); try { pdfStream.id = pv.id; pdfStream.pdf_path = getPdfPath(pv.path); // pdfStreamList.Add(pdfStream); int status = 1; //更新到数据库是否ocr成功 if (pdfStream.ocr_flag == OcrConstant.OCR_FINISH) { HttpUtil.updatePdfStreamDataByPipeLineToMultiFlag(pdfStreamList); //通过multiFlag接口更新 } else { status = -1; HttpUtil.updatePdfStreamDataPipeLine(pdfStreamList); //更新到数据库 } //将excel的标识改为-26(需要ocr就不需要excel) pdfStreamList.Clear(); pdfStream = new PdfStream(); pdfStream.id = pv.id; pdfStream.program_name = SystemConstant.EXCEL_PROGRAMNAME; pdfStream.excel_flag = SystemConstant.NO_NEED_EXCEL; pdfStreamList.Add(pdfStream); HttpUtil.updatePdfStreamDataPipeLineByExcel(pdfStreamList); //更新到数据库 Console.WriteLine("-更新数据库成功"); //插入数据库 Dao dao = new Dao(); Task.Run(() => //异步开始执行 { dao.update(pv.id, pv.type, status); Console.WriteLine("修改数据成功。。。"); //异步执行完成标记 }); } catch (Exception ex) { Console.WriteLine(ex.Message + "-更新时出错"); } finally { File.Delete(ocrFile.FullName); File.Delete(Path.Combine(PathUtil.needOcrDestFilePath, fileName)); if (fileName.Contains(SystemConstant.ERROR_STRING)) { String errorFileName = fileName.Replace(SystemConstant.ERROR_STRING, ""); Console.WriteLine("删除错误的文件名:" + errorFileName); File.Delete(Path.Combine(PathUtil.needOcrDestFilePath, errorFileName)); } //TxtUtil.removeId(int.Parse(num)); Console.WriteLine("删除needOcr文件" + Path.Combine(PathUtil.needOcrDestFilePath, fileName)); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }