URL(自分のFTPサーバー)からファイルをダウンロードするアプリケーションを作成しています。問題は、[ダウンロード] ボタンをクリックすると、アプリケーションのダウンロードが開始されますが、ダウンロード時にアプリケーションが応答せず、ダウンロード後にすべて正常に動作することです。
ここに私のコードの一部があります
GUI クラス
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();
}
Downloader.class
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");
}
}
「ダウンロード」ボタンを押すと、テキストペインに「Welcome Username」という単語が表示され、応答がありません。しかし、コンソールには「Welcome Username」、「Download Jobs started」、「Start download files from server...」という単語が表示されます。数分後 (ファイルのダウンロードが完了すると、アプリケーションは再び応答を開始します...