首页>代码>java swing基于p2p实现文件传输>/EasyTranfer_02/src/com/im/FileSendTask.java
package com.im;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.util.List;

import com.im.util.IConstant;
import com.im.util.TimeUnit;
import com.im.util.Utils;
/**
 * @author xblia
 * 2014井12冥12耒
 */
public class FileSendTask extends Thread
{

	private IFileTransObserver fileTranObserver;
	private SocketChannel socketChannel;
	
	private FilesMgr filesMgr;
	private boolean isRunning;
	
	public FileSendTask(IFileTransObserver fileTranObserver, SocketChannel socketChannel)
    {
	    super();
	    this.fileTranObserver = fileTranObserver;
	    this.socketChannel = socketChannel;
	    this.isRunning = true;
	    this.filesMgr = FilesMgr.getInstance();
    }

	@Override
	public void run()
	{
		while(isRunning)
		{
			List<File> files = filesMgr.getFiles();
			if(files.isEmpty())
			{
				try
                {
	                Thread.sleep(TimeUnit.ONE_SECOND);
                } catch (InterruptedException e)
                {
	                e.printStackTrace();
                }
				continue;
			}
			boolean isMsg = false;
			for (int i = 0; i < files.size(); i++)
            {
				if(isRunning)
				{
					if(files.get(i).getName().contains(IConstant.MSG_FILE_IDENTIFIED))
					{
						String msg = files.get(i).getName().replace(IConstant.MSG_FILE_IDENTIFIED, "");
						outputMsg(msg);
						fileTranObserver.notifyMsg(msg, false);
						isMsg = true;
					}else
					{
						fileTranObserver.notifyNextFile(files.get(i).getAbsolutePath());
						fileTranObserver.notifyProgress(files.size(), i+1, ((i+1)*1.0)/files.size() * 100);
						scannChildAndTrans(files.get(i), files.get(i).getParent());
					}
				}
            }
			if(!isMsg)
			{
				fileTranObserver.notifyFinished("");
			}
			files.clear();
		}
	}
	
	private void scannChildAndTrans(File file, String baseFolder)
    {
		if(file.isDirectory())
		{
			File [] files = file.listFiles();
			for (File file2 : files)
            {
				scannChildAndTrans(file2, baseFolder);
            }
		}else
		{
			outputFile(file, baseFolder);
		}
    }
	
	private void outputMsg(String msg)
    {
		byte []data = msg.getBytes();
		ByteBuffer buff = ByteBuffer.allocate(data.length + (Integer.SIZE / 8) * 2);
		buff.putInt(IConstant.MSG_HEADER_IDENTIFIED);// identified.
		buff.putInt(data.length);
		buff.put(data);
		buff.flip();
		try
        {
	        sendMsg(socketChannel, buff);
        } catch (Exception e)
        {
	        e.printStackTrace();
        }
    }

	private void outputFile(File file, String basePath)
	{
		FileInputStream fin = null;
		try
		{
			{//Header.
				String fileRelativePath = file.getAbsolutePath().replace(basePath, "");
				
				ByteBuffer buff = ByteBuffer.allocate(255 + 4 + 8);
				buff.putInt(IConstant.FILE_HEADER_IDENTIFIED);// identified.
				buff.putInt(fileRelativePath.getBytes().length);
				buff.put(fileRelativePath.getBytes());
				buff.putLong(file.length());
				buff.flip();
				sendMsg(socketChannel, buff);
			}
			
			// FileRead and socket write
			fin = new FileInputStream(file);
			int iReadLen = -1;
			do
			{
				ByteBuffer fileData = ByteBuffer.allocate(1024);
				FileChannel fileChannel = fin.getChannel();
				iReadLen = fileChannel.read(fileData);
				fileData.flip();
				sendMsg(socketChannel, fileData);
			} while (iReadLen != -1);
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			Utils.closeResource(fin);
		}
	}
	
	public void sendMsg(SocketChannel channel, ByteBuffer buff) throws Exception
	{
		int iTotalLen = buff.limit();
		int iSendLen = 0;
		while(iSendLen < iTotalLen)
		{
			iSendLen += channel.write(buff);
			buff.position(iSendLen);
		}
	}

	public void shutDown()
	{
		this.isRunning = false;
		if(null != socketChannel)
		{
			try
            {
				socketChannel.close();
            } catch (IOException e)
            {
	            e.printStackTrace();
            }
		}
	}
}
最近下载更多
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日
天助我也  LV1 2021年5月21日
最近浏览更多
interface  LV22 2月25日
WindSky2333  LV1 2023年12月19日
skiler  LV4 2023年9月23日
fewfsdaf  LV4 2023年4月16日
a1677596408  LV23 2022年7月12日
陈小灏  LV14 2022年5月23日
sswert  LV2 2022年3月18日
zhendong  LV7 2022年3月10日
yuxinnan  LV4 2022年2月23日
iRichard1314  LV6 2022年2月15日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友