怎样通过xmlrpc往wordpress上传文件

public class XmlRpc {
public static void main(String[] args) throws Exception {
XmlRpc xmlrpc = new XmlRpc();
Object result = xmlrpc.uploadFile();
System.out.println(result);
}
XmlRpcClient xmlrpc;
XmlRpcClientConfigImpl config;
public XmlRpc() throws MalformedURLException{
xmlrpc = new XmlRpcClient();
config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(“http://yourwordpress/xmlrpc.php”));
config.setConnectionTimeout(1000000);
xmlrpc.setConfig(config);
}

public Object uploadFile() throws XmlRpcException, IOException{
FileInputStream fis = new FileInputStream(“c:/01.txt”);
byte[] b = new byte[fis.available()];
fis.read(b);
HashMap map = new HashMap();
map.put(“name”, “abc.txt”);
map.put(“bits”, b);
Object[] params = new Object[] {“1″,”yourname”,”yourpassword”,map};
return xmlrpc.execute(“metaWeblog.newMediaObject“, params);
}
}

返回结果:

{type=, file=abc.txt, url=http://yourwordpress/wp-content/uploads/abc.txt}

使用apache的xmlrpc-client-3.1, wordpress2.3.2上测试通过

Comments are closed.