首页>代码>java swing基于p2p实现文件传输>/EasyTranfer_02/src/com/im/FileReciveTask.java
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(socket, IConstant.LEN_LONG);
	        	long fileSize = fileheadBuff.getLong();
	        	
	        	if(fileName != null && fileSize > 0)
	        	{
	        		beginSaveFile(fileName, fileSize, socketChannel);
	        		
	        	}
	        }
        } catch (IOException e)
        {
	        e.printStackTrace();
        } catch (Exception e)
        {
	        e.printStackTrace();
        }
    }
	
	private void beginSaveFile(String fileName, long fileSize, SocketChannel socket)
    {
	    String fullPath = IConstant.BASE_PATH + File.separator + "recive_folder" + File.separator + fileName;
	    File baseDir = new File(new File(fullPath).getParent());
	    if(!baseDir.exists())
	    {
	    	baseDir.mkdirs();
	    }
	    FileOutputStream fos = null;
	    FileChannel channel = null;
	    try
        {
	    	fos = new FileOutputStream(fullPath);
	    	channel = fos.getChannel();
	        long iReadLen = 0;
	        do
	        {
	        	int iNeedBuff = 1024;
	        	if((fileSize - iReadLen) < 1024)
	        	{
	        		iNeedBuff = (int)(fileSize - iReadLen);
	        	}
	        	iReadLen += iNeedBuff;
	        	fileTranObserver.notifyProgress(1, 1, ((iReadLen*1.0)/fileSize) * 100);
	        	ByteBuffer fileData = readMsg(socket, iNeedBuff);
	        	channel.write(fileData);
	        }
	        while (iReadLen < fileSize);
        } catch (FileNotFoundException e)
        {
	        e.printStackTrace();
        } catch (Exception e)
        {
	        e.printStackTrace();
        }finally
        {
        	Utils.closeResource(fos);
        }
	    
    }

	public ByteBuffer readMsg(SocketChannel channel, int iTotalLen) throws Exception
	{
		int iReadLen = 0;
		ByteBuffer buff = ByteBuffer.allocate(iTotalLen);
		while(iReadLen < iTotalLen)
		{
			iReadLen += channel.read(buff);
			buff.position(iReadLen);
		}
		buff.flip();
		return buff;
	}

	public void shutDown()
	{
		this.isRunning = false;
		if(null != socketChannel)
		{
			try
            {
				socketChannel.close();
            } catch (IOException e)
            {
	            e.printStackTrace();
            }
		}
	}
}
最近下载更多
hoictas  LV2 2024年11月26日
WindSky2333  LV1 2023年12月19日
yuxinnan  LV4 2022年2月23日
iRichard1314  LV6 2022年2月15日
MineCraft小玄易  LV1 2021年12月6日
zhanghl123  LV2 2021年11月17日
湫兮如风  LV1 2021年11月10日
xjf123  LV1 2021年11月3日
songronghu  LV2 2021年7月14日
1234567sc  LV3 2021年6月22日
最近浏览更多
hoictas  LV2 2024年11月26日
PSSDZH  LV3 2024年6月14日
interface  LV22 2024年2月25日
WindSky2333  LV1 2023年12月19日
skiler  LV4 2023年9月23日
fewfsdaf  LV4 2023年4月16日
a1677596408  LV23 2022年7月12日
陈小灏  LV18 2022年5月23日
sswert  LV2 2022年3月18日
zhendong  LV7 2022年3月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友