package cn.tianqi.game.servlet;import java.io.DataOutputStream;import java.io.IOException;import java.util.Collection;import java.util.List;import java.util.Map;import java.util.zip.DeflaterOutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.tianqi.game.action.util.AmfDataServlet;import cn.tianqi.game.action.util.Configuration;import cn.tianqi.game.action.view.MapBean;import cn.tianqi.game.action.view.ResourceBean;import cn.tianqi.game.action.view.SysBean;import cn.tianqi.game.action.vo.ResourceVo;import flex.messaging.io.SerializationContext;import flex.messaging.io.amf.Amf3Output;public class AllServlet extends HttpServlet { private static final long serialVersionUID = 1L; private AmfDataServlet amfData=new AmfDataServlet(); private ResourceBean catBean=ResourceBean.getInstance(); private MapBean mapBean=MapBean.getInstance(); private SysBean sysBean=SysBean.getInstance(); public AllServlet() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @SuppressWarnings("unchecked") public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); Map map=(Map)amfData.getObj(request.getContentLength(), request.getInputStream()); String method=String.valueOf(map.get("method")); if(method.equals(Configuration.SYS_INIT)){ Collection coll=catBean.resourceCat().values();//所有素材資源數據 Collection coll2=catBean.mapResource().values();//所有地圖素材資源數據 Collection coll3=mapBean.selectAllMap().values();//所有地圖資源數據 List<ResourceVo> coll4=catBean.selectALLRes();//所有資源表數據 send(response, sysBean.sysInit(coll,coll2,coll3,coll4)); }else if(method.equals(Configuration.CAT_RESOURCE)){ send(response, catBean.resourceCat().values()); }else if(String.valueOf(map.get("method")).equals(Configuration.MAP_RESOURCE)){ send(response, catBean.mapResource().values()); }else if(method.equals(Configuration.CAT_RESOURCE_ADD)){ Map cat=(Map)map.get("data"); send(response, catBean.saveResourceCat(cat)); }else if(method.equals(Configuration.MAP_RESOURCE_DATA)){ send(response, mapBean.selectAllMap().values()); }else if(method.equals(Configuration.MAP_DATA_SAVE)){ Map mds=(Map)map.get("data"); send(response, mapBean.saveMap(mds)); } } protected void send(HttpServletResponse response,Object obj) throws IOException{ Amf3Output amf3=new Amf3Output(new SerializationContext()); DeflaterOutputStream stream = new DeflaterOutputStream(new DataOutputStream(response.getOutputStream())); amf3.setOutputStream(stream); amf3.writeObject(obj); stream.finish(); } public void init() throws ServletException { // Put your code here }}package cn.tianqi.game.action.util;import java.io.ByteArrayInputStream;import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import javax.servlet.ServletInputStream;import flex.messaging.io.SerializationContext;import flex.messaging.io.amf.Amf3Input;public class AmfDataServlet { /** * 解析HttpServletRequest上傳的二進(jìn)制數據 * @param contentLength 上傳的數據總長(cháng)度(request.getContentLength()) * @param stream 上傳的數據(request.getInputStream()) * @return Object */ public Object getObj(int contentLength, ServletInputStream stream) { Object obj = null; DataInputStream dis = new DataInputStream(stream); byte[] by = new byte[contentLength]; try { int bytesRead = 0, totalBytesRead = 0; while (totalBytesRead < contentLength) { bytesRead = dis.read(by, totalBytesRead, contentLength); totalBytesRead += bytesRead; } Amf3Input amf = new Amf3Input(new SerializationContext()); InputStream is = new ByteArrayInputStream(by); amf.setInputStream(is); obj = amf.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return obj; } /** * 解析byte[]轉成Object * @param bytes 數據 * @return Object */ public Object getObj(byte[] bytes){ Object obj=null; try { Amf3Input amf = new Amf3Input(new SerializationContext()); InputStream is = new ByteArrayInputStream(bytes); amf.setInputStream(is); obj=amf.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return obj; }}package cn.tianqi.game.servlet{ import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import flash.net.URLRequestMethod; public class Servlet { private static var servlet:Servlet=null; private var httpUrl:String="http://192.168.1.101:8080/manager/"; //private var httpUrl:String="http://localhost:8080/manager/"; public function allServlet():URLRequest { var urlRequest:URLRequest=new URLRequest(httpUrl+"AllServlet"); urlRequest.contentType = 'applicatoin/octet-stream'; urlRequest.method=URLRequestMethod.POST; return urlRequest; }// public function resourceCatServlet():URLRequest// {// var urlRequest:URLRequest=new URLRequest(httpUrl+"CatInfoServlet");// urlRequest.contentType = "applicatoin/octet-stream";// urlRequest.method=URLRequestMethod.POST;// return urlRequest;// } public function fileUpload():URLRequest { var urlRequest:URLRequest=new URLRequest(httpUrl+"FileUpload"); return urlRequest; } // public function saveResourceCat():URLRequest// {// var urlRequest:URLRequest=new URLRequest(httpUrl+"ResourceAddServlet");// urlRequest.method=URLRequestMethod.POST;// return urlRequest;// } // public function mapResource():URLRequest// {// var urlRequest:URLRequest=new URLRequest(httpUrl+"MapResourceServlet");// urlRequest.method=URLRequestMethod.POST;// return urlRequest;// } // public function saveMapResource():URLRequest// {// var urlRequest:URLRequest=new URLRequest(httpUrl+"MapResourceDataServlet");// urlRequest.method=URLRequestMethod.POST;// return urlRequest;// } public static function getServlet():Servlet { if(servlet==null) { servlet=new Servlet(); } return servlet; } }}<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"> <mx:Script> <![CDATA[ import cn.tianqi.game.servlet.Servlet; import mx.collections.ArrayCollection; private var loader:URLLoader=null; private var v:URLVariables=null; private var urls:URLRequest=null; protected function mapResource_clickHandler(event:MouseEvent):void { loader=new URLLoader(); loader.dataFormat = URLLoaderDataFormat.BINARY; loader.addEventListener(Event.COMPLETE, loaderHandler); loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loaderHandler); loader.addEventListener(IOErrorEvent.IO_ERROR, loaderHandler); // v=new URLVariables();// v.type=2; //urls=Servlet.getServlet().mapResource(); urls=Servlet.getServlet().allServlet(); //urls.contentType = 'applicatoin/octet-stream'; var obj:Object=new Object(); obj.n="南泥灣"; obj.w=201; obj.h=150; obj.ms="101010100000001111000011111000000000"; var dt:ByteArray=new ByteArray(); dt.writeByte(100); obj.dt=dt; var obj1:Object=new Object(); obj1.method="mapDataSave"; obj1.data=obj; var byteArr:ByteArray=new ByteArray(); byteArr.writeObject(obj1); urls.data=byteArr; // urls.data=v; loader.load(urls); } protected function loaderHandler(event:Event):void { trace(event.type); switch(event.type){ case Event.COMPLETE: var byte:ByteArray = event.target.data as ByteArray; byte.uncompress(); var obj:Object = byte.readObject(); var arr:ArrayCollection=obj as ArrayCollection; break; } } ]]> </mx:Script> <mx:Button id="mapResource" label="點(diǎn)擊我" click="mapResource_clickHandler(event)"/></mx:Application>聯(lián)系客服