gpt4 book ai didi

java - 给定的 URL 有 fileNotFoundException

转载 作者:行者123 更新时间:2023-12-01 04:49:20 25 4
gpt4 key购买 nike

我有以下代码,它在我的本地开发服务器上运行得很好,但是当我上传到部署服务器时,我总是遇到文件未找到异常

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();
}

关于java - 给定的 URL 有 fileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15249887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com