⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downsys.java

📁 编写一个下载程序。要求: (1)一个文本文件里存储了要下载的文件地址(可以先搜集若干个Mp3文件的下载地址)
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

    


class ChoiceF extends Frame implements ActionListener 
{
	
	Label l1;
	FileDialog fd;
	Button b1,b2;
	TextArea a1;
	
	String s1,s2,s3;
	String sd[]=new String[100];//此数组存放地址列表
	String sf[]=new String[100];//此数组为辅助数组
	String sf1[]=new String[100];//此数组存放文件名
	File file1[]=new File[100];
	int chs[]=new int[100];
	int k=0;

	URL url;
	
	public void ChoiceF()
	{
		setSize(600,500);
		this.setVisible(true);
		fd=new FileDialog(this,"FileOpen");
		l1=new Label("请点击打开选择含有下载地址的文档,每个地址一行:");
		b1=new Button("打开");
		b2=new Button("开始下载");
		a1=new TextArea();
		setLayout(null);
		l1.setBounds(50,100,280,20);
		b1.setBounds(335,100,40,20);
		b2.setBounds(330,440,60,20);
		a1.setBounds(50,130,450,300);
		
		add(l1);
		add(b1);
		add(b2);
		add(a1);
		
		b2.setEnabled(false);
		b1.addActionListener(this);
		b2.addActionListener(this);
		
		this.addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent e)
	      {
		System.exit(0);
	      }
		});
	}
	class thread //extends Thread//实现文件下载
	{
		String s;
		int z;		
		public thread(String s,int z)
		{
			this.s=s;
			this.z=z;
		}
		public void run()
		{
			try
       	     {
       	     url=new URL(s);
       	     URLConnection conn=url.openConnection();
       	     conn.connect();
       	     sf[z]=url.getFile().toString();
       	     chs[z]=sf[z].lastIndexOf("/");
       	     chs[z]++;
       	     sf1[z]=sf[z].substring(chs[z]);//获取文件名       	     
       	     DataInputStream in = new DataInputStream(conn.getInputStream()); 
       	     file1[z]=new File("d:\\"+sf1[z]);
       	   	 DataOutputStream out=new DataOutputStream(new FileOutputStream(file1[z]));        		 
             int j=0;
             byte b[]=new byte[2048];
             a1.append("\n"+sf1[z]+"下载中>>>>>");             
             while((j=in.read(b))!=-1){        //传输数据     	
             	out.write(b,0,b.length);
                
              }
              out.flush();
             a1.append("\n"+sf1[z]+"下载完毕");
             in.close();
         	 out.close();
         	  	 
       		 }
       		 catch(FileNotFoundException ef)
       		 {
       		 	a1.append("\n"+"此文件可能有同名文件在使用中");
       		 }
       		 catch(UnknownHostException eu)
       		 {
       		 	a1.append("\n"+"请检查网络......");
       		 }
       		 catch(MalformedURLException ey)
       		 {
       		 	a1.append("\n"+"地址文件有错误!~请检查......");
       		 
       		 }
       		 catch(Exception e2)
       		 {
       			
       		 }
       		
       
       		}	 
		}
		

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==b1)//读取地址,在文本区域中显示
		{				
		int i=0;
       	while(sd[i]!=null&&i<100)
       	{
       	  sd[i]=null;
       	  i++;
       	}
		fd.setVisible(true);
		s1=fd.getDirectory();
		s2=fd.getFile();	
		a1.setText("");
	    try{
		FileReader fr=new FileReader(s1+s2); 
		BufferedReader br=new BufferedReader(fr);
		String Line=br.readLine(); 
		sd[i++]=Line;
		a1.setText("");
		while(Line!=null)
		{
		a1.append(Line+"\n");
		Line=br.readLine();
		sd[i++]=Line;
		}
		br.close(); 
		fr.close();
		}
        catch(FileNotFoundException eff)
        {b2.setEnabled(false);
         a1.setText("没打开文件");
        }
        catch(Exception e1){
        	System.out.println(e1.toString());
        	
        }
        if(a1.getText()!="没打开文件"){
        	b2.setEnabled(true);
        }
       }
       if(e.getSource()==b2)//开始下载
       {
       	    int m=0;
       	        
				
			b2.setEnabled(false);		
			thread th[]=new thread[100];			
	 		while(sd[m]!=null){
	 		th[m]=new thread(sd[m],m);
	 		th[m].run();	 			
	 		m++;
	 		}    
	 		    
	 		a1.append("\n"+"全部下完,谢谢!~");
	 		     
	 		int i=0;
       	    while(sd[i]!=null&&i<=m)
       	    {
       	    	sd[i]=null;
       	    	i++;
       	    }
       	    
       	    
       }
}}

public class DownSys
{
	public static void main(String args[])
	{   
	    
	    ChoiceF f1=new ChoiceF();
		f1.ChoiceF();
		
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -