eXistDB简单通讯05(RPC)

  • 保存文件
  • 取回文件

1、SaveFileRPC.java

package com.neohope.existdb.test;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.net.URL;
import java.util.Vector;

public class SaveFileRPC {
    public static void SaveXML(String xmlFilePath, String user, String pw) throws Exception {
        String url = "http://localhost:8080/exist/xmlrpc";
        String path = "/db/CDA/入院患者护理评估单05.xml";

        XmlRpcClient client = new XmlRpcClient();
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL(url));
        config.setBasicUserName(user);
        config.setBasicPassword(pw);
        client.setConfig(config);

        BufferedReader f = new BufferedReader(new FileReader(xmlFilePath));
        String line;
        StringBuffer xml = new StringBuffer();
        while ((line = f.readLine()) != null)
            xml.append(line);
        f.close();

        Vector<Object> params = new Vector<Object>();
        params.addElement(xml.toString());
        params.addElement(path);
        params.addElement(new Integer(0));
        Boolean result = (Boolean) client.execute("parse", params);

        if (result.booleanValue())
            System.out.println("document stored.");
        else
            System.out.println("could not store document.");
    }

    public static void SaveXMLChuncked(String xmlFilePath, String user, String pwd) throws Exception {
        String url = "http://localhost:8080/exist/xmlrpc";
        String path = "/db/PNG/兔子.png";

        XmlRpcClient client = new XmlRpcClient();
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL(url));
        config.setBasicUserName(user);
        config.setBasicPassword(pwd);
        client.setConfig(config);

        Vector<Object> params = new Vector<Object>();
        String handle = null;
        InputStream fis = new FileInputStream(xmlFilePath);
        byte[] buf = new byte[4096];
        int len;
        while ((len = fis.read(buf)) > 0) {
            params.clear();
            if (handle != null) {
                params.addElement(handle);
            }
            params.addElement(buf);
            params.addElement(new Integer(len));
            handle = (String) client.execute("upload", params);
        }
        fis.close();

        params.clear();
        params.addElement(handle);
        params.addElement(path);
        params.addElement(new Boolean(true));
        params.addElement("image/png");
        Boolean result = (Boolean) client.execute("parseLocal", params); // exceptions

        if (result.booleanValue())
            System.out.println("document stored.");
        else
            System.out.println("could not store document.");
    }

    public static void main(String args[]) throws Exception {
        String user = "neotest";
        String pwd = "neotest";
        SaveXML("PATH_TO_FILE\\入院患者护理评估单05.xml", user, pwd);
        //SaveXMLChuncked("D:\\MyProjects\\IDEA14\\TestExistDB\\XMLFiles\\兔子.png", user, pwd);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*