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

📄 loader.java

📁 本程序在Unix_Linux环境下用Java语言编写的文件下载程序
💻 JAVA
字号:
package loader.loader;

import java.net.*;
import java.io.*;
import java.util.*;

import loader.viewer.*;
import loader.recorder.*;
import loader.controller.*;

/**

*/
public class Loader extends Thread {

	private int control_flag; 

	protected Viewer viewer;
	protected Recorder recorder;
	protected Properties properties;
	/**
		create a loader instance. 
		an unique name to identify this loader is neccessary.
	*/
	public Loader( String name ){
		super( name );

		control_flag = Controller.CONTINUE;
	}
	/**
		when loader is working, he must have a environment to be born and live.
		he get what he needs from properties parameter.
		here, i define the following properties this loader instance needs. 
		all of these are neccessary.

			ResourceUrl:	
			DestinationFile:	
			Range:		
			If-Range:	
			BufferSize: define the buffer size that this loader would be used.
	*/
	public void setProperties( Properties properties ) {
		this.properties = properties;
                Enumeration<Object> k = this.properties.keys();
                while( k.hasMoreElements() ){
                    String key = (String)k.nextElement();
                    System.out.println( key );
                    System.out.println( this.properties.getProperty( key ) );
                }
	}
	/**
		set viewer.
	*/
	public void setViewer( Viewer viewer ) {
		this.viewer = viewer;
	}
	/**
		set Recorder.
	*/
	public void setRecorder( Recorder recorder ) {
		this.recorder = recorder;
	}
	/**
		set controller would control this loader.
	*/
	public void setController( Controller controller ) {
		controller.register( this );
	}

	/**
		call by controller to control this loader.
	*/
	public int getControl_flag() {
		return this.control_flag;
	}
	public void setControl_flag( int control_flag ){
		this.control_flag = control_flag;
	}

	public void run(){

		HttpURLConnection urlConnection = null;
		InputStream input = null;
		RandomAccessFile fout = null;

		try{
			urlConnection = ( HttpURLConnection )getURL().openConnection();
			if( getIf_Range() != null ) {
				urlConnection.setRequestProperty( "If-Range", getIf_Range() );				
			}
			urlConnection.setRequestProperty( "Range", getRange() );
			urlConnection.connect();

			input = urlConnection.getInputStream();
			fout = new RandomAccessFile( getFile(), "rw" );
			fout.seek( fout.length() );
			
			byte[] buffer = new byte[getBufferSize()];
			int curLength = 0;
			
			while( ( this.control_flag == Controller.CONTINUE ) &&
				   ( ( curLength = input.read( buffer ) ) != -1 ) ) {
				fout.write( buffer, 0, curLength );
				
				recorder.log( getName(), curLength );
				viewer.show( curLength );
			}
		}catch( Exception e ){
			e.printStackTrace();
		}finally{
			try{
				if( input != null ){
					input.close();
				}
				if( fout != null ){
					fout.close();
				}
				if( urlConnection != null ){
					urlConnection.disconnect();
				}
			}catch( Exception ee ){
			}
                        System.out.println( "exit thread: " + this.getName() );
		}
	}
	/******************** helper class ********************/
	private URL getURL() {
		URL url = null;
		try{
			url = new URL( this.properties.getProperty( "ResourceUrl" ) );
		} catch( MalformedURLException e  ) {
		}
		return url;
	}
	private File getFile() {
		File file = null;
		file = new File( this.properties.getProperty( "tmpDestinationFile" ) );
		return file;
	}
	private String getRange() {
		return this.properties.getProperty( "Range" );
	}
	private String getIf_Range() {
		return this.properties.getProperty( "If-Range" );
	}
	private int getBufferSize() {
		int s = 512;
		try{
			s = Integer.parseInt( this.properties.getProperty( "BufferSize" ) );
		}catch( Exception e ){}

		return s;
	}
}

⌨️ 快捷键说明

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