java amf

http://www.5uflash.com/flashjiaocheng/Flashyingyongkaifa/2932.html

SerializationContext serializationContext=new SerializationContext();
Amf3Output amfOut = new Amf3Output(serializationContext);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    DataOutputStream dataOutStream = new DataOutputStream(outStream);
    amfOut.setOutputStream(dataOutStream);
    
    HashMap<String, Object> map=new HashMap<String, Object>();
    Double[] arr=new Double[10000];
    for(int index=0;index<10000;index++)
    {
     arr[index]=Math.random();
    }
    map.put("arr", arr);
    map.put("name", "weni");
    map.put("age", "27");
    map.put("web", "www.weni.cn");
    try
    {
amfOut.writeObject(map); //写入java HashMap对象,会自动转换成Flash的Object对象
dataOutStream.flush();
} catch (IOException e)
{
e.printStackTrace();
}

    byte[] messageBytes = outStream.toByteArray();
    try 
    { 
      FileOutputStream  os; 
      OutputStreamWriter  ow; 
      BufferedWriter  out; 
      os  =  new  FileOutputStream("D://test.txt"); 
      ow  =  new  OutputStreamWriter(os); 
      out  =  new  BufferedWriter(ow); 
      os.write(messageBytes);
      os.flush();
      os.close(); 
      System.out.println("OK");
}catch(Exception  e) 
{
System.out.println("error  :"  +  e);                      
}


客户端读取代码:
var loader:URLLoader=new URLLoader();
loader.load(new URLRequest("D://test5.txt"));
loader.addEventListener(Event.COMPLETE,onComplete);
loader.dataFormat=URLLoaderDataFormat.BINARY;
private function onComplete(evt:Event):void
{
var start:Number=getTimer();
var byte:ByteArray=loader.data as ByteArray;
var obj:Object=byte.readObject(); //读取出来的对象
var end:Number=getTimer();
trace("耗时:"+(end-start)+"毫秒")
trace(obj.name,obj.age,obj.arr.length)
}

你可能感兴趣的:(java,html,Web,OS,Flash)