0

This 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.

1
  • when I ran debug, it ran smoothly; there is a catch statement below os.close(); however, no exception was caught, no new page was opened. Commented Dec 6, 2010 at 9:50

3 Answers 3

1

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/

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

The byte array is pretty big. Encoding and then decoding on other side causes performance issues. Is there any other way ? Actually the byte array is generated in java side and sent back to flex. Since creating new response from java side isn't working.
about 1MB. May expand in close future.
that's not too big. But if you can use channels (like I linked), prefer that.
Also, can u please tell me why the second part of code (refer question above) did not work.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.