我有一个http端点,它为我的查询提供xml响应。我试图解雇像这样的http请求 -
import java.io.*;
import java.net.*;
public class c {
public String getHTML(String urlToRead) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// how to store the response as an xml file on disc
rd.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String args[])
{
c c = new c();
System.out.println(c.getHTML(args[0]));
}
}
现在,端点发送xml响应,我想将其存储为光盘上的xml文件。我怎样才能做到这一点。有人可以帮忙吗?
答案 0 :(得分:0)
我没有尝试过,但也许你可以从这样的事情开始:
String dest = "c:\\filename.xml";
// this line substitutes your rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
InputStream is = conn.getInputStream();
// these others goes in your "how to store..." comment
byte[] bytes = IOUtils.toByteArray(is);
FileOutputStream fos = new FileOutputStream(dest);
fos.write(bytes);
fos.flush();
fos.close();