java-mina(nio 框架)
mina是对nio的具体实现。是目前比较高效和流行的nio框架了。
- import java.net.InetSocketAddress;
- import org.apache.mina.filter.codec.ProtocolCodecFilter;
- import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
- import org.apache.mina.filter.logging.LoggingFilter;
- import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
- public class Main {
- private static final int PORT = 8080;
- public static void main(String[] args) throws Exception {
- //服务端的实例
- NioSocketAcceptor accept=new NioSocketAcceptor();
- //添加filter,codec为序列化方式。这里为对象序列化方式,即表示传递的是对象。
- accept.getFilterChain().addLast("codec",
- new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
- //添加filter,日志信息
- accept.getFilterChain().addLast("logging", new LoggingFilter());
- //设置服务端的handler
- accept.setHandler(new FileUploadHandler());
- //绑定ip
- accept.bind(new InetSocketAddress(PORT));
- System.out.println("upload server started.");
- }
- }
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.mina.core.service.IoHandlerAdapter;
- import org.apache.mina.core.session.IoSession;
- public class FileUploadHandler extends IoHandlerAdapter {
- private BufferedOutputStream out;
- private int count;
- private String fileName = "D:/log/test.jpg";
- private static final Log log = LogFactory.getLog(FileUploadHandler.class);
- public void sessionOpened(IoSession session) throws Exception {
- System.out.println("server open");
- }
- public void exceptionCaught(IoSession session, Throwable cause)
- throws Exception {
- System.out.println("exception");
- session.close(true);
- super.exceptionCaught(session, cause);
- }
- public void messageReceived(IoSession session, Object message) {
- System.out.println("server received");
- try {
- if (message instanceof FileUploadRequest) {
- //FileUploadRequest 为传递过程中使用的DO。
- FileUploadRequest request = (FileUploadRequest) message;
- System.out.println(request.getFilename());
- if (out == null) {
- //新建一个文件输入对象BufferedOutputStream,随便定义新文件的位置
- out = new BufferedOutputStream(new FileOutputStream(
- "D:/log/" + request.getFilename()));
- out.write(request.getFileContent());
- } else {
- out.write(request.getFileContent());
- }
- count += request.getFileContent().length;
- } else if (message instanceof String) {
- if (((String)message).equals("finish")) {
- System.out.println("size is"+count);
- //这里是进行文件传输后,要进行flush和close否则传递的文件不完整。
- out.flush();
- out.close();
- //回执客户端信息,上传文件成功
- session.write("success");
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void sessionClosed(IoSession session) throws Exception {
- System.out.println("server session close");
- }
- }
- import java.io.Serializable;
- public class FileUploadRequest implements Serializable {
- private String hostname;
- private String filename;
- private byte[] fileContent;
- public String getHostname() {
- return hostname;
- }
- public void setHostname(String hostname) {
- this.hostname = hostname;
- }
- public String getFilename() {
- return filename;
- }
- public void setFilename(String filename) {
- this.filename = filename;
- }
- public byte[] getFileContent() {
- return fileContent;
- }
- public void setFileContent(byte[] fileContent) {
- this.fileContent = fileContent;
- }
- }
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import nio.upload.server.FileUploadRequest;
- import org.apache.mina.core.future.ConnectFuture;
- import org.apache.mina.core.session.IoSession;
- import org.apache.mina.filter.codec.ProtocolCodecFilter;
- import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
- import org.apache.mina.filter.logging.LoggingFilter;
- import org.apache.mina.transport.socket.nio.NioSocketConnector;
- public class MainClient {
- private static final int PORT = 8080;
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws Exception {
- //客户端的实现
- NioSocketConnector connector = new NioSocketConnector();
- connector.getFilterChain().addLast("codec",
- new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
- connector.getFilterChain().addLast("logging", new LoggingFilter());
- FileUploadClientHandler h = new FileUploadClientHandler();
- connector.setHandler(h);
- //本句需要加上,否则无法调用下面的readFuture来从session中读取到服务端返回的信息。
- connector.getSessionConfig().setUseReadOperation(true);
- ConnectFuture cf = connector.connect(new InetSocketAddress("localhost",
- PORT));
- IoSession session;
- //等待连接成功
- cf.awaitUninterruptibly();
- session = cf.getSession();
- System.out.println("client send begin");
- //传递文件开始
- String fileName = "test.jpg";
- FileInputStream fis = new FileInputStream(new File(fileName));
- byte[] a = new byte[1024 * 4];
- FileUploadRequest request = new FileUploadRequest();
- request.setFilename(fileName);
- request.setHostname("localhost");
- while (fis.read(a, 0, a.length) != -1) {
- request.setFileContent(a);
- //像session中写入信息供服务端获得
- session.write(request);
- }
- //发送完成的标志
- session.write(new String("finish"));
- System.out.println("client send finished and wait success");
- //接上面来取得服务端的信息
- Object result = session.read().awaitUninterruptibly().getMessage();
- if (result.equals("success")) {
- System.out.println("success!");
- //关闭客户端
- connector.dispose();
- }
- }
- }
- import org.apache.mina.core.service.IoHandlerAdapter;
- import org.apache.mina.core.session.IoSession;
- public class FileUploadClientHandler extends IoHandlerAdapter {
- public void sessionOpened(IoSession session) throws Exception {
- System.out.println("client open");
- }
- public void sessionClosed(IoSession session) throws Exception {
- System.out.println("client session close");
- }
- public void messageReceived(IoSession session, Object message)
- throws Exception {
- System.out.println("thr result is" + message);
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。