国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

用Java下載文件。多線程,這行得通嗎?

瀏覽:100日期:2024-05-04 18:34:44
如何解決用Java下載文件。多線程,這行得通嗎??

意見建議:

使用SwingWorker進(jìn)行后臺線程工作。在您的SwingWorker內(nèi)部,通過設(shè)置其進(jìn)度“ bound”屬性setProgress(int progress)。該值應(yīng)在1到100之間。不要讓您的SwingWorker /文件下載器擁有JProgressBar或任何Swing組件。將Propertychangelistener添加到您的SwingWorker,并監(jiān)視progress屬性中的更改。切勿將您的Swing字段(或大多數(shù)和所有字段)公開。限制訪問,而是通過方法更改對象狀態(tài)。閱讀Swing中的并發(fā)教程以獲取必要的詳細(xì)信息。

例如,下面的代碼是一個(gè)簡單的簡化,不下載任何文件,但是應(yīng)該可以使您了解:

import java.awt.*;import java.beans.PropertyChangeEvent;import java.beans.Propertychangelistener;import java.util.Random;import javax.swing.*;public class Initial { static AtomFrame atomLauncher; public static void main(String[] args) { atomLauncher = new AtomFrame(); atomLauncher.start(); System.out.println(Integer.MAX_VALUE); final Download theDownload = new Download(); theDownload.addPropertychangelistener(new Propertychangelistener() { @Override public void propertyChange(PropertyChangeEvent pcEvt) { if ('progress'.equals(pcEvt.getPropertyName())) { int progress = theDownload.getProgress(); atomLauncher.setProgress(progress); } } }); theDownload.execute(); }}class AtomFrame extends JFrame { // ********* should be private! private JProgressBar progressBar; private static final long serialVersionUID = 4010489530693307355L; public static void main(String[] args) { AtomFrame testFrame = new AtomFrame(); testFrame.start(); } public void setProgress(int progress) { progressBar.setValue(progress); } public AtomFrame() { initializeComponents(); } public void initializeComponents() { this.setSize(400, 400); this.setLocationRelativeto(null); this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); this.setTitle('Atom Launcher'); this.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); progressBar = new JProgressBar(); this.add(progressBar); // this.pack(); } public void start() { this.setVisible(true); } public void close() { this.dispose(); }}class Download extends SwingWorker<Void, Void> { private static final long SLEEP_TIME = 300; private Random random = new Random(); @Override protected Void doInBackground() throws Exception { int myProgress = 0; while (myProgress < 100) { myProgress += random.nextInt(10); setProgress(myProgress); try { Thread.sleep(SLEEP_TIME); } catch (InterruptedException e) {} } return null; }}解決方法

首先,每個(gè)人都需要知道我對Java編碼還比較陌生。更準(zhǔn)確地說,我是面向?qū)ο缶幊痰男率帧?/p>問題。

我試圖創(chuàng)建一個(gè)下載類來更新進(jìn)度條,以顯示其進(jìn)度。可能還有其他決定,我決定在以后進(jìn)行更新。

目前的問題是,在我看來,這不起作用。我可以在“主要”方法上做任何我想做的事情,而且GUI仍然響應(yīng)迅速。根據(jù)我過去的編程經(jīng)驗(yàn),除非我對GUI進(jìn)行線程化,否則這是不可能的。為什么是這樣?

既然可以了,這樣可以嗎?

主班

