2015-06-23 33 views
0

我想在URL从Java类发送XML文件作为Java的附件

代码发送XML文件作为附件与我试图为如下

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"); 

但值传递的p_xml_file为src /request.xml

+1

究竟是什么*是问题?你得到一个例外吗?编译错误或警告?其他一些迹象? – nitind

+0

没有例外,但是,我将得到的响应类似于“确保您的XML格式正确且有效”...其RESTFul Web服务URL ..服务提供商要求仅将参数作为XML文件发送 – Vishal

+0

基本的东西我想要的是。 。发送XML文件,就好像附件..到URL – Vishal

回答

0

你也可以考虑的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()); } } } 
0

请使用此链接

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); 
+0

请让我知道这个解决方案是否有助于我可以提供另一种替代方案 – Mudassar

相关问题