开发者

Send objects from flex to java servlet

Th开发者_如何学运维is is my method in actionscript

var urlVars : URLVariables = new URLVariables();
                urlVars.myname = byteArr;
                var urlReq : URLRequest = new URLRequest('MyServlet');
                urlReq.data = urlVars;
                urlReq.method = 'post';
                navigateToURL(urlReq, '_blank');

How do I recieve the byte array on servlet ?

Also the byteArr above comes from java side,

byte[] byteArr = aMethodWhichReturnsaPDFByteArray();
        HttpServletResponse response = FlexContext.getHttpResponse();
        ServletOutputStream os = null;
        try {
            response.reset();
            response.setContentType("application/pdf");
            response.setContentLength(byteArr.length);
            response.setHeader("Content-disposition",
                    "inline; filename=\"Report.pdf\"");
            os = response.getOutputStream();
            os.write(byteArr);
            os.flush();
os.close();

The above method did not work.


You can use BlazeDS for this. I don't have an example for deserializing on the server but here is one for serializing. In a servlet do something like:

response.setHeader("Content-Type", "application/x-amf");
ServletOutputStream out = response.getOutputStream();

ActionMessage requestMessage = new ActionMessage(MessageIOConstants.AMF3);

MessageBody amfMessage = new MessageBody();
amfMessage.setData(list);
requestMessage.addBody(amfMessage);

AmfMessageSerializer amfMessageSerializer = new AmfMessageSerializer();
amfMessageSerializer.initialize(SerializationContext.getSerializationContext(), out, new AmfTrace());
amfMessageSerializer.writeMessage(requestMessage);

out.close();

On the client do something like:

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, function(event:Event):void {
  var ba:ByteArray = (event.currentTarget.data as ByteArray);          
  var packet:AMFPacket = AMFDecoder.decodeResponse(ba) as AMFPacket;
});
loader.load(urlReq);

Your use case is similar to this, just serialize on the client and deserialize on the server.

You can find all of the source code for this example at:
http://flexapps.svn.sourceforge.net/viewvc/flexapps/census2-tests/


If you insist on sending the params this way, you'd better encode the byte array in a string representation. Base64 for example. On the java side used commons-codec to decode it.

Otherwise, see this article. It's a bit old, but still applies. Also see here


Commons File Upload library comes into rescue ! Just pass the request object to ServletFileUpload object. A detailed example can be found here

http://commons.apache.org/fileupload/using.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