可以下載檔案的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 />