当我下载文件(35‘m)使用下面的代码。它给我的输出如下:
连接超时:连接
下面是我的文件下载过程的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);
}
}发布于 2017-06-08 15:59:57
使用以下代码手动设置连接的超时时间,并查看哪些时间足够长以让文件成功上载:
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)顺便说一下,时间以毫秒为单位。
https://stackoverflow.com/questions/44439656
复制相似问题