java–下載器

來源:互聯網
上載者:User


可以下載檔案的java程式

import java.awt.GridLayout;<br />import java.awt.event.ActionEvent;<br />import java.awt.event.ActionListener;<br />import java.io.File;<br />import java.io.FileInputStream;<br />import java.io.FileOutputStream;<br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.net.HttpURLConnection;<br />import java.net.URL;<br />import javax.swing.JButton;<br />import javax.swing.JFrame;<br />import javax.swing.JLabel;<br />import javax.swing.JPanel;<br />import javax.swing.JTextField;<br />public class DownSomething extends JFrame implements ActionListener{</p><p>/**<br />JTextField field = new JTextField(20);<br />JLabel label = new JLabel("請輸入檔案的url:");<br />JLabel label2 = new JLabel("檔案大小:");<br />JLabel label4 = new JLabel("已下載:");<br />JLabel label5 = new JLabel("0.0%");<br />JLabel label3 = new JLabel("0.0M");<br />JButton button = new JButton("下載");<br />JPanel panel = new JPanel();<br />JPanel panel2 =new JPanel();<br />public DownSomething() {<br />panel.add(label);<br />panel.add(field);<br />panel2.add(label2);<br />panel2.add(label3);<br />panel2.add(label4);<br />panel2.add(label5);<br />panel2.add(button);<br />this.add(panel);<br />this.add(panel2);<br />this.setLayout(new GridLayout(2 ,1));<br />button.addActionListener(this);<br />this.pack();<br />this.show();<br />}<br />**/<br />JLabel label1 = new JLabel("已下載:");<br />JLabel label2 = new JLabel("0.0%");<br />JPanel panel = new JPanel();<br />public DownSomething() {<br />panel.add(label1);<br />panel.add(label2);<br />this.add(panel);<br />this.setLayout(new GridLayout(1 ,1));<br />this.setResizable(false);<br />this.pack();<br />this.show();<br />}<br />/**<br /> *<br /> * @param url 要下載檔案的路徑<br /> * @param fileUrl 要儲存該檔案的路徑<br /> * @throws IOException<br /> * @throws InterruptedException<br /> */<br />private int fileLength;<br />private double downLength;<br />void download(String url, String fileUrl) throws IOException, InterruptedException<br />{<br />URL u = new URL(url);<br /> HttpURLConnection urlcon=(HttpURLConnection)u.openConnection();<br /> //根據響應擷取檔案大小<br /> fileLength=urlcon.getContentLength();<br /> System.out.println("檔案大小:"+Math.ceil(fileLength / 1024 / 1024) +"M");<br /> //label3.setText("aaa");<br /> File file = new File(fileUrl);<br /> InputStream in = u.openStream();<br />FileOutputStream fou = new FileOutputStream(file);<br />System.out.println("DownLoading...");<br />int c = 0;</p><p>while((c = in.read()) > -1)<br />{<br />FileInputStream fi = new FileInputStream(file);<br />downLength = Math.ceil(fi.available());<br />fou.write(c);<br />//System.out.println("已下載:"+Math.ceil((downLength / fileLength ) * 100) +"%");<br />label2.setText(Math.ceil((downLength / fileLength ) * 100) +"%");<br />//label5.setText(Math.ceil((downLength / fileLength ) * 100) +"%");<br />}<br />System.out.println("DownLoad Completed");</p><p>}<br />public static void main(String[] args) {<br />DownSomething downSomething = new DownSomething();</p><p>try {<br />try {<br />downSomething.download("http://himusic.org/sample/Sample_M/Track36.mp3","C://Documents and Settings//Administrator//案頭//"+System.currentTimeMillis()+".mp3");<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}<br />} catch (IOException e) {<br />e.printStackTrace();<br />}<br />}<br />/**<br />public void actionPerformed(ActionEvent e) {<br />String str = "";<br />if(e.getSource() == button)<br />{<br />if(field.getText().contains("."))<br />{<br />str = field.getText().substring(field.getText().lastIndexOf(".") );<br />}<br />try {<br />this.download(field.getText(), "C://Documents and Settings//Administrator//案頭//"+System.currentTimeMillis()+str);<br />} catch (IOException e1) {<br />e1.printStackTrace();<br />} catch (InterruptedException e1) {<br />e1.printStackTrace();<br />}<br />}<br />}<br />**/<br />public void actionPerformed(ActionEvent e) {<br />// TODO Auto-generated method stub</p><p>}</p><p>}<br />
  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.