www.pudn.com > FileShare.rar > SendDownloadFile_thread.java


/* 
 * SendDownloadFile_thread.java 
 * 
 * Created on 2008年4月25日, 下午9:07 
 * 
 * To change this template, choose Tools | Template Manager 
 * and open the template in the editor. 
 */ 
 
/** 
 * 
 * @author 602 
 */ 
import java.io.*; 
import java.net.*; 
 
public class SendDownloadFile_thread extends Thread  
{ 
    /** Creates a new instance of SendDownloadFile_thread */ 
    String downloadFileName=""; 
    String downloadFilePath=""; 
 
    public SendDownloadFile_thread(String fp,String fn) 
    { 
        
       downloadFileName=fn; 
       downloadFilePath=fp; 
    } 
     
    public void run() 
    { 
        while(true) 
        { 
            try 
            { 
                ServerSocket server_socket=new ServerSocket(9000);//9000端口用于传输下载文件 
                Socket client=server_socket.accept(); 
 
                File file=new File(downloadFilePath,downloadFileName); 
                FileInputStream fileIn=new FileInputStream(file); 
 
                DataOutputStream out=null; 
                out=new DataOutputStream(client.getOutputStream()); 
 
                byte[] buf=new byte[4096]; 
                int num=fileIn.read(buf); 
                while(num!=-1) 
                { 
                   out.write(buf); 
                   num=fileIn.read(buf); 
                } 
                fileIn.close(); 
		out.close(); 
            } 
            catch(Exception e) 
            { 
                 
//                System.out.println("se"); 
                break; 
            } 
        } 
    } 
}