7
从HttpURLConnection获取InputStream对象时发生FileNotFoundException
我正在尝试使用HttpURLConnection(在Java中使用cUrl)将发布请求发送到url。请求的内容为xml,最后,应用程序处理xml并将记录存储到数据库,然后以xml字符串的形式发送回响应。该应用程序本地托管在apache-tomcat上。 当我从终端执行此代码时,将按预期将一行添加到数据库。但是,从连接获取InputStream时会引发如下异常 java.io.FileNotFoundException: http://localhost:8080/myapp/service/generate at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401) at org.kodeplay.helloworld.HttpCurl.main(HttpCurl.java:30) 这是代码 public class HttpCurl { public static void main(String [] args) { HttpURLConnection con; try { con = (HttpURLConnection) new URL("http://localhost:8080/myapp/service/generate").openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); File xmlFile = new File("test.xml"); String xml = ReadWriteTextFile.getContents(xmlFile); con.getOutputStream().write(xml.getBytes("UTF-8")); InputStream response = con.getInputStream(); BufferedReader reader = new …