對參考的文檔、文章表示感謝。
package org.apache.http.examples.conn;import java.io.File;import java.io.InputStream; import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpHead;import org.apache.http.client.methods.HttpPut;import org.apache.http.entity.FileEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import java.io.FileOutputStream; /** * How to send a request directly using {@link HttpClient}. * * @since 4.0 */public class test_m { public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); try { HttpHost target = new HttpHost("http://127.0.0.1", 8080, "http"); HttpGet req = new HttpGet("http://127.0.0.1:8080/auth/v1.0"); req.addHeader("X-Storage-User", "admin:admin"); req.addHeader("X-Storage-Pass", "admin"); System.out.println("executing request to " + target); //HttpResponse rsp = httpclient.execute(target, req); HttpResponse rsp = httpclient.execute( req); HttpEntity entity = rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } Header h1=rsp.getFirstHeader("X-Storage-Url"); Header h2=rsp.getFirstHeader("X-Auth-Token"); System.out.println(h1.getValue()+h2.getValue()); //test head method HttpHead hph=new HttpHead(h1.getValue()); hph.addHeader(h2); rsp=httpclient.execute(hph); entity=rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); headers=null; headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); //test put method HttpPut hpp=new HttpPut(h1.getValue()+"/photos5"); hpp.addHeader(h2); rsp=httpclient.execute(hpp); entity=rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); headers=null; headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); //put file File file = new File("/home/ralph/cute.jpg"); //File file = new File("/home/ralph/test.txt"); FileEntity entity1 = new FileEntity(file, "text/plain; charset=\"UTF-8\""); HttpPut httppost = new HttpPut(h1.getValue()+"/photos5/lzl.jpg"); //HttpPut httppost = new HttpPut(h1.getValue()+"/photos5/lzl.txt"); httppost.setHeader(h2); httppost.setHeader("Content-Type","image/jpeg"); //httppost.setHeader("Content-Type","text/plain; charset=\"UTF-8\""); httppost.setHeader("X-Object-Meta-lzl", "一張關於林志玲的美圖"); httppost.setEntity(entity1); DefaultHttpClient httpclient1 = new DefaultHttpClient(); rsp=httpclient1.execute(httppost); /////////////////////////////////////////////////////// entity=rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); headers=null; headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); //put file //download file HttpGet httpget = new HttpGet(h1.getValue()+"/photos5/lzl.jpg"); httpget.addHeader(h2); DefaultHttpClient httpclient2 = new DefaultHttpClient(); HttpResponse response = httpclient2.execute(httpget); if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){ entity = response.getEntity(); if (entity != null) { System.out.println(entity.getContentType()); System.out.println(entity.isStreaming()); File storeFile = new File("/home/ralph/cute1.jpg"); FileOutputStream output = new FileOutputStream(storeFile); InputStream input = entity.getContent(); byte b[] = new byte[1024]; int j = 0; while( (j = input.read(b))!=-1){ output.write(b,0,j); } output.flush(); output.close(); } if (entity != null) { entity.consumeContent(); } } //end download file } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }}