最近做的一个java与flash通讯的项目,由于项目框架的需要,要把AMF格式解析代码抽出.代码有点乱,现丑了.
package com.musicxx.web.servlet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import juan.framework.action.Action4J;
import juan.framework.arguments.ArgumentsFactory;
import juan.framework.arguments.IArguments;
import juan.view.context.IContext;
import juan.view.context.impl.InnerContextImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.ActionMessage;
import flex.messaging.io.amf.Amf0Input;
import flex.messaging.io.amf.Amf0Output;
import flex.messaging.io.amf.Java15Amf0Output;
import flex.messaging.io.amf.MessageBody;
import flex.messaging.io.amf.MessageHeader;
/**
* Description : AMF通讯方式
*
* @author: JayChou
* @version 1.0
* @Created on Aug 18, 2008 5:57:21 PM
*/
public class AmfServlet extends HttpServlet {
private static Log logger = LogFactory.getLog(AmfServlet.class);
/**
* flash-->java
* @return
* @throws IOException
* @throws ClassNotFoundException
* @throws Exception
*/
private ActionMessage in(InputStream is) throws IOException, ClassNotFoundException {
ActionMessage inMs = new ActionMessage();
Amf0Input amfIn = new Amf0Input(SerializationContext.getSerializationContext());
amfIn.setInputStream(is);
int version = amfIn.readUnsignedShort();
inMs.setVersion(version);
int headerCount = amfIn.readUnsignedShort();
for (int i = 0; i < headerCount; ++i) {
MessageHeader header = new MessageHeader();
inMs.addHeader(header);
header.setName(amfIn.readUTF());
header.setMustUnderstand(amfIn.readBoolean());
amfIn.readInt();
amfIn.reset();
header.setData(amfIn.readObject());
}
int bodyCount = amfIn.readUnsignedShort();
for (int i = 0; i < bodyCount; ++i) {
MessageBody body = new MessageBody();
inMs.addBody(body);
body.setTargetURI(amfIn.readUTF());
body.setResponseURI(amfIn.readUTF());
amfIn.readInt();
amfIn.reset();
body.setData(amfIn.readObject());
}
amfIn.close();
amfIn=null;
return inMs;
}
/**
* java --> flash
* @param o
* @throws IOException
* @throws Exception
*/
private void out(ActionMessage outMs,HttpServletRequest request, HttpServletResponse response) throws IOException {
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
Amf0Output amfOut = new Java15Amf0Output(SerializationContext.getSerializationContext());
int version = outMs.getVersion();
amfOut.setAvmPlus(version > 0);
amfOut.setOutputStream(outBuffer);
amfOut.writeShort(version);
int headerCount = outMs.getHeaderCount();
amfOut.writeShort(headerCount);
for (int i = 0; i < headerCount; ++i) {
MessageHeader header = outMs.getHeader(i);
amfOut.writeUTF(header.getName());
amfOut.writeBoolean(header.getMustUnderstand());
amfOut.writeInt(-1);
amfOut.reset();
amfOut.writeObject(header.getData());
}
int bodyCount = outMs.getBodyCount();
amfOut.writeShort(bodyCount);
for (int i = 0; i < bodyCount; ++i) {
MessageBody body = outMs.getBody(i);
if (body.getTargetURI() == null)
amfOut.writeUTF("null");
else
amfOut.writeUTF(body.getTargetURI());
if (body.getResponseURI() == null)
amfOut.writeUTF("null");
else
amfOut.writeUTF(body.getResponseURI());
amfOut.writeInt(-1);
amfOut.reset();
amfOut.writeObject(body.getData());
}
response.setContentType(request.getContentType());
response.setContentLength(outBuffer.size());
outBuffer.writeTo(response.getOutputStream());
response.flushBuffer();
amfOut.flush();
amfOut.close();
amfOut=null;
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Action4J action = new Action4J();
IContext icontext = new InnerContextImpl();
IArguments args = ArgumentsFactory.getArguments();
action.setContext(icontext);
action.setRequest(req);
action.setResponse(resp);
action.setServletConfig(getServletConfig());
action.setServletContext(getServletContext());
ActionMessage outMs = new ActionMessage();
ActionMessage inMs = null;
try {
inMs = in(req.getInputStream());
outMs.setVersion(inMs.getVersion());
for(int i=0;i<inMs.getBodyCount();i++){
MessageBody body = inMs.getBody(i);
String method = body.getReplyMethod().split("\\.")[1];
Object[] o = (Object[]) body.getData();
Object[] arr = (Object[]) o;
for (int j = 0; j < arr.length; j++){
args.add("index" + (j+1), arr[j]);
}
action.execute("/"+method, args);
Object result=action.getContext().get("result");
MessageBody responseBody = new MessageBody();
responseBody.setData(result);
responseBody.setTargetURI( body.getResponseURI()+ "/onResult");
outMs.addBody(responseBody);
args.clear();
}
} catch (Exception e) {
logger.error(e.getMessage());
MessageBody responseBody = new MessageBody();
responseBody.setData(new Object[]{100});
responseBody.setTargetURI( inMs.getBody(0).getResponseURI()+ "/onResult");
outMs.addBody(responseBody);
} finally {
args.clear();
out(outMs,req,resp);
}
}
}
分享到:
相关推荐
自己写的flash webgame 搭建的java 后台 解决了安全沙箱问题,可以进行聊天,和后台java函数的请求及响应。amf3 编码。
java运行依赖jar包
AMF1.0(AMP).zip 代码
从PHPAMF提取并修改测试可用的AMF反解代码 调用例子: $amf = new AMFObject($data); //$data为AMF数据 $deserializer = new AMFDeserializer($amf->rawData); $deserializer->deserialize($amf); $AMF = $...
AMF 读写器库简单的 Java AMF 读写器库。
移植RMTP 看看效果 amf flex
openamf openamf-1.0RC12 openamf实例 openamf+flash实例 openamf最完整实例java + flash+eclipse工程源码
《RTMP协议中AMF的定义》,原名《TheAMF》英文版本。... This is the first AMF object sent by the Flash Player after the initial handshake and is a product of the Action Script call NetConnection.Connect( ).
把JAVA代码转化为AS3代码的工具 非常适合于使用AMF进行开发。 最好是直接放在SOURCE FOLDER里使用,避免莫名其妙的问题。
thinkphp_amf的一个简单demo
amf3 的格式说明,英文版,做rtmp协议或者做与flash数据交互服务器端必备的参考
功能: 支持AS3的ByteArray序列化对象的AMF格式编码、解码( readObject、writeObject )。 支持AS3的ByteArray的compress、uncompress压缩算法:...为方便想了解AMF格式及封包的坛友分析解码AMF数据资料使用。
Delphi的AMF格式代码。 TAMF3Stream = class(TAMFBaseStream) private type TAMFCache<T> = class private FIndex: Integer; FCache: TDictionary, Integer>; function GetIndex(const Obj: T): Integer; ...
通过使用AMF,Android应用可以与服务器端进行高效的数据交换,提高应用性能和用户体验。 3. AMF序列化 AMF序列化是将对象转换为二进制数据的过程。通过使用AMF序列化,Android应用可以将对象转换为二进制数据,...
要实现JAVA与FLEX的前后端交互,并通过amf技术来实现,可以将本附件中的blazeds工程中 flex和lib两个文件夹中的文件拷贝到你要java工程的web-info目录下,具体配置步骤可参考本人博客中的文章,希望能对各位初学者...
AMF开源FluorineFx文件太大 对于只需要序列化和反序列化AMF数据流的朋友,这个库是不错的选择 支持.net 1.1和2.0+
Adobe Flash AMF3 specification for flash player 9 and above serialization and communication.
amf解析构造易语言源码
易语言amf解析构造源码,amf解析构造,分析amf,取短整数_字节集,时间到双精度,到时间_双精度,取整数_字节集,integer_字节集,取双精度_字节集,amf3型数据,十六进制转字节集,分析数据段,解析amf数据,解析amf3数据,Utf8转...
amf0 原版文档 完整版