1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
public class PutFile {
public static void main(String[] args) throws Exception {
/*
* Creation of The API service
*/
PiiService_Service shs = new PiiService_Service();
PiiService sh = (PiiService) shs.getPiiServicePort();
String PolicyUploadUrl = "http://localhost:8082/api?";
File file = new File("test.xml");
DataInputStream ips = (new DataInputStream(new FileInputStream("test.xml")));
InputStreamReader ipsr = new InputStreamReader(ips);
BufferedReader br = new BufferedReader(ipsr);
String XMLcontent = "", line;
while ((line = br.readLine()) != null)
{
XMLcontent += line + "\n";
}
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPut httpput = new HttpPut(PolicyUploadUrl);
MultipartEntity mpEntity = new MultipartEntity();
httpput.setEntity(mpEntity);
httpput.addHeader("name", "http");
httpput.addHeader("value", "httpValue");
httpput.addHeader("value",XMLcontent);
System.out.println("Request is "+httpput.toString()+" "+httpput.getParams());
System.out.println("Executing Request \n" + httpput.getRequestLine());
HttpResponse response = httpclient.execute(httpput);
System.out.println("Finish Executing Request");
HttpEntity resEntity = response.getEntity();
System.out.println(" Test response "+response.getStatusLine());
if (resEntity != null)
{
System.out.println(" Error: "+EntityUtils.toString(resEntity));
}
if (resEntity != null)
{
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
} |
Partager