public class Fetcher {
private String getPage(String file) { // 读取日志文件 BufferedReader reader = null;
StringBuilder sb = new StringBuilder(); String line = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream((file)), Charset.forName("utf-8")));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
} catch (MalformedURLException e) {
System.out.println("file:" + file);
System.out.println("error:" + e);
} catch (IOException e) {
System.out.println("file:" + file);
System.out.println("error:" + e);
}
return sb.toString();
}
// 解析日志文件:标题,日期,正文 public void getDoc(String file, String server, String user, String passwd) {
String html = getPage(file);
String titleDivRegex = "<title>.+?</title>";
Pattern titleDivPattern = Pattern.compile(titleDivRegex);
Matcher titleDivMatcher = titleDivPattern.matcher(html);
String title = null;
if (titleDivMatcher.find()) { // 标题 title = titleDivMatcher.group().replaceAll("<title>", "")
.replaceAll("</title>", "");
}
String dateDivRegex = "<h5 id=/".+?/">.+?</h5>";
Pattern dateDivPattern = Pattern.compile(dateDivRegex);
Matcher dateMatcher = dateDivPattern.matcher(html);
String dateStr = null;
Date postDate = null;
if (dateMatcher.find()) { // 日期 dateStr = dateMatcher.group().replaceAll("<h5 id=/".+?/">", "")
.replaceAll("</h5>", "").trim();
String tmp = dateStr.replace(":", ":");
postDate = new Date(tmp);
}
String textDivRegex = "<div id=/".*/" class=/"blogpost/">.+?</div>";
Pattern textDivPattern = Pattern.compile(textDivRegex);
Matcher textMatcher = textDivPattern.matcher(html);
String text = null;
if (textMatcher.find()) { // 正文 text = textMatcher.group().replaceAll(
"<div id=/".*/" class=/"blogpost/">", "").replaceAll(
"</div>", "").trim();
}
String[] categories = { "android", "linux" };
text = "<html><meta http-equiv=/"Content-Type/" content=/"text/html; charset=utf-8/">"
+ text + "</html>";
Post post = new Post(title, text, categories, postDate);
post.setServer(server, user, passwd);
post.publish();
System.out.println("title:" + title);
System.out.println("date" + postDate);
}}
2) 上转到其它博客
public class Post {
private Date dateCreated;
private String description;
private String title;
private String[] categories;
private String mServer;
private String mUser; private String mPasswd;
static private XmlRpcClientConfigImpl config;
static private XmlRpcClient client;
public Post(String title, String description, String[] categories,
Date dateCreated) {
this.dateCreated = dateCreated;
this.description = description;
this.title = title;
this.categories = categories;
}
static {
config = new XmlRpcClientConfigImpl();
client = new XmlRpcClient();
}
private void writelog(String log) {
UI.getInstance().showLog(log);
}
public void setServer(String server, String user, String passwd) {
mServer = server;
mUser = user;
mPasswd = passwd;
try {
config.setServerURL(new URL(mServer));
client.setConfig(config);
} catch (MalformedURLException e) {
System.out.println("connect error");
}
}
public void publish() {
Map<String, Object> struct = new HashMap<String, Object>();
struct.put("dateCreated", dateCreated);
struct.put("description", description);
struct.put("title", title);
struct.put("categories", categories);
Object[] params = new Object[] { mUser, mUser, mPasswd, struct, true };
String blogid = null;
try { // 发布日志 blogid = (String) client.execute("metaWeblog.newPost", params);
writelog("OK: title=" + title + " id=" + blogid + "/n");
} catch (XmlRpcException e) {
writelog("ERR: title=" + title + "/n");
}
struct.clear();
}}