インターネットからファイルをダウンロードするためにJava関数を使用しています。
public void getLatestRelease()
{
try
{
// Function called
long startTime = System.currentTimeMillis();
// Open connection
System.out.println("Connecting...");
URL url = new URL(latestReleaseUrl);
url.openConnection();
// Download routine
InputStream reader = url.openStream();
FileOutputStream writer = new FileOutputStream("release.zip");
byte[] buffer = new byte[153600];
int totalBytesRead = 0;
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) > 0)
{
writer.write(buffer, 0, bytesRead);
buffer = new byte[153600];
totalBytesRead += bytesRead;
}
// Download finished
long endTime = System.currentTimeMillis();
// Output download information
System.out.println("Done.");
System.out.println((new Integer(totalBytesRead).toString()) + " bytes read.");
System.out.println("It took " + (new Long(endTime - startTime).toString()) + " milliseconds.");
// Close input and output streams
writer.close();
reader.close();
}
// Here I catch MalformedURLException and IOException :)
}
そして、ダウンロードの進行状況を視覚化することになっているJProgressBar
コンポーネントがあります。JPanel
private static void createProgressBar(JPanel panel)
{
JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
panel.add(progressBar, BorderLayout.SOUTH);
}
WebアプリケーションのMVCと同様に、ユーザーに表示される「フロントエンド」ビューから「バックエンド」機能を分離したいと思います。
したがって、関数はクラスgetLatestRelease()
のパッケージにあります。framework
MyFramework
イベントリスナーを含む、インターフェイス生成に関連するすべてSwing
のものがパッケージに含まれていますfrontend
。
メインクラスで、パッケージのメインクラスであるのインスタンスとのController
インスタンスを作成します。MyFramework
ApplicationFrontend
frontend
progressBar
問題は、ダウンロードの進行状況に応じて、値を更新する方法です。