天天看點

你還記得大明湖畔的哪個lotus嗎?

 利用http協定将本地檔案上傳至domino服務的java執行個體程式,無需多說,自己看吧:

package tryrun;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.mime.MultipartEntity;

import org.apache.http.entity.mime.content.FileBody;

import org.apache.http.entity.mime.content.StringBody;

import org.apache.http.impl.client.DefaultHttpClient;

public class GetSample {

private static final String CONTENT_CHARSET = "UTF-8";// httpclient

    public static void main(String[] args) throws Exception {

        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost("http://...../fileuptest.nsf/fileup?OpenForm");

        FileBody bin = new FileBody(new File("D://dom4j.jar"));

        StringBody form = new StringBody("fileup");

        MultipartEntity reqEntity = new MultipartEntity();

        httppost.setHeader("Cookie", "DomAuthSessId=3049B760B64B37C313A04027DB14FA17");

        StringBody click=new StringBody("48257633002FFBB6.eebb452b4c2dc66448257633003007a2/$Body/0.2BE");

        reqEntity.addPart("__Click", click);

        StringBody comment = new StringBody("A binary file of some kind");

        reqEntity.addPart("comment", comment);

        reqEntity.addPart("Form", form);

        reqEntity.addPart("%%File.48257633002ffbb6.eebb452b4c2dc66448257633003007a2.$Body.0.1D6", bin);

        httppost.setEntity(reqEntity);

        System.out.println("executing request " + httppost.getRequestLine());

        HttpResponse response = httpclient.execute(httppost);

        HttpEntity resEntity = response.getEntity();

        System.out.println("----------------------------------------");

        System.out.println(response.getStatusLine());

        if (resEntity != null)

        {

            System.out.println("Response content length: " + resEntity.getContentLength());

            System.out.println("Chunked?: " + resEntity.isChunked());

            System.out.println("Response content length: " + inputStream2String(resEntity.getContent()));

        }

        if (resEntity != null) {

            resEntity.consumeContent();

        }

    }

    public   static   String   inputStream2String(InputStream   is)   throws   IOException{ 

     ByteArrayOutputStream   baos   =   new   ByteArrayOutputStream(); 

     int   i=-1; 

     while((i=is.read())!=-1){ 

       baos.write(i); 

     } 

       return   baos.toString(); 

     }

}