通过StreamIoHandler来进行文件的传输
1. 创建通过接收的BufferedInputStream写输出BufferedOutputStream的方法
public class IoStreamThreadWork extends Thread {
public static final int BUFFER_SIZE = 1024*2;
private BufferedInputStream bis;
private BufferedOutputStream bos;
public BufferedInputStream getBis() {
return bis;
}
public void setBis(BufferedInputStream bis) {
this.bis = bis;
}
public BufferedOutputStream getBos() {
return bos;
}
public void setBos(BufferedOutputStream bos) {
this.bos = bos;
}
public IoStreamThreadWork(InputStream in, OutputStream os){
bis = new BufferedInputStream(in);
bos = new BufferedOutputStream(os);
}
public synchronized void run() {
byte[] bufferByte = new byte[BUFFER_SIZE];
int tempData = 0;
try {
while((tempData = bis.read(bufferByte)) != -1 ){
bos.write(bufferByte, 0, tempData);
}
try {
bos.flush();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. 创建Server端服务及其StreamIoHandler
Server.java
public class Server {
public Server(){
}
public void init() throws IOException{
IoAcceptor acceptor = new NioSocketAcceptor();
ObjectSerializationCodecFactory factory = new ObjectSerializationCodecFactory();
factory.setDecoderMaxObjectSize(Integer.MAX_VALUE);
factory.setEncoderMaxObjectSize(Integer.MAX_VALUE);
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.setHandler(new MyStreamIoHandler());
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
acceptor.setDefaultLocalAddress(new InetSocketAddress(Constants.PORT));
acceptor.bind();// 启动监听
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Server server = new Server();
server.init();
}
}
MyStreamIoHandler.java
public class MyStreamIoHandler extends StreamIoHandler {
@Override
public void sessionOpened(IoSession session) {
System.out.println("客户端连接了:"+session.getRemoteAddress());
super.sessionOpened(session);
}
@Override
protected void processStreamIo(IoSession session, InputStream in,
OutputStream out) {
//设定一个线程池
//参数说明:最少数量3,最大数量6 空闲时间 3秒
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 6, 3,TimeUnit.SECONDS,
//缓冲队列为3
new ArrayBlockingQueue<Runnable>(3),
//抛弃旧的任务
new ThreadPoolExecutor.DiscardOldestPolicy());
FileOutputStream fos = null;
File receiveFile = new File("e:\\hello.doc");
try {
fos = new FileOutputStream(receiveFile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
//将线程放入线程池 当连接很多时候可以通过线程池处理
threadPool.execute(new IoStreamThreadWork(in,fos));
}
}
3. 创建Client端连接机器StreamIoHandler
Client.java
public class Client {
public Client(){
super();
}
public void connect() throws InterruptedException{
NioSocketConnector connector = new NioSocketConnector();
ObjectSerializationCodecFactory factory = new ObjectSerializationCodecFactory();
factory.setDecoderMaxObjectSize(Integer.MAX_VALUE);
factory.setEncoderMaxObjectSize(Integer.MAX_VALUE);
// Configure the service.
connector.setConnectTimeoutMillis(Constants.CONNECT_TIMEOUT);
//connector.getFilterChain().addLast("codec",new ProtocolCodecFilter(factory));
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.setHandler(new ClientStreamIoHandler());
IoSession session;
for (;;) {
try {
ConnectFuture future = connector.connect(new InetSocketAddress(Constants.HOSTNAME, Constants.PORT));
future.awaitUninterruptibly();
session = future.getSession();
break;
} catch (RuntimeIoException e) {
System.err.println("Failed to connect.");
e.printStackTrace();
Thread.sleep(5000);
}
}
// wait until the summation is done
session.getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Client client = new Client();
client.connect();
}
}
ClientStreamIoHandler.java
public class ClientStreamIoHandler extends StreamIoHandler {
@Override
protected void processStreamIo(IoSession session, InputStream in,
OutputStream out) {
//客户端发送文件
File sendFile = new File("D:\\ttt.doc");
FileInputStream fis = null;
try {
fis = new FileInputStream(sendFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//放入线程让其执行
//客户端一般都用一个线程实现即可 不用线程池
new IoStreamThreadWork(fis,out).start();
return;
}
}
分享到:
相关推荐
Apache MINA是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用程序提供了非常便利的框架。 当前发行的 MINA 版本支持基于 Java NIO 技术的 TCP/UDP 应用程序开发、串口通讯程序(只在最新的预览版...
NULL 博文链接:https://jean7155.iteye.com/blog/1954890
Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)
apache-mina-2.0.16.zip
Apache MINA 线程模型配置 Mina配置
深入理解Apache_Mina_(1)----_Mina的几个类 深入理解Apache_Mina_(2)----_与IoFilter相关的几个类 深入理解Apache_Mina_(3)----_与IoHandler相关的几个类 深入理解Apache_Mina_(4)----_IoFilter和IoHandler的区别和...
hive 开发UDF 使用maven工程 引发jar包缺失 hive 开发UDF 使用maven工程 引发jar包缺失
这个是我学的apache mina2.0学习笔记的实例
收集整理的Apache Mina chm pdf教程和帮助文档
Apache MINA是一个网络应用程序框架,用来帮助用户简单地开发高性能和高可靠性的网络应用程序。 apache-mina-2.0.7-bin.zip,apache-mina-2.0.7-src.zip,log4j-1.2.17.zip,slf4j-api-1.6.6.jar,slf4j-api-1.6.6-...
apache mina的入门完整学习资料,附加中文参考手册。
apache mina 简单示例apache mina 简单示例apache mina 简单示例apache mina 简单示例apache mina 简单示例apache mina 简单示例
本资源包含两个 pdf 文档,一本根据官方最新文档 (http://mina.apache.org/mina-project/userguide/user-guide-toc.html) 整理的 mina_2.0_user_guide_en.pdf,一个中文翻译的 mina_2.0_user_guide_cn.pdf。...
Apache MINA2实用手册 Apache MINA2实用手册
apache mina实例免费下载,有很多实例代码简单易懂欢迎大家下载!
一个Apache MINA使用案例源代码ApacheMina
Apache MINA 2.0 用户指南
mina:Apache MINA的镜像
apache mina chm 格式的api帮助文档。
最新的Apache Mina v2.0.8 API手册,chm格式,2014年9月下旬制作。