Swing/ProgressMonitorInputStream のバックアップ(No.3)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/ProgressMonitorInputStream へ行く。
- 1 (2013-04-22 (月) 03:35:42)
- 2 (2013-09-16 (月) 20:04:19)
- 3 (2014-02-04 (火) 15:03:27)
- 4 (2014-12-19 (金) 14:13:43)
- 5 (2014-12-19 (金) 21:05:16)
- 6 (2015-09-17 (木) 18:10:52)
- 7 (2016-05-30 (月) 16:11:37)
- 8 (2017-08-16 (水) 17:31:33)
- 9 (2018-08-22 (水) 20:05:54)
- 10 (2020-08-16 (日) 17:12:26)
- 11 (2022-01-14 (金) 07:59:19)
- 12 (2024-02-16 (金) 08:25:14)
- 13 (2025-01-03 (金) 08:57:02)
- 14 (2025-01-03 (金) 09:01:23)
- 15 (2025-01-03 (金) 09:02:38)
- 16 (2025-01-03 (金) 09:03:21)
- 17 (2025-01-03 (金) 09:04:02)
- 18 (2025-06-19 (木) 12:41:37)
- 19 (2025-06-19 (木) 12:43:47)
TITLE:ProgressMonitorInputStreamを使用してテキストファイルのダウンロード状況を表示
Posted by aterai at 2013-04-22
ProgressMonitorInputStreamを使用してテキストファイルのダウンロード状況を表示
ProgressMonitorInputStream
を使用してテキストファイルのダウンロード状態を進捗表示します。
Screenshot

Advertisement
サンプルコード
worker = new SwingWorker<String, Chunk>() {
@Override public String doInBackground() {
Charset cs = Charset.forName("EUC-JP");
String ret = "Done";
String path = "http://terai.xrea.jp/";
URLConnection urlConnection;
try{
urlConnection = new URL(path).openConnection();
System.out.println(urlConnection.getContentEncoding());
System.out.println(urlConnection.getContentType());
String encoding = urlConnection.getContentEncoding();
if(encoding!=null) {
cs = Charset.forName(encoding);
}else{
String contentType = urlConnection.getContentType();
for(String value: contentType.split(";")) {
value = value.trim();
if(value.toLowerCase().startsWith("charset=")) {
encoding = value.substring("charset=".length());
}
}
if(encoding!=null) {
cs = Charset.forName(encoding);
}
}
System.out.println(cs);
}catch(Exception ex) {
ex.printStackTrace();
ret = "Error";
return ret;
}
int length = urlConnection.getContentLength();
try(InputStream is = urlConnection.getInputStream();
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(frame, "Loading", is);
BufferedReader reader = new BufferedReader(new InputStreamReader(pmis, cs))) {
monitor = pmis.getProgressMonitor();
monitor.setNote(" "); //Need for JLabel#getPreferredSize
monitor.setMillisToDecideToPopup(0);
monitor.setMillisToPopup(0);
monitor.setMinimum(0);
monitor.setMaximum(length);
int i = 0;
int size = 0;
String line;
while((line = reader.readLine()) != null) {
if(i++%50==0) { //Wait
Thread.sleep(10);
}
size += line.getBytes(cs).length + 1; //+1: \n
String note = String.format("%03d%% - %d/%d%n", 100*size/length, size, length);
publish(new Chunk(line, note));
}
}catch(InterruptedException | IOException ex) {
ret = "Exception";
cancel(true);
}
return ret;
}
@Override protected void process(List<Chunk> chunks) {
for(Chunk c: chunks) {
textArea.append(c.line+"\n");
monitor.setNote(c.note);
//System.out.println(c.note);
}
textArea.setCaretPosition(textArea.getDocument().getLength());
}
@Override public void done() {
frame.getGlassPane().setVisible(false);
runButton.setEnabled(true);
String text = null;
try{
text = isCancelled() ? "Cancelled" : get();
}catch(Exception ex) {
ex.printStackTrace();
text = "Exception";
}
System.out.println(text);
}
};
worker.execute();
View in GitHub: Java, Kotlin解説
上記のサンプルでは、URLConnection
から開いたInputStream
にProgressMonitorInputStream
をラップして、ファイルのダウンロード進捗状態をProgressMonitor
で表示しています。
ProgressMonitorInputStream
の使用するProgressMonitor
の最大値は、ファイルサイズ(バイト)ProgressMonitorInputStream
がデフォルトで設定する最大値は、InputStream#available()
の値- この値がダウンロード中のストリームの合計バイト数を返す訳ではないので、これを最大値のままにしておくと、
ProgressMonitor
が表示されない、またはすぐ閉じてしまう URLConnection#getContentLength()
で取得したバイト数をProgressMonitor#setMaximum(...)
で設定
- 一行ずつ
JTextArea
に文字列として読み込ませるために、InputStreamReader
を使用しているので、エンコードをURLConnection#getContentEncoding()
やURLConnection#getContentType()
などで取得- 何パーセント読み込んだかを
ProgressMonitor#setNote(...)
で表示する場合は、一行が何バイトかをString#getBytes(Charset)
で取得して計算 - 注: 改行は
1
バイトで決め打ちしている - 進捗を表示する前に
ProgressMonitor#setNote("dummy note");
としておかないと、Note
に使用するJLabel
がnull
のままで表示されない、またはレイアウトがおかしくなる
- 何パーセント読み込んだかを