注意:下面的方法在csdn博客改版以后无法使用,因为现在csdn博客不支持metadata api,不知道什么时候可以支持。
http://hi.baidu.com/cnjsp/blog/item/e175cf1b27bc6af6ae513335.html
本方法我测试过,是可以用来的,一则感觉思路挺新颖了,程序员自己写代码解决自己的事情。另一个可以通过这个实例学习一下java,所以我贴出我修改后的java代码。
具体思路可以参见原文。
package cn.mingyuan.baidu2csdn.core; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; /** * csdn博文 * * @author mingyuanonline@gmail.com * */ public class CSDNPost { /** * 博文创建日期 */ private Date dateCreated; /** * 博文内容 */ private String description; /** * 标题 */ private String title; /** * 博文分类 */ private String[] categories; public CSDNPost() { } public CSDNPost(String title, String description, String[] categories, Date dateCreated) { this.dateCreated = dateCreated; this.description = description; this.title = title; this.categories = categories; } public Date getDateCreated() { return dateCreated; } public void setDateCreated(Date dateCreated) { this.dateCreated = dateCreated; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String[] getCategories() { return categories; } public void setCategories(String[] categories) { this.categories = categories; } /** * xml-rpc配置 */ private static XmlRpcClientConfigImpl config; /** * xml-rpcClient */ private static XmlRpcClient client; static { config = new XmlRpcClientConfigImpl(); try { // 此处请将telnetor替换为您的用户名 config.setServerURL(new URL( "http://blog.csdn.net/xw13106209/services/metablogapi.aspx")); } catch (MalformedURLException e) { System.out.println("请检查url"); } client = new XmlRpcClient(); client.setConfig(config); } /** * 日志记录 * * @param log * log */ private void writelog(String log) { FileOutputStream fos = null; try { fos = new FileOutputStream("post.log", true); fos.write((log + "\r\n").getBytes()); fos.flush(); fos.close(); } catch (IOException e) { System.out.println("写入日志错误:" + log); } } /** * 发布 */ 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[] { "your usrname", // "replace it with your username", // "replace it with your password", struct, true }; Object[] params = new Object[] { "xw13106209", "xw13106209", "password", struct, true }; String blogid = null; try { blogid = (String) client.execute("metaWeblog.newPost", params); } catch (XmlRpcException e) { writelog("导入出现错误:title=" + title); System.out.println("导入出现错误:title=" + title); } writelog(title + ">> 导入完毕,生成博文id为>>" + blogid); System.out.println(title + ">> 导入完毕,生成博文id为>>" + blogid); struct.clear(); } public static void main(String[] args) { CSDNPost post = new CSDNPost(); post.publish(); } }
package cn.mingyuan.baidu2csdn.core; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; public class DeletePostById { private static XmlRpcClientConfigImpl config; private static XmlRpcClient client; static { config = new XmlRpcClientConfigImpl(); try { config.setServerURL(new URL( "http://blog.csdn.net/telnetor/services/metablogapi.aspx")); } catch (MalformedURLException e) { System.out.println("请检查url"); } client = new XmlRpcClient(); client.setConfig(config); } /** * 删除帖子 * * @param appkey * appkey,可以任意,这是一个忽略的值 * @param postid * 帖子id * @param username * 用户名 * @param password * 密码 * @param publish * 博客在帖子被删除之后是否重新发布 */ public static void delete(String appkey, String postid, String username, String password, boolean publish) { Object[] params = new Object[] { "ignored value", postid, username, password, true }; try { client.execute("blogger.deletePost", params); } catch (XmlRpcException e) { System.out.println("删除出错,postid=" + postid); } System.out.println(postid + "删除完毕"); } /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { BufferedReader reader = null; String line; try { reader = new BufferedReader(new InputStreamReader( new FileInputStream("content"))); while ((line = reader.readLine()) != null) { line = line.split("生成博文id为:")[1]; delete("ignored", line, "your username", "your password", true); Thread.sleep(1000 * 10); } } catch (FileNotFoundException e1) { System.out.println("文件没找到"); } catch (IOException e) { System.out.println("读取文件失败"); } } }