• 开发语言:
  • Java
  • 源码大小:
  • 0.17M
  • 源码类别:
  • 常规Java编程
  • 文件格式:
  • .rar

源码介绍

【源码简介】简单实现网络快传
【源码截图】

【核心源码】

/** 
 * 接收文件服务 
 * @author admin_Hzw 
 * 
 */  
class Server extends Thread
{
    static public String pathdir;
    public void setPathdir(String s)//设置文件路径方法
    {
    pathdir=s;
    }
    public void run()//run方法
    {
ServerSocket server = null;
try
{
server = new ServerSocket(9999);
        while (!Thread.currentThread().isInterrupted())//如果线程没有中断
        {
            Socket socket = server.accept();//等待连接,一直阻塞
            byte[] inputByte = null;//定义输入字节数组
            int length = 0;//定义长度
            double sumL=0;//进度条参数
            long l;//文件长度
            DataInputStream dis = null;  
            FileOutputStream fos = null;  
            String filePath;
            try
            {  
                dis = new DataInputStream(socket.getInputStream());
                File f = new File(pathdir);//新建文件f
                if(!f.exists())
                {  
                    f.mkdir();
                }  
                filePath=pathdir "/" dis.readUTF();//得到文件路径
                l=dis.readLong();//获取文件大小
                fos = new FileOutputStream(new File(filePath));//文件输出流
                inputByte = new byte[1024];
                inframe in = new inframe();
                in.news.setText("正在接收文件...   " filePath);//进度条显示框
                while ((length = dis.read(inputByte, 0, inputByte.length)) > 0)
                { 
                sumL = length;
                in.value.setText("已传输:" (int)((sumL/(l*1024))*100) "%");
                in.jpb.setValue((int)((sumL/(l*1024))*100) 1);
                    fos.write(inputByte, 0, length);  
                    fos.flush();
                }  
                in.dispose();//关闭进度条框
            }
            finally//关闭流及socket
            {  
                if (fos != null)  
                    fos.close();  
                if (dis != null)  
                    dis.close();  
                if (socket != null)  
                    socket.close();   
            }
        }
}
        catch (Exception e)
        {  
        Thread.currentThread().interrupt();
            e.printStackTrace();
        }
    }
}  

  • 商品评价
  • 交易规则