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

📄 search.java

📁 java 多线程文件下载器源代码......
💻 JAVA
字号:
package com.sam.net.download;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Observable;
import java.util.concurrent.ExecutionException;

import javax.swing.JOptionPane;


public class Search extends Observable implements Runnable{
	public static final String STATUSES[]={"Downloading","Paused","Complete"};
    public static final int DOWNLOADING=0;
    public static final int PAUSED=1;
    public static final int COMPLETE=2;  
	int status;
	private String filename;
	private URL url;
	private long fileLength;
    public int downloaded;
	private String filetype;
	private String time;
	
	public Search(URL url)
	{
	  status=PAUSED;
	  filename="";
	  this.url = url;
	  fileLength=-1;
      downloaded=0;
	  filetype="";
	  time="";
	  search();
	}
	
	public void search(){
	      Thread thread = new Thread(this);
	      thread.start();
	}

	public int getStatus() {
		return status;
	}

	public String getFilename() {
		return filename;
	}

	public String getUrl() {
		return url.toString();
	}

	public long getFileLength() {
		return fileLength;
	}

	public long getDownloaded() {
		return downloaded;
	}

	public String getFiletype() {
		return filetype;
	}

	public String getTime() {
		if (time == "")
			return "00:00:00";
		else
		   return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public float getProgress() {
		return ((float)downloaded/fileLength)*100;
	}
    
	public void run() {
		this.filename = url.getFile().substring(url.getFile().lastIndexOf("/")+1);
		this.filetype = url.getFile().substring(url.getFile().lastIndexOf(".")+1)+"文件";
		HttpURLConnection huc = null;
		try {
			huc = (HttpURLConnection) url.openConnection();
	        if (huc.getContentLength() != -1 && huc.getResponseCode()<400)
			    {
	        	this.fileLength = huc.getContentLength();
	            stateChanged();
			    }
	        else 
		     {JOptionPane.showMessageDialog(null, "连接错误,请检查网络连接!", "错误信息", JOptionPane.ERROR_MESSAGE); 
		     }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.filetype = url.getFile().substring(url.getFile().lastIndexOf(".")+1)+"文件";
        UseTime useTime = new UseTime(status);
		this.time = useTime.getUseTime();
	}
	
   public  void stateChanged() {
        setChanged();
        notifyObservers();
    }
}

⌨️ 快捷键说明

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