云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > java文件传输

java文件传输

  • 62 次阅读
  • 3 次下载
  • 2025/12/12 4:38:00

{

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

搜索更多关于: java文件传输 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

{ socketChannel.register(selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE);//注册事件 while(selector.select()>0) { Set readyKeys=selector.selectedKeys(); Iterator it=readyKeys.iterator(); while(it.hasNext()) {

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com