我正在创建一个应用程序,它将从我自己的 Web 服务器下载大量文件。但是我知道为什么它不起作用。它没有任何反应..
这是我的代码的一部分
下载器类
private Proxy proxy = Proxy.NO_PROXY;
public void downloadLibrary()
{
System.out.println("Start downloading libraries from server...");
try
{
URL resourceUrl = new URL("http://www.example.com/libraries.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(resourceUrl.openConnection(proxy).getInputStream());
NodeList nodeLst = doc.getElementsByTagName("Contents");
for (int i = 0; i < nodeLst.getLength(); i++)
{
Node node = nodeLst.item(i);
if (node.getNodeType() == 1)
{
Element element = (Element)node;
String key = element.getElementsByTagName("Key").item(0).getChildNodes().item(0).getNodeValue();
File f = new File(launcher.getWorkingDirectory(), key);
downloadFile("http://www.example.com/" + key, f, "libraries");
}
}
}
catch(Exception e)
{
System.out.println("Error was found when trying to download libraries file " + e);
}
}
public void downloadFile(final String url, final File path, final String fileName)
{
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
{
@Override
protected Void doInBackground() throws Exception
{
launcher.println("Downloading file " + fileName + "...");
try
{
URL fileURL = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(fileURL.openStream());
FileOutputStream fos = new FileOutputStream(path);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
catch(Exception e)
{
System.out.println("Cannot download file : " + fileName + " " + e);
}
return null;
}
@Override
public void done()
{
System.out.println(fileName + " had downloaded sucessfully");
}
};
worker.execute();
}
这是我的 xml 文件的一部分(libraries.xml)
<Key>libraries/org/lwjgl/lwjgl/lwjgl/2.9.0/lwjgl-2.9.0.jar</Key>
我的想法是,我的应用程序将读取 XML 文件。然后它将从服务器下载文件并保存到计算机。例如,我的应用程序下载http://www.example.com/libraries/org/lwjgl/lwjgl/lwjgl/2.9.0/lwjgl-2.9.0.jar
然后它将保存到我的 XML 文件中C://WorkingDir/libraries/org/lwjgl/lwjgl/lwjgl/2.9.0/lwjgl-2.9.0.jar
有大量的内容<Key></Key>
,我必须下载所有内容。
那有什么代码错了吗?感谢您的帮助。