我有以下代码,它在我的本地开发服务器上完全可以正常工作,但是当我上载到部署服务器时,我总是命中找不到文件

String urlStr = "http://" + getContext().getRequest().getServerName() +
getContext().getServletContext().getContextPath() + "test.action";
URL url = new URL(urlStr);
InputStream input = url.openStream(); //Error always occurs here, it gives me the correct URL but it says file not found.


谁能帮我这个?

最佳答案

因为它是HTTP URL,所以正确的方法如下。

String urlStr = "http://" + getContext().getRequest().getServerName() +
        getContext().getServletContext().getContextPath() + "test.action";
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_ACCEPTED) {
    InputStream input = conn.getInputStream();
}

09-26 04:11