在java中将XML文件作为附件发送

时间:2015-06-23 06:21:09

标签: java xml eclipse restful-url

我想将XML文件作为附件通过Java类

发送到URL

我正在尝试的代码如下

 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

3 个答案:

答案 0 :(得分: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()); } } }

答案 1 :(得分: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);

答案 2 :(得分:0)

2天后,搜索得到了一些使用完整的东西,它为我工作..无需导入任何额外的Jar文件.. 如果我们想通过RESTFul Web服务URL将文件作为附件发送,则MultipartUtility是正确的,为什么要这样做.. 开始了..!!现成的代码 - &gt; http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically