求帮忙用java编写一个扫描程序,只需实现能扫描出特征码库中的文件就行,多谢多谢。 10
3个回答
展开全部
import java.io.FileOutputStream;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.Node;
import org.htmlparser.Parser;
public class ParserHtml {
private static String ENCODE = "GBK";
/**
* 中文字符编码
*
*/
private String message(String codingMsg) {
String str = null;
try {
str = new String(codingMsg.getBytes(ENCODE), System.getProperty("file.encoding"));
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/**
* 写入操作
* @param filePath 静态页面路径
* @param fileStr 网页内容
* @throws Exception
*/
public void writeSourceFile(String szFileName,String pageUrl) {
try {
String parserContent = readerPageByUrl(pageUrl);
File file=new File(szFileName);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fileout = new FileOutputStream(file);
fileout.write(parserContent.getBytes());
fileout.close();
}
catch( Exception e ) {
e.printStackTrace();
}
}
/**
* 根据网址读取网页HTML内容
* @param szFileName 静态页面路径
*/
public String readerPageByUrl(String pageUrl) throws Exception{
StringBuffer parserContent=new StringBuffer();
String returnStr = "";
URL url = null;
try{
url = new URL(pageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Parser parser = new Parser(connection);
for (NodeIterator i = parser.elements(); i.hasMoreNodes();) {
Node node = i.nextNode();
parserContent.append(node.toHtml(true)+"\n");
}
returnStr = message(parserContent.toString().trim());
System.out.println(returnStr);
}
catch( Exception e ) {
e.printStackTrace();
}
return returnStr;
}
public static void main(String[] args) {
String pageUrl="http://10.1.250.31:8080/login.jsp";
String filePath="E://static.txt";
try {
new ParserHtml().writeSourceFile(filePath, pageUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.Node;
import org.htmlparser.Parser;
public class ParserHtml {
private static String ENCODE = "GBK";
/**
* 中文字符编码
*
*/
private String message(String codingMsg) {
String str = null;
try {
str = new String(codingMsg.getBytes(ENCODE), System.getProperty("file.encoding"));
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/**
* 写入操作
* @param filePath 静态页面路径
* @param fileStr 网页内容
* @throws Exception
*/
public void writeSourceFile(String szFileName,String pageUrl) {
try {
String parserContent = readerPageByUrl(pageUrl);
File file=new File(szFileName);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fileout = new FileOutputStream(file);
fileout.write(parserContent.getBytes());
fileout.close();
}
catch( Exception e ) {
e.printStackTrace();
}
}
/**
* 根据网址读取网页HTML内容
* @param szFileName 静态页面路径
*/
public String readerPageByUrl(String pageUrl) throws Exception{
StringBuffer parserContent=new StringBuffer();
String returnStr = "";
URL url = null;
try{
url = new URL(pageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Parser parser = new Parser(connection);
for (NodeIterator i = parser.elements(); i.hasMoreNodes();) {
Node node = i.nextNode();
parserContent.append(node.toHtml(true)+"\n");
}
returnStr = message(parserContent.toString().trim());
System.out.println(returnStr);
}
catch( Exception e ) {
e.printStackTrace();
}
return returnStr;
}
public static void main(String[] args) {
String pageUrl="http://10.1.250.31:8080/login.jsp";
String filePath="E://static.txt";
try {
new ParserHtml().writeSourceFile(filePath, pageUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |