开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 1.4K

java-utils/DocSys

forked from Rainy/DocSys
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (22)
标签 (21)
master
dev_DocSys_AddGit_Rainy
DocSys_bld_1.18
DocSys_bld_1.15
DocSys_bld_1.16
DocSys_bld_1.14
dev_DocSys_GD_1.13
DocSys_bld_1.13
DocSys_bld_1.12
DocSys_bld_1.11
DocSys_bld_1.10
dev_DocSys_GD
DocSys_bld_1.09
DocSys_bld_1.08
DocSys_bld_1.07
DocSys_bld_1.06
DocSys_bld_1.05
DocSys_bld_1.04
DocSys_bld_1.03
DocSys_bld_1.02
DocSys_V1.17.10
DocSys_V1.17.9
DocSys_V1.17.8
DocSys_V1.17
DocSys_V1.16
DocSys_V1.15
DocSys_V1.14
DocSys_V1.13
DocSys_V1.12
DocSys_V1.11
DocSys_V1.10
DocSys_V1.09
DocSys_V1.08
DocSys_V1.07
DocSys_V1.06
DocSys_V1.05
DocSys_V1.04
DocSys_V1.03
DocSys_V1.02
DocSys_V1.01
master
分支 (22)
标签 (21)
master
dev_DocSys_AddGit_Rainy
DocSys_bld_1.18
DocSys_bld_1.15
DocSys_bld_1.16
DocSys_bld_1.14
dev_DocSys_GD_1.13
DocSys_bld_1.13
DocSys_bld_1.12
DocSys_bld_1.11
DocSys_bld_1.10
dev_DocSys_GD
DocSys_bld_1.09
DocSys_bld_1.08
DocSys_bld_1.07
DocSys_bld_1.06
DocSys_bld_1.05
DocSys_bld_1.04
DocSys_bld_1.03
DocSys_bld_1.02
DocSys_V1.17.10
DocSys_V1.17.9
DocSys_V1.17.8
DocSys_V1.17
DocSys_V1.16
DocSys_V1.15
DocSys_V1.14
DocSys_V1.13
DocSys_V1.12
DocSys_V1.11
DocSys_V1.10
DocSys_V1.09
DocSys_V1.08
DocSys_V1.07
DocSys_V1.06
DocSys_V1.05
DocSys_V1.04
DocSys_V1.03
DocSys_V1.02
DocSys_V1.01
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (22)
标签 (21)
master
dev_DocSys_AddGit_Rainy
DocSys_bld_1.18
DocSys_bld_1.15
DocSys_bld_1.16
DocSys_bld_1.14
dev_DocSys_GD_1.13
DocSys_bld_1.13
DocSys_bld_1.12
DocSys_bld_1.11
DocSys_bld_1.10
dev_DocSys_GD
DocSys_bld_1.09
DocSys_bld_1.08
DocSys_bld_1.07
DocSys_bld_1.06
DocSys_bld_1.05
DocSys_bld_1.04
DocSys_bld_1.03
DocSys_bld_1.02
DocSys_V1.17.10
DocSys_V1.17.9
DocSys_V1.17.8
DocSys_V1.17
DocSys_V1.16
DocSys_V1.15
DocSys_V1.14
DocSys_V1.13
DocSys_V1.12
DocSys_V1.11
DocSys_V1.10
DocSys_V1.09
DocSys_V1.08
DocSys_V1.07
DocSys_V1.06
DocSys_V1.05
DocSys_V1.04
DocSys_V1.03
DocSys_V1.02
DocSys_V1.01
DocSys
/
src
/
util
/
LuceneUtil2.java
DocSys
/
src
/
util
/
LuceneUtil2.java
LuceneUtil2.java 22.59 KB
一键复制 编辑 原始数据 按行查看 历史
Rainy 提交于 2019年01月12日 20:26 +08:00 . extarctor should be closed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
/**
*
*/
package util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.wltea.analyzer.lucene.IKAnalyzer;
import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;
/**
* 类描述: lucene索引增删改查的公共类
* 创建人:高雨
* 创建时间:2018年10月1日
* @version
* This class is the full-text search driver for DocSys
* There are multi-lucence document mapped to one doc in DocSys, if the content of this Doc is very large
*/
public class LuceneUtil2 {
// 保存路径
private static String INDEX_DIR = getLucenePath();
private static String getLucenePath() {
String path = ReadProperties.read("docSysConfig.properties", "lucenePath");
if(path == null || "".equals(path))
{
String os = System.getProperty("os.name");
System.out.println("OS:"+ os);
if(os.toLowerCase().startsWith("win")){
path = "C:/DocSys/Lucene/";
}
else
{
path = "/data/DocSys/Lucene/"; //Linux系统放在 /data
}
}
File dir = new File(path);
if(dir.exists() == false)
{
System.out.println("getLucenePath() path:" + path + " not exists, do create it!");
if(dir.mkdirs() == false)
{
System.out.println("getLucenePath() Failed to create dir:" + path);
}
}
return path;
}
/**
* 增加索引
* @param id: lucence document id
* @param docId: docId of DocSys
* @param content: 文件内容或markdown文件内容
* @param indexLib: 索引库名字
*/
@SuppressWarnings("deprecation")
public static void addIndex(String id,Integer docId, String content,String indexLib) throws Exception {
System.out.println("addIndex() id:" + id + " docId:"+ docId + " indexLib:"+indexLib);
//System.out.println("addIndex() content:" + content);
Date date1 = new Date();
Analyzer analyzer = new IKAnalyzer();
Directory directory = FSDirectory.open(new File(INDEX_DIR + File.separator+ indexLib));
IndexWriterConfig config = new IndexWriterConfig(
Version.LUCENE_CURRENT, analyzer);
IndexWriter indexWriter = new IndexWriter(directory, config);
Document doc = new Document();
doc.add(new Field("id", id, Store.YES,Index.NOT_ANALYZED_NO_NORMS));
doc.add(new IntField("docId", docId, Store.YES));
doc.add(new TextField("content", content, Store.YES));
indexWriter.addDocument(doc);
indexWriter.commit();
indexWriter.close();
Date date2 = new Date();
System.out.println("创建索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
}
/**
* 更新索引
* @param id: lucence document id
* @param docId: docId of DocSys
* @param content: 文件内容或markdown文件内容
* @param indexLib: 索引库名字
*/
@SuppressWarnings("deprecation")
public static void updateIndex(String id,Integer docId,String content,String indexLib) throws Exception {
System.out.println("updateIndex() id:" + id + " docId:"+ docId + " indexLib:"+indexLib);
System.out.println("updateIndex() content:" + content);
Date date1 = new Date();
Analyzer analyzer = new IKAnalyzer();
Directory directory = FSDirectory.open(new File(INDEX_DIR + File.separator + indexLib));
IndexWriterConfig config = new IndexWriterConfig(
Version.LUCENE_CURRENT, analyzer);
IndexWriter indexWriter = new IndexWriter(directory, config);
Document doc1 = new Document();
doc1.add(new Field("id", id, Store.YES,Index.NOT_ANALYZED_NO_NORMS));
doc1.add(new IntField("docId", docId, Store.YES));
doc1.add(new TextField("content", content, Store.YES));
indexWriter.updateDocument(new Term("id",id), doc1);
indexWriter.close();
Date date2 = new Date();
System.out.println("更新索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
}
/**
* 删除索引
*
* @param id: lucene document id
* @throws Exception
*/
@SuppressWarnings("deprecation")
public static void deleteIndex(String id,String indexLib) throws Exception {
System.out.println("deleteIndex() id:" + id + " indexLib:"+indexLib);
Date date1 = new Date();
Directory directory = FSDirectory.open(new File(INDEX_DIR + File.separator + indexLib));
IndexWriterConfig config = new IndexWriterConfig(
Version.LUCENE_CURRENT, null);
IndexWriter indexWriter = new IndexWriter(directory, config);
indexWriter.deleteDocuments(new Term("id",id));
indexWriter.commit();
indexWriter.close();
Date date2 = new Date();
System.out.println("删除索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
}
/**
* 关键字精确查询,返回docId List
* @param str: 关键字
* @param indexLib: 索引库名字
*/
@SuppressWarnings("deprecation")
public static List<String> search(String str,String indexLib) throws Exception {
Directory directory = FSDirectory.open(new File(INDEX_DIR + File.separator +indexLib));
Analyzer analyzer = new IKAnalyzer();
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "content",analyzer);
Query query = parser.parse(str);
ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
List<String> res = new ArrayList<String>();
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
String docId = hitDoc.get("docId");
if(docId != null && !"".equals(docId))
{
res.add(docId);
System.out.println("search() id:" + hitDoc.get("id") + " docId:"+ docId);
//System.out.println("search() content:" + hitDoc.get("content"));
}
}
ireader.close();
directory.close();
return res;
}
/**
* 关键字模糊查询, 返回docId List
* @param str: 关键字
* @param indexLib: 索引库名字
*/
public static List<String> fuzzySearch(String str,String indexLib) throws Exception {
Directory directory = FSDirectory.open(new File(INDEX_DIR + File.separator +indexLib));
Analyzer analyzer = new IKAnalyzer();
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
FuzzyQuery query = new FuzzyQuery(new Term("content",str));
ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
List<String> res = new ArrayList<String>();
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
String docId = hitDoc.get("docId");
if(docId != null && !"".equals(docId))
{
res.add(docId);
System.out.println("fuzzySearch() id:" + hitDoc.get("id") + " docId:"+ docId);
//System.out.println("fuzzySearch() content:" + hitDoc.get("content"));
}
}
ireader.close();
directory.close();
return res;
}
/**
* 根据docId查询idList,返回idList
*
* @param docId: DocSys doc id
* @param indexLib: 索引库名字
*/
public static List<String> getIdListForDoc(Integer docId,String indexLib) throws Exception {
System.out.println("getIdListForDoc() docId:" + docId + " indexLib:" + indexLib);
Directory directory = FSDirectory.open(new File(INDEX_DIR + File.separator +indexLib));
Analyzer analyzer = new IKAnalyzer();
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
Query query = NumericRangeQuery.newIntRange("docId", docId,docId, true,true);// 没问题
ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
List<String> res = new ArrayList<String>();
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
res.add(hitDoc.get("id"));
System.out.println("searchResult: id:" + hitDoc.get("id") + " docId:"+ hitDoc.get("docId"));
}
ireader.close();
directory.close();
return res;
}
//Delete All Index For Doc
public static void deleteIndexForDoc(Integer docId, String indexLib) throws Exception {
System.out.println("deleteIndexForDoc() docId:" + docId + " indexLib:" + indexLib);
List<String> res = getIdListForDoc(docId, indexLib);
for(int i=0;i < res.size(); i++)
{
deleteIndex(res.get(i),indexLib);
}
}
//Delete Indexs For Real Doc
public static void deleteIndexForRDoc(Integer docId, String indexLib) throws Exception {
System.out.println("deleteIndexForRDoc() docId:" + docId + " indexLib:" + indexLib);
List<String> res = getIdListForDoc(docId, indexLib);
for(int i=0;i < res.size(); i++)
{
deleteIndex(generateRDocId(docId,i), indexLib);
}
}
//Add Index For RDoc
public static void addIndexForRDoc(Integer docId, String filePath, String indexLib) throws Exception {
System.out.println("addIndexForRDoc() docId:" + docId + " filePath:" + filePath + " indexLib:" + indexLib);
File file =new File(filePath);
if(file.length() == 0)
{
System.out.println("addIndexForRDoc() file size is 0");
return;
}
//According the fileSuffix to confirm if it is Word/Execl/ppt/pdf
String fileSuffix = FileUtils2.getFileSuffix(filePath);
if(fileSuffix != null)
{
switch(fileSuffix)
{
case "doc":
addIndexForWord(docId,filePath,indexLib);
break;
case "docx":
addIndexForWord2007(docId,filePath,indexLib);
break;
case "xls":
addIndexForExcel(docId,filePath,indexLib);
break;
case "xlsx":
addIndexForExcel2007(docId,filePath,indexLib);
break;
case "ppt":
addIndexForPPT(docId,filePath,indexLib);
break;
case "pptx":
addIndexForPPT2007(docId,filePath,indexLib);
break;
case "pdf":
addIndexForPdf(docId,filePath,indexLib);
break;
case "txt":
case "TXT":
case "log":
case "LOG":
case "md":
case "MD":
addIndexForFile(docId,filePath,indexLib);
break;
}
}
}
private static boolean addIndexForWord(Integer docId, String filePath, String indexLib) throws Exception{
try {
StringBuffer content = new StringBuffer("");// 文档内容
HWPFDocument doc;
FileInputStream fis = new FileInputStream(filePath);
doc = new HWPFDocument(fis);
Range range = doc.getRange();
int paragraphCount = range.numParagraphs();// 段落
for (int i = 0; i < paragraphCount; i++) {// 遍历段落读取数据
Paragraph pp = range.getParagraph(i);
content.append(pp.text());
}
doc.close();
fis.close();
addIndex(generateRDocId(docId,0),docId,content.toString().trim(),indexLib);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private static boolean addIndexForWord2007(Integer docId, String filePath, String indexLib) throws Exception {
try {
File file = new File(filePath);
String str = "";
FileInputStream fis = new FileInputStream(file);
XWPFDocument xdoc;
xdoc = new XWPFDocument(fis);
XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
str = extractor.getText();
extractor.close();
xdoc.close();
fis.close();
addIndex(generateRDocId(docId,0),docId,str,indexLib);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private static boolean addIndexForExcel(Integer docId, String filePath, String indexLib) throws Exception {
try {
InputStream is = new FileInputStream(filePath);
String text="";
HSSFWorkbook wb = null;
wb = new HSSFWorkbook(new POIFSFileSystem(is));
ExcelExtractor extractor=new ExcelExtractor(wb);
extractor.setFormulasNotResults(false);
extractor.setIncludeSheetNames(true);
text=extractor.getText();
extractor.close();
wb.close();
is.close();
addIndex(generateRDocId(docId,0),docId,text,indexLib);
} catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
private static boolean addIndexForExcel2007(Integer docId, String filePath, String indexLib) throws Exception {
try {
InputStream is = new FileInputStream(filePath);
XSSFWorkbook workBook = null;
String text="";
workBook = new XSSFWorkbook(is);
XSSFExcelExtractor extractor=new XSSFExcelExtractor(workBook);
text=extractor.getText();
extractor.close();
workBook.close();
is.close();
addIndex(generateRDocId(docId,0),docId,text,indexLib);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private static boolean addIndexForPPT(Integer docId, String filePath, String indexLib) throws Exception {
try {
InputStream is = new FileInputStream(filePath);
PowerPointExtractor extractor = null;
String text="";
extractor = new PowerPointExtractor(is);
text=extractor.getText();
extractor.close();
is.close();
addIndex(generateRDocId(docId,0),docId,text,indexLib);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private static boolean addIndexForPPT2007(Integer docId, String filePath, String indexLib) throws Exception {
try {
InputStream is = new FileInputStream(filePath);
String text="";
XMLSlideShow slide = new XMLSlideShow(is);
XSLFPowerPointExtractor extractor=new XSLFPowerPointExtractor(slide);
text=extractor.getText();
extractor.close();
is.close();
addIndex(generateRDocId(docId,0),docId,text,indexLib);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private static boolean addIndexForPdf(Integer docId, String filePath, String indexLib) throws Exception {
File pdfFile=new File(filePath);
String content = "";
try
{
PDDocument document=PDDocument.load(pdfFile);
int pages = document.getNumberOfPages();
// 读文本内容
PDFTextStripper stripper=new PDFTextStripper();
// 设置按顺序输出
stripper.setSortByPosition(true);
stripper.setStartPage(1);
stripper.setEndPage(pages);
content = stripper.getText(document);
document.close();
//System.out.println(content);
addIndex(generateRDocId(docId,0),docId,content,indexLib);
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
private static boolean addIndexForFile(Integer docId, String filePath, String indexLib) throws Exception {
try {
int lineCount = 0;
int totalLine = 0;
int bufSize = 0;
int totalSize = 0;
int chunkIndex = 0;
StringBuffer buffer = new StringBuffer();
String code = FileUtils2.getFileEncode(filePath);
if(FileUtils2.isBinaryFile(code) == true)
{
System.out.println("addIndexForFile() BinaryFile will not add Index");
return true;
}
InputStream is = new FileInputStream(filePath);
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is, code));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
totalLine ++;
lineCount ++;
bufSize = buffer.length();
totalSize += bufSize;
if(bufSize >= 10485760) //10MByte
{
addIndex(generateRDocId(docId,chunkIndex),docId,buffer.toString(),indexLib);
chunkIndex ++;
System.out.println("addIndexForFile() lineCount:" + lineCount + " bufSize:" + bufSize + " chunkIndex:" + chunkIndex);
//Clear StringBuffer
lineCount = 0;
bufSize = 0;
buffer = new StringBuffer();
}
}
if(bufSize > 0)
{
addIndex(generateRDocId(docId,chunkIndex),docId,buffer.toString(),indexLib);
chunkIndex ++;
System.out.println("addIndexForFile() lineCount:" + lineCount + " bufSize:" + bufSize + " chunkIndex:" + chunkIndex);
}
reader.close();
is.close();
System.out.println("addIndexForFile() totalLine:" + totalLine + " totalSize:" + totalSize + " chunks:" + chunkIndex);
} catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
//Update Index For RDoc
public static void updateIndexForRDoc(Integer docId, String filePath, String indexLib) throws Exception {
System.out.println("updateIndexForRDoc() docId:" + docId + " indexLib:" + indexLib + " filePath:" + filePath);
try {
deleteIndexForRDoc(docId,indexLib);
} catch(Exception e) {
System.out.println("deleteIndexForRDoc Failed!");
e.printStackTrace();
}
addIndexForRDoc(docId,filePath,indexLib);
}
//Delete Indexs For Virtual Doc
public static void deleteIndexForVDoc(Integer docId, String indexLib) throws Exception {
System.out.println("deleteIndexForVDoc() docId:" + docId + " indexLib:" + indexLib);
deleteIndex(generateVDocId(docId,0), indexLib);
}
//Add Index For VDoc
public static void addIndexForVDoc(Integer docId, String content, String indexLib) throws Exception {
System.out.println("addIndexForVDoc() docId:" + docId + " indexLib:" + indexLib);
addIndex(generateVDocId(docId,0),docId,content,indexLib);
}
//Update Index For RDoc
public static void updateIndexForVDoc(Integer docId, String content, String indexLib) throws Exception {
System.out.println("updateIndexForVDoc() docId:" + docId + " indexLib:" + indexLib);
updateIndex(generateVDocId(docId,0),docId,content,indexLib);
}
private static String generateVDocId(Integer docId, int index) {
return "VDoc-" + docId + "-" + index;
//return docId+"-0";
}
private static String generateRDocId(Integer docId, int index) {
return "RDoc-" + docId + "-" + index;
//return docId+"-"+ (index+1);
}
public static void readToBuffer(StringBuffer buffer, String filePath) throws Exception
{
try {
String code = getFileEncode(filePath);
InputStream is = new FileInputStream(filePath);
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is, code));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
reader.close();
is.close();
} catch(Exception e){
e.printStackTrace();
}
}
/**
* 获取文件编码格式
* @param filePath
* @return UTF-8/Unicode/UTF-16BE/GBK
* @throws Exception
*/
public static String getFileEncode(String filePath) throws Exception {
String charsetName = null;
try {
File file = new File(filePath);
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
detector.add(new ParsingDetector(false));
detector.add(JChardetFacade.getInstance());
detector.add(ASCIIDetector.getInstance());
detector.add(UnicodeDetector.getInstance());
java.nio.charset.Charset charset = null;
charset = detector.detectCodepage(file.toURI().toURL());
if (charset != null) {
charsetName = charset.name();
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return charsetName;
}
public static String readFile(String filePath) throws Exception {
StringBuffer sb = new StringBuffer();
readToBuffer(sb, filePath);
return sb.toString();
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

DocSys是基于Java开发的Web文件管理系统,旨在为个人和企业用户提供一个简单、方便、安全的文件存储方案,采用Windows桌面系统和Apple手机的用户操作习惯,支持使用SVN和GIT进行历史版本管理、支持文件加密、支持文件内容全文搜索、支持文件预览、支持文件权限管理、支持用户分组管理,支持扩展和定制。GPL 2.0开源协议.
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/java-utils/DocSys.git
git@gitee.com:java-utils/DocSys.git
java-utils
DocSys
DocSys
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /