使用Java的Nio流进行gxdmt传输的情况。
package com.nio.demo;
import java.io.File;
import java.io.xsdxt;
import Java.net.inetsocketaddress;
import java.nio.ByteBuffer;
import Java.nio.channels.file channel;
import Java.nio.channels.server socket channel;
import Java.nio.channels.socket channel;
import java.nio.file.Path;
import java.nio.file.Paths;
import Java.nio.file.standardopenoption;
import java.util.EnumSet;
公共类文件接收器{
publicstaticvoidmain (stringgddmhtargs ) throws xsdxt {
filereceiverfilesender=new file receiver (;
file sender.rec file (客户端套接字);
}
//*
privatesocketchannelcreatesocketchannel () throws xsdxt { )。
服务器套接字通道. bind (newinetsocketaddress (8081 );
返回客户端套接字;
}
//*
私有语音文档(套接字通道客户端套接字) throws xsdxt { )。
发送gxdmt流
int读取长度=0;
while ((读取长度=客户端套接字.读取)缓冲器)0) )
buffer.flip (;
filename.append (新字符串(buffer.array ) );
buffer.clear (;
//最后一个数据包读取特殊处理。 否则,我会一直等待读取
读取长度!=buffer.capacity (
布雷克;
}
}
接收gxdmt名称
文件服务=新文件(文件路径);
if (! saveDir.exists (
saveDir.mkdirs (;
}
file path=file path filename.tostring ().trim );
//响应获取文件名称succ
客户端套接字. write (byte buffer.wrap ) newstring('0000 ' ).getBytes ) );
//gxdmt获取
path path=paths.get (文件路径);
enum set.of (标准dardopenoption.create,
标准dardopenoption.write );
//写入本地
wile (客户端套接字. read (buffer )=0) )。
buffer.flip (;
fileChannel.write(buffer );
buffer.clear (;
}
fileChannel.close (;
客户端socket.close (;
}
}
package com.nio.demo;
import java.io.xsdxt;
import Java.net.inetsocketaddress;
import java.nio.ByteBuffer;
import Java.nio.channels.file channel;
import Java.nio.channels.socket channel;
import java.nio.file.Path;
import java.nio.file.Paths;
//*
公共类文件发送器{
publicstaticvoidmain (stringgddmhtargs ) throws xsdxt {
filesenderfilesender=new file sender (;
file sender.sendfileandname (send channel,文件路径);
}
//*
privatevoidsendfileandname (socketchannelclientchannel,字符串文件路径) throws xsdxt {
发送gxdmt流
客户端channel.write (byte buffer.wrap ) newstring('1.jpg ).getBytes ) );
int length=0;
while ((length=客户端通道. read ) ) Buffer )!=-1 ()
buffer.flip (;
answer code.append (新字符串(buffer.array ) );
If (长度!=buffer.capacity (
布雷克;
}
buffer.clear (;
}
if(answercode.tostring(.trim ) ).equals('0000 ' ) )。
sendfile (客户端通道,文件路径);
} else {
客户端channel.close (;
}
}
//*
privatesocketchannelcreatesocketchannel () throws xsdxt { )。
send channel.connect (newinetsocketaddress (' localhost ',8081 );
返回发送通道;
}
//*
专用文件sendfile (socketchannelsendchannel,字符串文件路径) throws xsdxt {
发送gxdmt流
path path=paths.get (文件路径);
wile (文件通道. read ) Buffer!=-1 ()
buffer.flip (;
发送信道. write (buffer );
buffer.clear (;
}
sendChannel.close (;
}
}
不懂源代码的人可以私信,大家一起学习。
源代码下载地址: http://download.csdn.net/download/zhanghua 1369/10270176