URL的openStream()为什么在服务器上获取不了?


1.自定义的函数:(调用的是下面ImageIO的方法)
public static void imgResize(String webPicPath,String dictFileName) throws Exception {
Image img = ImageIO.read(new URL(webPicPath));
//TODO:other
}

2.ImageIO.read(URL input)函数:


 public static BufferedImage read(URL input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }

    InputStream istream = null;
    try {
        istream = input.openStream();
    } catch (IOException e) {
        throw new IIOException("Can't get input stream from URL!", e);
    }
    ImageInputStream stream = createImageInputStream(istream);
    BufferedImage bi;
    try {
        bi = read(stream);
        if (bi == null) {
            stream.close();
        }
    } finally {
        istream.close();
    }
    return bi;
}

为什么放在服务器上,在openStream的时候会报错?
报错信息:"Can't get input stream from URL!"
今天本机还特意部署了跟服务器上一样的环境:apache+tomcat+JKMount

本机测试都无问题的,放在服务器上就报错了。求解

webPicPath是任意一张网络图片的路径,异常的内容: Caused by: javax.imageio.IIOException: Can't get input stream from URL! at javax.imageio.ImageIO.read(ImageIO.java:1369) at com.xxx.webapp.aaa.util.ImgUtil.imgCut(ImgUtil.java:265) ... 65 more Caused by: java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at sun.net.NetworkClient.doConnect(NetworkClient.java:163) at sun.net.www.http.HttpClient.openServer(HttpClient.java:394) at sun.net.www.http.HttpClient.openServer(HttpClient.java:529) at sun.net.www.http.HttpClient. <init> (HttpClient.java:233) at sun.net.www.http.HttpClient.New(HttpClient.java:306) at sun.net.www.http.HttpClient.New(HttpClient.java:323) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:975) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:916) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:841) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177) at java.net.URL.openStream(URL.java:1010) at javax.imageio.ImageIO.read(ImageIO.java:1367) ... 67 more </init>

编程 程序员 java eclipse javaweb

无色D夜空 10 years, 5 months ago

Your Answer