【发布时间】:2017-06-08 15:17:58
【问题描述】:
当我使用以下代码下载文件 (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);
}
}
【问题讨论】:
-
你想连接什么样的服务器?哪一行给了你错误?
-
我正在将此文件从我的硬盘分区下载到我的 Web 应用程序。上传任务耗时太长,最后提示连接超时:connect。有什么办法可以阻止这个时间吗?还是加快下载任务?
-
这是一个“连接超时”错误,所以你的问题不是速度,而是你实际上无法连接到服务器。我认为问题在于您使用的是“文件”协议,而它应该是 http,例如myserver/en-parser-chunking.bin".
-
我不清楚如何使用 http 来完成这项任务。你能解释一下在这段代码中是怎么做的吗?
-
您说您正在运行一个 Web 应用程序,因此应该只是将 URL 更改为您的 Web 服务器提供文档的位置,例如“http:...”等..
标签: java file url download timeout