0

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

Vishal
  • 549
  • 1
  • 4
  • 21
  • 1
    What actually *is* the problem? Are you getting an Exception? A compilation error or warning? Some other indication? – nitind Jun 23 '15 at 06:55
  • no exception but, response i will get is like "Ensure that your XML is well-formed and valid"... its RESTFul web service URL.. service provider asking to send Parameter as XML file only – Vishal Jun 23 '15 at 06:57
  • Basic thing i want is.. send XML file as if like attachment.. to URL – Vishal Jun 23 '15 at 06:59
  • Try this? http://stackoverflow.com/a/7557047/2294676 – Lydia Ralph Jun 23 '15 at 09:12
  • @alex thanks for link.. i used same code... gave some different response now "The XML request file specified by the client was not found or the file was found to be empty." how can i assign that file to p_xml_file parameter ? – Vishal Jun 23 '15 at 11:10

3 Answers3

0

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()); } } }
Mudassar
  • 3,135
  • 17
  • 22
0

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);
Mudassar
  • 3,135
  • 17
  • 22
0

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

Vishal
  • 549
  • 1
  • 4
  • 21