Java下载文件:"连接超时:连接"

时间:2017-06-08 15:17:59

标签: java file url download timeout

当我使用以下代码下载文件(35MB)时。它给出了输出:

  

连接超时:连接

以下是我的文件下载过程的java代码。我该如何解决这个问题?

//download file
public void download(String url, File destination) throws IOException {
    URL website = new URL(url);
    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);        
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}

public void parserAction() throws Exception {
    // InputStream is = new FileInputStream("en-parser-chunking.bin");
    File modelFile = new File("en-parser-chunking.bin");

    if (!modelFile.exists()) {
        System.out.println("Downloading model.");
        download("file://E:/Final Project/Softwares and tools/en-parser-chunking.bin", modelFile);

    }       
    ParserModel model = new ParserModel(modelFile);
    Parser parser = ParserFactory.create(model);
    Parse topParses[] = ParserTool.parseLine(line, parser, 1);
    for (Parse p : topParses) {
        //p.show();
        getNounPhrases(p);
    }
}

1 个答案:

答案 0 :(得分:0)

使用以下代码手动设置连接超时,并查看哪个时间足以让文件成功上传:

            String url;            

         try{    
         HttpURLConnection Conn = (HttpURLConnection) new URL( url ).openConnection();

                                      Conn.setConnectTimeout(2000);
                                      Conn.setReadTimeout(2000);

             }
     catch (java.lang.ClassCastException|ConnectException|java.lang.IllegalArgumentException|java.net.MalformedURLException  ex) { }  //catch the possible exception.
     catch (SSLHandshakeException |SocketException | SocketTimeoutException | UnknownHostException ex)

顺便说一下,时间以毫秒为单位。