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


/* 
 * ReceiveDownloadFile_thread.java 
 * 
 * Created on 2008年4月25日, 下午9:27 
 * 
 * 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 ReceiveDownloadFile_thread extends Thread 
{ 
    /** Creates a new instance of ReceiveDownloadFile_thread */ 
    String downloadFileName=""; 
    String downloadFilePath=""; 
    String desIP=""; 
    public ReceiveDownloadFile_thread(String fp,String fn)  
    { 
        downloadFileName=fn; 
        downloadFilePath=fp; 
    } 
     
    public void run() 
    { 
        try 
        { 
            desIP=MainJFrame.jTextField1.getText(); 
            Socket server=new Socket(desIP,9000);//9000端口用于传输下载文件 
            File file=new File(downloadFilePath,downloadFileName); 
            if(file.exists()) 
            { 
                file.delete(); 
            } 
            file.createNewFile(); 
            RandomAccessFile wFile=new RandomAccessFile(file,"rw"); 
             
             
            DataInputStream in=null; 
            in=new DataInputStream(server.getInputStream()); 
 
            byte[] buf=new byte[4096]; 
            int num=in.read(buf); 
 
            while(num!=(-1)) 
            { 
                    wFile.write(buf); 
                    num=in.read(buf); 
            } 
            MainJFrame.jLabel2.setText("下载完毕"); 
            in.close(); 
            wFile.close(); 
        } 
        catch(Exception e) 
        {System.out.println("re");} 
    } 
}