Send objects from flex to java servlet
Asked Answered
H

3

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.

Heroine answered 6/12, 2010 at 9:26 Comment(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.Heroine
W
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

Wet answered 6/12, 2010 at 9:28 Comment(4)
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.Heroine
about 1MB. May expand in close future.Heroine
that's not too big. But if you can use channels (like I linked), prefer that.Wet
Also, can u please tell me why the second part of code (refer question above) did not work.Heroine
B
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/

Blindage answered 6/12, 2010 at 12:40 Comment(0)
W
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

Wet answered 6/12, 2010 at 9:28 Comment(4)
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.Heroine
about 1MB. May expand in close future.Heroine
that's not too big. But if you can use channels (like I linked), prefer that.Wet
Also, can u please tell me why the second part of code (refer question above) did not work.Heroine
R
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

Rowlock answered 6/12, 2010 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.