您好,登錄后才能下訂單哦!
- final JButton button = new JButton("下載");
- button.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- new Thread()
- {
- @Override
- public void run()
- {
- button.setEnabled(false);//設(shè)置按鈕在下載過程中不可操作
- FileOutputStream fos = null;
- try
- {
- URL url = new URL(txtFilefjavaindexhtml.getText());//建立HTTP連接
- URLConnection conn = url.openConnection();//創(chuàng)建連接對象
- int fileSize = conn.getContentLength();//用getContentLength()得到連接對象的文件大小
- InputStream is = conn.getInputStream();
- byte[] buffer = new byte[1024];
- int length = 0;
- fos = new FileOutputStream(txtFilefjavaindexhtml_1.getText());
- double downSize = 0;
- while (-1 != (length = is.read(buffer)))
- {
- downSize += length;
- double percent = downSize * 100 / fileSize;//計算下載百分比
- fos.write(buffer, 0, length);
- progressBar.setValue((int) percent);//設(shè)置進度條變化
- }
- }
- catch (MalformedURLException e1)
- {
- e1.printStackTrace();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- finally//輸出流要記得在fanally關(guān)掉
- {
- button.setEnabled(true);//在finally中恢復(fù)按鈕可操作
- if (null != fos)
- {
- try
- {
- fos.close();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- }
- }
- }
- }.start();
- }
- });
- button.setFont(new Font("宋體", Font.PLAIN, 24));
- button.setBounds(10, 164, 97, 37);
- frame.getContentPane().add(button);
- progressBar = new JProgressBar();
- progressBar.setBounds(152, 164, 272, 37);
- frame.getContentPane().add(progressBar);//進度條按鈕
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。