发布时间 : 星期一 文章java文件传输更新完毕开始阅读b1fdc00af12d2af90242e60d
{
socketChannel.register(selector,
SelectionKey.OP_READ|SelectionKey.OP_WRITE);//注册事件 while(selector.select()>0)
{
Set readyKeys=selector.selectedKeys(); Iterator it=readyKeys.iterator(); while(it.hasNext()) {
SelectionKey key=null; try { }
catch(InterruptedException e) { }
catch(FileNotFoundException e) { }
catch(IOException e) {
e.printStackTrace(); try { {
ee.printStackTrace(); if(key!=null)
key.cancel(); key.channel().close();
System.out.println(\文件不存在!\); continue;
e.printStackTrace();
key=(SelectionKey)it.next(); it.remove();
if(key.isReadable()) { }
if(key.isWritable()) { }
Thread.sleep(100);
send(key); receive(key);
}catch(Exception ee)
}
}
}
}
}
public void send(SelectionKey key)throws IOException {
String subCMD;
synchronized(sendCMDBuffer) {
sendCMDBuffer.flip();
String CMD=decode(sendCMDBuffer);
if(CMD.indexOf(\)==-1)//还没有完整的命令输入 { }
subCMD=CMD.substring(0,CMD.indexOf(\)); sendCMDBuffer.compact();
sendCMDBuffer.limit(sendCMDBuffer.capacity());//把极限设return;
置为容量
sendCMDBuffer.put(encode(CMD.substring(CMD.indexOf(\)+4)));
socketChannel.write(encode(\+String.valueOf(bytePath)+\+
System.out.println(\文件大小:\+bytes+\字节\); FileChannel fic=fis.getChannel(); fic.position(0);
ByteBuffer buffer=ByteBuffer.allocate(1024*1024); int byteRead=0;
System.out.println(\文件正在发送!\); while(byteRead>=0) {
byteRead=fic.read(buffer);
}
if(subCMD.substring(0,3).equals(\))//如果是SET命令 {
String path=subCMD.substring(4);
int bytePath=path.getBytes(\).length;//获取文件名字节数 FileInputStream fis=new FileInputStream(FILE_PATH+path); int bytes=fis.available();//获得文件总字节数
path+String.valueOf(bytes)+\));//发送命令
if(byteRead==-1)
break; buffer.flip();
while(buffer.hasRemaining())
socketChannel.write(buffer);
buffer.compact();
}
System.out.println(path+\文件发送完成!\); } else
if(subCMD.substring(0,3).equals(\)) {
String path=subCMD.substring(4);
int bytePath=path.getBytes(\).length;
socketChannel.write(encode(\+String.valueOf(bytePath)+\+
path));//发送命令 }
}
public void receive(SelectionKey key)throws IOException {
int bytes;
ByteBuffer buffer;
buffer=ByteBuffer.allocate(3); socketChannel.read(buffer); buffer.flip();
String flag=decode(buffer); if(flag.equals(\)) { }
System.out.println(\服务器不存在该文件!\); return;
else { }
socketChannel.write(encode(subCMD)); socketChannel.close(); key.cancel();
System.out.print(\退出程序\); System.exit(0);
bytes=getFileNameLength(socketChannel);//获得文件大小 System.out.println(\文件大小:\+bytes+\字节\);
int byteFileName=getFileNameLength(socketChannel);//文件名大小 buffer=ByteBuffer.allocate(byteFileName); while(buffer.position()!=byteFileName) socketChannel.read(buffer); buffer.flip();
String FileName=decode(buffer);// 获得文件名 buffer=ByteBuffer.allocate(1024*1024);
FileOutputStream fio=new
FileChannel fic=fio.getChannel(); fic.position(0);
System.out.println(\正在接收文件!\); while(bytes>0) { }
if(bytes<1024*1024) { }
socketChannel.read(buffer);
int position=buffer.position();//记录当前读到的文件长度 buffer.flip();
while(buffer.hasRemaining()) fic.write(buffer); buffer.compact(); bytes=bytes-position;
buffer=ByteBuffer.allocate(bytes);
FileOutputStream(FILE_PATH+FileName);
System.out.println(FileName+\文件接收完成!\); }
public String decode(ByteBuffer buffer) { }
public ByteBuffer encode(String str) { }
private int getFileNameLength(SocketChannel socketChannel)throws {
return charset.encode(str);
CharBuffer charBuffer=charset.decode(buffer); return charBuffer.toString();
IOException