eXistDB简单通讯07(HTTP_REST)

  • 保存文件
  • 取回文件
  • 查询

1、SaveFileHTTP.java

package com.neohope.existdb.test;

//apache的base64会多一个换行符
//import org.apache.ws.commons.util.Base64;
//import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import com.sun.xml.internal.messaging.saaj.util.Base64;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class SaveFileHTTP {
    public static void SaveXML(String inFileName, String fileId) throws IOException {
        URL url = new URL("http://localhost:8080/exist/rest/db/CDA/" + fileId);
        System.out.println("Save file to " + url.toString());

        HttpURLConnection connect = (HttpURLConnection) url.openConnection();
        connect.setRequestMethod("PUT");
        connect.setDoOutput(true);
        connect.setRequestProperty("Content-Type", "application/xml");
        String userCredentials = "neotest:neotest";
        String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
        System.out.println(basicAuth);
        connect.setRequestProperty ("Authorization", basicAuth);

        File file = new File(inFileName);
        InputStream is = new FileInputStream(file);
        OutputStream os = connect.getOutputStream();

        byte[] buf = new byte[1024];
        int c;
        while ((c = is.read(buf)) > -1) {
            os.write(buf, 0, c);
        }
        os.flush();
        os.close();
        System.out.println("Statuscode " + connect.getResponseCode()
                + " (" + connect.getResponseMessage() + ")");
    }

    public static void main(String[] args) throws IOException {
        SaveXML("D:\\MyProjects\\IDEA14\\TestExistDB\\XMLFiles\\医惠\\入院患者护理评估单01.xml", "入院患者护理评估单01.xml");
    }
}

Leave a Reply

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

*