• 大小: 1.07MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-08-17
  • 语言: Java
  • 标签: java聊天  

资源简介

用java的NIO技术编写的 1. 支持聊天功能 2. 拖拽文件可以实现文件传输功能,也可以是文件夹 3. 启动时可以选择server端或client端启动 4. 本人原创,学习NIO和java的网络通信一定会不错 5. 下载下来可以直接导致eclipse,然后运行

资源截图

代码片段和文件信息

package com.im;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

import com.im.util.IConstant;
import com.im.util.Utils;
/**
 * @author xblia
 * 2014年12月12日
 */
public class FileReciveTask extends Thread
{
private IFileTransObserver fileTranObserver;
private SocketChannel socketChannel;

private boolean isRunning;

public FileReciveTask(IFileTransObserver fileTranObserver SocketChannel socketChannel)
    {
    super();
    this.fileTranObserver = fileTranObserver;
    this.socketChannel = socketChannel;
    this.isRunning = true;
    }

@Override
public void run()
{
while(isRunning)
{
ByteBuffer headerBuff = ByteBuffer.allocate(4);
try
            {
            int iDentified= socketChannel.read(headerBuff);
            if(iDentified == -1)
            {
             break;
            }
            if(iDentified == IConstant.LEN_INT)
            {
             headerBuff.flip();
             int iValue = headerBuff.getInt();
             if(iValue == IConstant.FILE_HEADER_IDENTIFIED)
             {
             parseFileHeader(socketChannel);
             }else if(iValue == IConstant.MSG_HEADER_IDENTIFIED)
             {
             parseMsg(socketChannel);
             }
             else
             {
             continue;
             }
            }
            } catch (IOException e)
            {
            e.printStackTrace();
            break;
            }

}
}

private void parseMsg(SocketChannel socket)
    {
ByteBuffer buff = ByteBuffer.allocate(IConstant.LEN_INT);
try
        {
        socket.read(buff);
        buff.flip();
        int iMsgLen = buff.getInt();
        if(iMsgLen != -1)
        {
         buff = readMsg(socket iMsgLen);
        
         byte []data = new byte[iMsgLen];
         buff.get(data);
         String strMsg = new String(data);
         fileTranObserver.notifyMsg(strMsg true);
        
        }
        } catch (IOException e)
        {
        e.printStackTrace();
        } catch (Exception e)
        {
        e.printStackTrace();
        }
    }

private void parseFileHeader(SocketChannel socket)
    {
ByteBuffer fileheadBuff = ByteBuffer.allocate(IConstant.LEN_INT);
try
        {
        socket.read(fileheadBuff);
        fileheadBuff.flip();
        int iFileNameLen = fileheadBuff.getInt();
        if(iFileNameLen != -1)
        {
         fileheadBuff = readMsg(socket iFileNameLen);
        
         byte []data = new byte[iFileNameLen];
         fileheadBuff.get(data);
         String fileName = new String(data);
         fileTranObserver.notifyNextFile(fileName);
        
         fileheadBuff = readMsg(sock

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-12-16 18:07  EasyTranfer_02\
     文件         363  2014-12-12 14:35  EasyTranfer_02\.classpath
     文件         390  2014-12-16 14:35  EasyTranfer_02\.project
     目录           0  2014-12-16 14:35  EasyTranfer_02\.settings\
     文件         598  2014-12-11 11:18  EasyTranfer_02\.settings\org.eclipse.jdt.core.prefs
     目录           0  2014-12-16 15:17  EasyTranfer_02\bin\
     目录           0  2014-12-16 15:17  EasyTranfer_02\bin\com\
     目录           0  2014-12-16 15:17  EasyTranfer_02\bin\com\im\
     文件        4595  2014-12-16 17:47  EasyTranfer_02\bin\com\im\FileReciveTask.class
     文件        4859  2014-12-16 17:54  EasyTranfer_02\bin\com\im\FileSendTask.class
     文件        1251  2014-12-16 15:17  EasyTranfer_02\bin\com\im\FilesMgr.class
     文件         303  2014-12-16 17:45  EasyTranfer_02\bin\com\im\IFileTransObserver.class
     文件         193  2014-12-16 15:17  EasyTranfer_02\bin\com\im\ISocketOberser.class
     文件        2287  2014-12-16 17:25  EasyTranfer_02\bin\com\im\TranferClient.class
     文件        4338  2014-12-16 17:25  EasyTranfer_02\bin\com\im\TranferServer.class
     目录           0  2014-12-16 17:14  EasyTranfer_02\bin\com\im\ui\
     文件        2080  2014-12-16 18:04  EasyTranfer_02\bin\com\im\ui\EasyTransferMainframe$1.class
     文件       10458  2014-12-16 18:04  EasyTranfer_02\bin\com\im\ui\EasyTransferMainframe.class
     文件         164  2014-12-16 15:17  EasyTranfer_02\bin\com\im\ui\ITextAreaTask.class
     文件        1889  2014-12-16 16:45  EasyTranfer_02\bin\com\im\ui\JTextPaneEx.class
     文件         711  2014-12-16 16:41  EasyTranfer_02\bin\com\im\ui\TextAreaByMenu$1.class
     文件         711  2014-12-16 16:41  EasyTranfer_02\bin\com\im\ui\TextAreaByMenu$2.class
     文件         711  2014-12-16 16:41  EasyTranfer_02\bin\com\im\ui\TextAreaByMenu$3.class
     文件         711  2014-12-16 16:41  EasyTranfer_02\bin\com\im\ui\TextAreaByMenu$4.class
     文件         711  2014-12-16 16:41  EasyTranfer_02\bin\com\im\ui\TextAreaByMenu$5.class
     文件        4731  2014-12-16 16:41  EasyTranfer_02\bin\com\im\ui\TextAreaByMenu.class
     目录           0  2014-12-16 15:17  EasyTranfer_02\bin\com\im\util\
     文件         782  2014-12-16 17:58  EasyTranfer_02\bin\com\im\util\IConstant.class
     文件        1084  2014-12-16 15:17  EasyTranfer_02\bin\com\im\util\T.class
     文件         205  2014-12-16 15:17  EasyTranfer_02\bin\com\im\util\TimeUnit.class
     文件        1939  2014-12-16 17:57  EasyTranfer_02\bin\com\im\util\Utils.class
............此处省略22个文件信息

评论

共有 条评论