您好,登錄后才能下訂單哦!
- final JButton button = new JButton("下載");
- button.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- new Thread()
- {
- @Override
- public void run()
- {
- button.setEnabled(false);//設置按鈕在下載過程中不可操作
- FileOutputStream fos = null;
- try
- {
- URL url = new URL(txtFilefjavaindexhtml.getText());//建立HTTP連接
- URLConnection conn = url.openConnection();//創建連接對象
- 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);//設置進度條變化
- }
- }
- catch (MalformedURLException e1)
- {
- e1.printStackTrace();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- finally//輸出流要記得在fanally關掉
- {
- button.setEnabled(true);//在finally中恢復按鈕可操作
- 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);//進度條按鈕
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。