package com.xx.mina;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.demux.MessageDecoder;
import org.apache.mina.filter.codec.demux.MessageDecoderResult;
public class BaseMessageDecoder implements MessageDecoder {
private AttributeKey CONTEXT = new AttributeKey(getClass(), "context");
private class Context {
public int dataType;
public boolean isReadName;
public int strLength;
public byte[] byteStr;
public String fileName;
public int fileSize;
public byte[] byteFile;
public int count;
public boolean init = false;
public void reset() {
dataType = 0;
byteFile = null;
count = 0;
strLength = 0;
isReadName = false;
fileSize = 0;
byteStr = null;
fileName = null;
}
}
/**
* 验证是否可以解析
* */
public MessageDecoderResult decodable(IoSession session, IoBuffer in) {
Context context = (Context) session.getAttribute(CONTEXT);
if (context == null) {// 建立连接后第�?��传输
context = new Context();
context.dataType = in.getInt();
if (context.dataType == BeanUtil.SEND_TYPE_FILE) {
context.strLength = in.getInt();// 名称长度
context.byteStr = new byte[context.strLength];// 名称内容
context.fileSize = in.getInt();// 文件长度
context.byteFile = new byte[context.fileSize];// 文件内容
session.setAttribute(CONTEXT, context);
return MessageDecoderResult.OK;
} else if (context.dataType == BeanUtil.SEND_TYPE_TXT) {
context.strLength = in.getInt();// 名称长度
context.byteStr = new byte[context.strLength];// 名称内容
context.fileSize = 0;// 文件长度
context.byteFile = new byte[context.fileSize];// 文件内容
session.setAttribute(CONTEXT, context);
return MessageDecoderResult.OK;
} else {
return MessageDecoderResult.NOT_OK;
}
} else {// 建立后第�?��传输之后
if (context.dataType == BeanUtil.SEND_TYPE_FILE
|| context.dataType == BeanUtil.SEND_TYPE_TXT) {
return MessageDecoderResult.OK;
} else {
return MessageDecoderResult.NOT_OK;
}
}
}
public MessageDecoderResult decode(IoSession session, IoBuffer in,
ProtocolDecoderOutput outPut) throws Exception {
// TODO Auto-generated method stub
System.out.println("开始解析");
Context context = (Context) session.getAttribute(CONTEXT);
int sendType = context.dataType;
if (!context.init) {
context.init = true;
sendType = in.getInt();// 文件类型
in.getInt();// 文件名长�?
System.out.println("xx" + sendType);
if (sendType == BeanUtil.SEND_TYPE_FILE) {
in.getInt();// 文件大小
}
}
if (sendType == BeanUtil.SEND_TYPE_FILE) {
byte[] byteFile = context.byteFile;
int count = context.count;
while (in.hasRemaining()) {
byte b = in.get();// 整体读取
if (!context.isReadName) {//文件�?
context.byteStr[count] = b;
if (count == context.strLength - 1) {
context.fileName = new String(context.byteStr,
BeanUtil.charset);
System.out.println(context.fileName);
count = -1;
context.isReadName = true;
}
}
if (context.isReadName && count != -1) {//文件�?
byteFile[count] = b;
}
count++;
}
context.count = count;
System.out.println("count:" + count);
System.out.println("context.fileSize:" + context.fileSize);
session.setAttribute(CONTEXT, context);
if (context.count == context.fileSize) {
BaseMessage message = new BaseMessage();
message.setDataType(context.dataType);
FileBean bean = new FileBean();
bean.setFileName(context.fileName);
bean.setFileSize(context.fileSize);
bean.setFileContent(context.byteFile);
message.setData(bean);
outPut.write(message);
context.reset();
}
return MessageDecoderResult.OK;
} else if (sendType == BeanUtil.SEND_TYPE_TXT) {
int count = context.count;
String resultMsg = null;
while (in.hasRemaining()) {
byte b = in.get();// 整体读取
context.byteStr[count] = b;
if (count == context.strLength - 1) {
resultMsg = new String(context.byteStr, BeanUtil.charset);
System.out.println(resultMsg);
}
count++;
}
context.count = count;
System.out.println("count:" + count);
System.out.println("context.strLength:" + context.strLength);
session.setAttribute(CONTEXT, context);
if (context.count == context.strLength) {
BaseMessage message = new BaseMessage();
message.setDataType(context.dataType);
message.setData(resultMsg);
outPut.write(message);
context.reset();
}
return MessageDecoderResult.OK;
} else {
System.out
.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
return MessageDecoderResult.NOT_OK;
}
}
/**
*
* */
public void finishDecode(IoSession session, ProtocolDecoderOutput outPut)
throws Exception {
// TODO Auto-generated method stub
System.out.println("end:::::::::::::::::");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
mina 集成spring 传输文件和文本
共109个文件
jar:65个
class:12个
java:11个
5星 · 超过95%的资源 需积分: 10 32 下载量 134 浏览量
2014-03-14
16:09:42
上传
评论
收藏 17.65MB RAR 举报
温馨提示
用一个监听既可以传文件又可以传文本 mina 集成spring 传输文件和文本
资源推荐
资源详情
资源评论
收起资源包目录
mina 集成spring 传输文件和文本 (109个子文件)
BaseMessageDecoder.class 5KB
Client.class 3KB
BaseMessageEncoder.class 3KB
ServerHandler.class 2KB
Server.class 2KB
FileHelper.class 2KB
BaseMessageDecoder$Context.class 1KB
FileBean.class 969B
BaseMessage.class 875B
ClientHandler.class 870B
MathProtocolCodecFactory.class 695B
BeanUtil.class 605B
.classpath 900B
org.eclipse.wst.common.component 582B
org.eclipse.wst.jsdt.ui.superType.container 49B
spring.jar 2.81MB
hibernate3.jar 2.31MB
org.springframework.context-3.1.0.RELEASE.jar 810KB
mina-core-2.0.4.jar 630KB
javassist-3.8.0.GA.jar 580KB
org.springframework.beans-3.1.0.RELEASE.jar 575KB
commons-collections-3.2.1.jar 562KB
org.springframework.web.servlet-3.1.0.RELEASE.jar 559KB
smackx-3.4.1-0cec571.jar 544KB
mysql.jar 528KB
org.springframework.web-3.1.0.RELEASE.jar 528KB
spring-beans.jar 477KB
spring-context.jar 466KB
c3p0-0.9.5-pre6.jar 462KB
antlr-2.7.6.jar 433KB
org.springframework.core-3.1.0.RELEASE.jar 432KB
spring-webmvc.jar 393KB
org.springframework.jdbc-3.1.0.RELEASE.jar 392KB
org.springframework.orm-3.1.0.RELEASE.jar 369KB
log4j-1.2.14.jar 359KB
smack-3.4.1-0cec571.jar 351KB
spring-jdbc.jar 328KB
org.springframework.aop-3.1.0.RELEASE.jar 324KB
spring-aop.jar 318KB
smackx-jingle-3.4.1-0cec571.jar 314KB
dom4j-1.6.1.jar 307KB
spring-core.jar 279KB
hibernate-annotations-3.3.1.GA.jar 274KB
commons-httpclient-3.0.1.jar 273KB
org.springframework.transaction-3.1.0.RELEASE.jar 240KB
commons-lang-2.3.jar 240KB
commons-beanutils-1.8.0.jar 226KB
org.springframework.test-3.1.0.RELEASE.jar 224KB
jaxen-1.1.1.jar 222KB
org.springframework.jms-3.1.0.RELEASE.jar 194KB
gson-2.2.1.jar 185KB
org.springframework.expression-3.1.0.RELEASE.jar 172KB
smackx-workgroup-3.4.1-0cec571.jar 152KB
json-lib-2.2.3-jdk15.jar 145KB
wsdl4j-1.6.2.jar 145KB
commons-dbcp-1.2.1.jar 105KB
org.springframework.context.support-3.1.0.RELEASE.jar 104KB
servlet-api.jar 86KB
ezmorph-1.0.3.jar 76KB
commons-discovery-0.2.jar 70KB
org.springframework.oxm-3.1.0.RELEASE.jar 69KB
hibernate-commons-annotations-3.0.0.ga.jar 65KB
commons-logging-1.1.1.jar 59KB
commons-fileupload-1.2.2.jar 58KB
smackx-debug-3.4.1-0cec571.jar 56KB
org.springframework.asm-3.1.0.RELEASE.jar 52KB
org.springframework.aspects-3.1.0.RELEASE.jar 49KB
ejb3-persistence-1.0.2.GA.jar 49KB
asm-3.1.jar 42KB
commons-pool-1.2.jar 41KB
mina-integration-beans-2.0.0-M6.jar 39KB
jaxrpc.jar 31KB
spring-aspects.jar 20KB
slf4j-api-1.5.2.jar 17KB
jta-1.1.jar 15KB
org.springframework.instrument.tomcat-3.1.0.RELEASE.jar 11KB
smackx-experimental-0.1-0cec571.jar 10KB
slf4j-log4j12-1.5.2.jar 9KB
org.springframework.instrument-3.1.0.RELEASE.jar 7KB
aopalliance-1.0.jar 4KB
BaseMessageDecoder.java 5KB
FileHelper.java 2KB
Client.java 2KB
BaseMessageEncoder.java 1KB
ServerHandler.java 1KB
Server.java 953B
FileBean.java 604B
BaseMessage.java 544B
ClientHandler.java 533B
MathProtocolCodecFactory.java 430B
BeanUtil.java 245B
.jsdtscope 503B
minademos.log 6KB
MANIFEST.MF 39B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 395B
org.eclipse.core.resources.prefs 88B
.project 1KB
log4j.properties 583B
log4j.properties 583B
共 109 条
- 1
- 2
帝释天
- 粉丝: 33
- 资源: 81
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于 Java的班级管理系统课程设计
- 深入探索Suno AI:教程、元标签与案例分析.pdf
- 超市会员积分管理系统主要用于实现了企业管理数据统计等
- 基于 Java的班级管理系统
- MyBatis 动态 SQL:灵活而强大的查询构建器.pdf
- com.accordion.prettyo.apk
- 毕业设计:基于SSM的mysql-ssm软件bug管理系统(源码 + 数据库 + 说明文档)
- MTSQL8.0.35windows(64bit)-mysql-installer-community-8.0.35.0
- 人工智能引领音乐创作新时代之Suno AI
- Public-bicycle-usage-forecast-master.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页