采用metaWeblog.newMediaObject方法发送图片到wordpress中

采用metaWeblog.newMediaObject方法发送图片到wordpress中:
/**
	  * 将字节数组中的对象通过xml-rpc方式发布到Wordpress中
	  * @param name 要发布成的名字
	  * @param type 媒体类型 标准MIME类型
	  * @param bits 字节数组
	  * @return 发布成功时返回可访问此媒体的永久链接地址,失败时返回null
	  * @throws Exception
	  */
	 private String postMediaObject(String name, String type, byte[] bits) throws Exception{        
		 String rt = null;
		 
		 //设置链接到远程接口的对象
		 XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		 config.setServerURL(new URL(this.remoteInterfaceURL));
		 XmlRpcClient client = new XmlRpcClient();
		 client.setConfig(config);	              
	     
		 //设置远程发布方法的参数
		 Map post = new HashMap();
		 post.put("name", name);	   
		 post.put("type", type);	          
		 post.put("bits", bits);
		 Object[] params = new Object[]{"0", this.username, this.password, post};
		 
		 //远程方法调用
		 Map result = (Map) client.execute("metaWeblog.newMediaObject", params);
		 rt = ((String)result.get("url")).toString();
		 return rt;
	}


其中要 注意的问题是:
1. name参数一定要带上扩展名,否则会出错(无法写入文件 无效的文件类型)
2. bits参数,在RFC中介绍要Base64格式,经过实际测验不需要,直接原始数据即可

注:需要先在wordpress中设置开启XML-RPC远程撰写权限,可参见wordpress环境搭建文章

你可能感兴趣的:(xml,wordpress)