Requirement: Java parses the CHM file and inserts the content into the database and Redis.
Java parsing chm file, on-line except GitHub on a guy a little bit, there is no reference, including chm4j this thing, no introduction, in line with the spirit of service to the public, finishing the next process, time rush, error is unavoidable, look correct.
First step: Download Chm4j.jar and dependencies
http://sourceforge.net/projects/chm4j/
The second step: Create a new Java project, build an analytic parsechm class, build an analytic test class, similar to:
Parsechm class:
The following package, please import Chm4j.jar, and copy the Chm4j.dll to the Lib directory of the JRE, Linux or Mac copy libchm4j.so IE//CAN, because Chm4j.jar relies on C + +
Package cn.lswe.baseframe.utils;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import org.chm4j.*;
Import cn.lswe.baseframe.validator.Conf;
public class Parsechm {
public static void Main (String ... args) {
try {
Chmfile cFile = new Chmfile (Conf.chmsource);
String dir = conf.dir;
Chmentry.attribute attributes = ChmEntry.Attribute.ALL;
chmentry[] entries = cfile.entries (attributes);
for (Chmentry entry:entries) {
Listchmentry (dir, entry, attributes);
}
} catch (IOException ex) {
System.out.println ("Error:" + ex.getmessage ());
}
}
/**
* Extracts recursively the sub entries of the specified chmentry into the
* Specified output directory according to the specified attributes.
* @param output of the output directory.
* @param entry
* @param attributes
* @throws java.io.IOException If an I/O error occurs.
*/
private static void Listchmentry (String output, chmentry entry, Chmentry.attribute attributes) throws IOException {
Printentry (entry);
String Er=guidhelper.createguid (). toString ();
File Dest = new file (output, Entry.getpath ());
if (Entry.hasattribute (ChmEntry.Attribute.DIRECTORY)) {
if (!dest.isdirectory ()) {
if (!dest.mkdirs ()) {
throw new IOException ("Failed to create directory:" + dest);
}
}
For (chmentry e:entry.entries (attributes)) {
Listchmentry (output, E, attributes);
}
} else {
InputStream in = null;
OutputStream out = null;
try {
in = Entry.getinputstream ();
out = new FileOutputStream (dest);
int buffersize = 1024;
byte[] data = new Byte[buffersize];
int nbread;
while ((Nbread = in.read (data)) > 0) {
Out.write (data, 0, nbread);
Out.flush ();
}
} catch (IOException ex) {
System.out.println (Ex.getmessage ());
} finally {
try {
if (out! = null) {
Out.close ();
}
} finally {
if (in = null) {
In.close ();
}
}
}
}
}
/**
* Display the specified entry.
* @param entry
*/
private static void Printentry (Chmentry entry) {
StringBuilder sb = new StringBuilder ("Extract entry" + Entry + "(");
Boolean first = true;
For (Chmentry.attribute attribute:entry.getAttributes ()) {
if (first) {
First = false;
} else {
Sb.append (",");
}
Sb.append (attribute);
}
Sb.append (")");
System.out.println (Sb.tostring ());
}
}
This has been a number of transit HTML files (more than 20,000), note that the format of the CHM file is quite complex, decided to the processing method, in fact, my next Word,freemind file, all of this processing.
Test class:
Package Cn.lswe.baseframe.spider;
Import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;
Import Cn.lswe.baseframe.utils.FileHelper;
import cn.lswe.baseframe.validator.Conf;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import Us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
Public class Parsedisease implements pageprocessor {
Private Site site = site. Me (). Setretrytimes (3). Setsleeptime (1000);
@Override
Public void Process (Page page) {
list<string>links=New linkedlist<string> ();
Links.add (Conf.diseasedatasource);
Page.addtargetrequests (links);
list<string> tdlist = page.gethtml (). XPath ("table"). XPath ("TD"). All ();
Using the WebMagic crawler framework here, you can refer to this link:
http://webmagic.io/docs/zh/
int J= 0;
int k= 0;
for (int i=0;i<tdlist.size (); i++) {
Here, you can filter and process your content.
Insert Jedis, insert database OK
j=i+1;
k=i+2;
String TD1=tdlist.get (i);
String td2=tdlist.get (i);//The second column is split into an array, which is the next level category of the first column
String td3=tdlist.get (i);//If the first column is a letter, the third column and the second column one by one correspond, if the first column is a Chinese character, the third column and the second column start each corresponding, its encoding is "B" + the first row of the third column + Bank third column
}
System. out. println (tdlist);
}
@Override
Public Site Getsite () {
return site;
}
@SuppressWarnings ("deprecation")
Public Static void Testspider () {
Conf. the starting point of diseasedatasource sniffing, such as www.ask.com, for Speed , Please deploy all the resource files
// to the localhost
Spider. Create (new parsedisease ())
. Addurl (Conf.diseasedatasource)
. Pipeline (new consolepipeline ()). Thread (5). Run ();
Open 5 Thread Crawl
}
}.
Call Method:
@ResponseBody
@RequestMapping ("/test/spider")
Public void spider ()
{
Oschinablogpageprocesser.testspider ();
Parsechm. Testspider ();
}
Java parsing CHM file Combat (original)