上传附件判断word、excel、txt等文档中是否含有敏感词如身份证号,手机号等,其它检测如PDF,图片(OCR)等可以自行扩展。
互联网项目中,展示的数据中不能包罗个人信息等敏感信息。判断word中是否包罗手机号,word正文中是否包罗身份证号等敏感信息,通过正则表达式判断匹配手机号,身份证号,以下做为参考。会出现碰撞错误,碰撞不准确等情况,不在本文范围。
开发语言C#,框架asp.net webform。由于上传文件是做的判断,以是这里是判断数据流HttpPostedFile postedFile中的内容。通过load当地文件,本文不做过多介绍。
一、word校验身份证号,手机号
获取word中内容最初用的是npoi插件,office的插件导入导出以前用的比较多,npoi只获取docx文档文本,npoi获取doc后缀有问题,又找的Spire.Doc。可以都使用后者,笔者只简单测试了doc后缀,其它复杂情况没具体测试。有时间的推荐Spire,因为附件中pdf也是一个大项,刚好有对应的using Spire.Pdf;
using NPOI.XWPF.UserModel;
using Spire.Doc;
using Spire.Doc.Documents;- public class WordToTextConvert
- {
- /// <summary>
- /// docx提取成纯文本
- /// </summary>
- /// <param name="file"></param>
- /// <returns></returns>
- public static string ExtractTextFromWord(Stream wordFileStream,string fileExt)
- {
- using (wordFileStream)
- {
- XWPFDocument doc = new XWPFDocument(wordFileStream);
- using (StringWriter writer = new StringWriter())
- {
- string text = "";
- foreach (var para in doc.Paragraphs)
- {
- text += para.Text+" ";
- }
- foreach (XWPFTable table in doc.Tables)
- {
- foreach (XWPFTableRow row in table.Rows)
- {
- foreach (XWPFTableCell cell in row.GetTableCells())
- {
- text += cell.GetText() + " ";
- }
- }
- text += "\r\n";
- }
- return text;
- }
- }
- }
- /// <summary>
- /// doc后缀
- /// </summary>
- /// <param name="wordFileStream"></param>
- /// <param name="fileExt"></param>
- /// <returns></returns>
- public static string ExtractTextFromWordDoc(Stream wordFileStream, string fileExt)
- {
- Spire.Doc.Document doc = new Spire.Doc.Document(wordFileStream);
- Spire.Doc.Table table = doc.Sections[0].Tables[0] as Table;
- string text = doc.GetText()+" ";//获取word文档中的文本
- //纯表格可以使用以下方法
- //遍历表格内容
- for (int i = 0; i < table.Rows.Count; i++)
- {
- var cellsindex = table.Rows[i].Cells.Count;
- for (int j = 0; j < cellsindex; j++)
- {
- TableCell cell = table.Rows[i].Cells[j];
- foreach (Paragraph paragraph in cell.Paragraphs)
- {
- text += paragraph.Text+" ";
- }
- }
- text += "\r\n";
- }
- return text;
- }
- }
复制代码 二、EXCEL校验校验身份证号,手机号
npoi处理excel时要判断后缀,xls和xlsx使用的类不同- public class ExcelToTextConvert
- {
- /// <summary>
- /// 提取成纯文本
- /// </summary>
- /// <param name="file"></param>
- /// <returns></returns>
- public static string ExtractTextFromExcel(Stream excelFileStream,string fileExt)
- {
- using (excelFileStream)
- {
- string text = "";
- IWorkbook workbook=null;
- if (fileExt == "xls")
- {
- workbook = new HSSFWorkbook(excelFileStream);
- }
- if (fileExt == "xlsx")
- {
- workbook = new XSSFWorkbook(excelFileStream);
- }
- ISheet sheet = workbook.GetSheetAt(0);
- if (sheet != null)
- {
- foreach (IRow row in sheet)
- {
- foreach (ICell cell in row)
- {
- switch (cell.CellType)
- {
- case CellType.String:
- text += cell.StringCellValue + " ";
- break;
- case CellType.Numeric:
- text += cell.NumericCellValue + " ";
- break;
- }
- }
- text += "\r\n";
- }
- }
- return text;
- }
- }
- }
复制代码 三、txt校验校验身份证号,手机号
获取文件流的内容,文本文件可以直接读取。方法如下:- if ("txt".Contains(fileExt))
- {
- // 确保文件不为null并且有数据
- if (postedFile != null && postedFile.ContentLength > 0)
- {
- using (StreamReader reader = new StreamReader(postedFile.InputStream))
- {
- // 读取文件内容并返回
- string readContent = reader.ReadToEnd();
- bool hasMobile = ContainsMobileNumber(readContent);
- if (hasMobile)
- {
- return "{"status": 0, "msg": "内容中不可含有手机号!"}";
- }
- bool hasId = ContainsIdNumber(readContent);
- if (hasId)
- {
- return "{"status": 0, "msg": "内容中不可含有身份证号!"}";
- }
- }
- }
- }
复制代码 四、正则校验字符串中是否包罗身份证号,手机号等- public static bool ContainsMobileNumber(string text)
- {
- // 中国大陆手机号码正则表达式
- string pattern = @"1[3-9]\d{9}";
- return Regex.IsMatch(text, pattern);
- }
- public static bool ContainsIdNumber(string text)
- {
- // 中国大陆身份证号正则表达式
- string pattern = @"[1-9]\d{5}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])";
- return Regex.IsMatch(text, pattern);
- }
复制代码 五、读取pdf方法,未做项目验证,请自行搜索相关方法
供参考:读取显示PDF需要借助PDF库,国内Spire.PDF可以读取PDF内容,包括文本,图片以及表格,你可以通过NuGet搜索安装
读取PDF的内容,文本、表格、图片
总结:尽一切合理努力掩护用户个人信息, 并对个人信息进行掩护。为防止用户个人信息在意外的、未经授权的情况下泄漏。
压缩包内容校验根本方法同上,先解压缩,再逐文件处理。本文直接判断有敏感词,不让上传,也可以通过正则把信息更换成****后再转存,这里不再展开。
如有专门的更好用的插件请留言告知讨论,制止重复造轮子。
出处:http://www.cnblogs.com/oorz/Q群:NET CORE技能交流(444036561)
微信公众号:“专卓”;因为专业,以是杰出!可扫描左侧二维码关注。本文版权归作者和博客园共有,接待转载,但未经作者同意必须保留此段声明,且在文章页面显着位置给出原文连接,否则保留追究法律责任的权利。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |