Send XML file as attachment in java

1.8k Views Asked by At

I wanted to send XML file as attachment over URL from Java class

Code with which i am trying is as below

 File request_XML_file = new File("src/request.xml");      
            URL url = new URL("https://************?p_xml_file="+request_XML_file); 
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("enctype","multipart/form-data"); 

But value passed for p_xml_file is src/request.xml

3

There are 3 best solutions below

0
On BEST ANSWER

After 2 days search got some use full stuff and its worked for me.. No need to import any additional Jar file.. If we wanted to send an file as attachment over RESTFul Web service URL MultipartUtility is correct why to do it.. Here we go..!! A ready made code --> http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically

0
On

You can also consider the new features of Java 7

Path path = Paths.get("/tmp/foo/bar.txt"); Files.createDirectories(path.getParent()); try { Files.createFile(path); } catch (FileAlreadyExistsException e) { System.err.println("already exists: " + e.getMessage()); } } }
1
On

Kindly use this link

http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/PostXML.java?view=markup

DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost("http://localhost:8080/TESTINGrestful/rest/polls/comment"); StringEntity input = new StringEntity("<Comment>...</Comment>"); input.setContentType("text/xml"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest);