我正在创建一个将从 URL(我自己的 FTP 服务器)下载文件的应用程序。问题是,当我单击“下载”按钮时,我的应用程序将开始下载,但我的应用程序在下载时没有任何响应,但下载后一切正常。
这是我的代码的一部分
GUI.class
b_Download.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String username = "Test";
startDownloading(username);
}
});
private void startDownload(String username)
{
downloader.println("Welcome " + username); //println will show text in a textpane(GUI) and console
downloader.startDownloading();
}
下载器类
public void startDownloading()
{
println("Download jobs started");
download.downloadLIB();
}
下载Job.class
public void downloadLIB()
{
launcher.println("Start downloading files from server...");
String libURL = "http://www.example.com/file.jar";
File libFile = new File("C://file.jar");
downloadFile(libURL, libFile, "file.jar");
}
public void downloadFile(String url, File path, String fileName)
{
InputStream in = null;
FileOutputStream fout = null;
try
{
in = URI.create(url).toURL().openStream();
fout = new FileOutputStream(path);
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1)
{
fout.write(data, 0, count);
}
}
catch(Exception e)
{
launcher.println("Cannot download file : " + fileName, e);
}
finally
{
if (in != null)
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
if(fout != null)
try
{
fout.close();
}
catch (IOException e)
{
e.printStackTrace();
}
launcher.println("File " + fileName + " downloaded successfully");
}
}
当我按下“下载”按钮时,我的文本窗格显示“欢迎用户名”这个词然后它没有响应。但我的控制台会显示“欢迎用户名”、“开始下载作业”和“开始从服务器下载文件...”。几分钟后(文件下载完成后,我的应用程序将再次开始响应...