receivedownloadfile_thread.java

来自「局域网p2p文件传输」· Java 代码 · 共 69 行

JAVA
69
字号
/*
 * 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");}
    }
}






⌨️ 快捷键说明

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