package atomicElectronics;import java.io.IOException;import atomicElectronics.physical.AtomFrame;import atomicElectronics.utility.Download;public class Initial { static AtomFrame atomLauncher; public static void main(String[] args) {atomLauncher = new AtomFrame();atomLauncher.start();System.out.println(Integer.MAX_VALUE);Download theDownload = new Download();theDownload.fileProgressBar(atomLauncher.progressBar);try { theDownload.exicute('http://download.videolan.org/pub/videolan/vlc/last/win64/vlc-2.1.3-win64.exe','C:UsersTrinaryAtomAppDataRoaming');} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();}// TODO Add Download Methods// theDownload.updateBarTotal(JProgressBar);// theDownload.updateLabelSpeed(String);// theDownload.updateLabelTotal(String);// theDownload.addFile(File);// theDownload.addFiles(Files); }}

類AtomFrame

package atomicElectronics.physical;import javax.swing.JFrame;import java.awt.FlowLayout;import javax.swing.JProgressBar;public class AtomFrame extends JFrame{ public JProgressBar progressBar; private static final long serialVersionUID = 4010489530693307355L; public static void main(String[] args){AtomFrame testFrame = new AtomFrame();testFrame.start(); } public AtomFrame(){initializeComponents(); } public void initializeComponents(){this.setSize(400,400);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setTitle('Atom Launcher');this.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));progressBar = new JProgressBar();this.add(progressBar);//this.pack(); } public void start() {this.setVisible(true); } public void close() {this.dispose(); }}

類下載

package atomicElectronics.utility;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import javax.swing.JProgressBar;public class Download { private static final int BUFFER_SIZE = 4096; private JProgressBar fileProgressBar; public Download() { } public void fileProgressBar(JProgressBar fileBar) {fileProgressBar = fileBar; } public void exicute(String fileURL,String saveDir) throws IOException {URL url = new URL(fileURL);HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();int responseCode = httpConn.getResponseCode();// always check HTTP response code firstif (responseCode == HttpURLConnection.HTTP_OK) { String fileName = ''; String disposition = httpConn.getHeaderField('Content-Disposition'); String contentType = httpConn.getContentType(); double contentLength = httpConn.getContentLength(); if (disposition != null) {// extracts file name from header fieldint index = disposition.indexOf('filename=');if (index > 0) { fileName = disposition.substring(index + 9,disposition.length()); }} else { // extracts file name from URL fileName = fileURL.substring(fileURL.lastIndexOf('/') + 1,fileURL.length());}System.out.println('Content-Type = ' + contentType);System.out.println('Content-Disposition = ' + disposition);System.out.println('Content-Length = ' + contentLength);System.out.println('fileName = ' + fileName);// opens input stream from the HTTP connectionInputStream inputStream = httpConn.getInputStream();String saveFilePath = saveDir + File.separator + fileName;// opens an output stream to save into fileFileOutputStream outputStream = new FileOutputStream(saveFilePath);double totalRead = 0;int bytesRead = -1;byte[] buffer = new byte[BUFFER_SIZE];while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer,bytesRead); totalRead += bytesRead; System.out.println((totalRead / contentLength) * 100); fileProgressBar.setValue((int)((totalRead / contentLength) * 100));}outputStream.close();inputStream.close();System.out.println('File downloaded'); } else {System.out.println('No file to download. Server replied HTTP code: ' + responseCode); } httpConn.disconnect();}

}

標(biāo)簽: java
主站蜘蛛池模板: 一区二区播放 | 久草在线色站 | 97成人精品视频在线播放 | 国产成人精品日本亚洲专区6 | 欧美精品一区二区三区免费播放 | 欧美日韩在线播一区二区三区 | 久久视频精品53在线观看 | 午夜91理论片 | 亚洲另类激情综合偷自拍图 | 亚洲国产精品91 | av片免费大全在线观看不卡 | 欧美大屁股精品毛片视频 | 国产欧美一区二区三区免费 | 亚洲一区二区在线视频 | 国产免费专区 | 特黄特黄aaaa级毛片免费看 | 最新三级网站 | 国产欧美日韩视频免费61794 | 男人天堂网在线观看 | 色在线网站免费观看 | 视频一区在线播放 | 欧美日韩一| 久久精品国产免费一区 | 久久国产精品一区二区三区 | 日韩精品在线一区二区 | 男女超猛烈啪啦啦的免费视频 | 国产乱肥老妇精品视频 | 青青影院一区二区免费视频 | 国产精品99| 国产a国产片国产 | 国产成人免费在线观看 | 成人欧美一区二区三区视频xxx | 亚洲在线免费免费观看视频 | 国产成人黄网址在线视频 | 国产欧美日韩在线一区二区不卡 | 天堂成人av | 国产精品一区二区三区久久 | 欧美色xxx| 日本高清无吗免费播放 | 黄视频免费在线 | 午夜久久久久久久 |