论坛首页 Java企业应用论坛

java amf3格式数据的接收与响应

浏览 3089 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-08-18  

package org.jivesoftware.openfire.plugin.mongodb.file;

 


import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.ASObject;
import flex.messaging.io.amf.Amf3Input;
import flex.messaging.io.amf.Amf3Output;
/**
 * AMF3 handle
 *
 * @author dhdu@qq.com
 *
 */
public class Amf3 {
 private final static Amf3 instance = new Amf3();
 private static SerializationContext serializationContext;

 //
 private Amf3() {
 }

 //
 public static Amf3 getInstance() throws Exception {
  return instance;
 }

 static {
  serializationContext = new SerializationContext();
 }

 public ASObject getASObject(HttpServletRequest request) {
  ASObject aso=null;
  try {
   ServletInputStream in=request.getInputStream();
   Amf3Input aip = new Amf3Input(serializationContext);
   if (in != null) {
    aip.setInputStream(in);
    aso = (ASObject) aip.readObject(); 
    
    //in.close();
    aip.close();
   }
   
  } catch (Exception e) {
   e.printStackTrace();   
  }
  return aso;
 }

 public void writeASObject(HttpServletResponse response,ASObject a){
  Amf3Output amfOut=null;
  try {
   response.setContentType("application/x-gzip-compressed");
   ServletOutputStream os=response.getOutputStream();
   amfOut = new Amf3Output(serializationContext);  
   //ByteArrayOutputStream outStream = new ByteArrayOutputStream();
   //DataOutputStream dataOutStream = new DataOutputStream(outStream);
   amfOut.setOutputStream(os);
   amfOut.writeObject(a);
   
   //byte[] bytes=outStream.toByteArray();
   //os.write(bytes);
   
   os.flush();
   //os.close();
   //outStream.flush();
   //outStream.close();   
   //dataOutStream.flush();
   //dataOutStream.close();   
   amfOut.flush();
   amfOut.close();
   
  }catch(Exception e)
  {
   e.printStackTrace(); 
  }
  
 }
}

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics