0

I need to send a ByteArray from Flex to Spring MVC servlet but it doesn't work. I record a sample of audio, convert to bytearray in AS3 and send to SpringMVC servlet but the data that that receives Java is null.

AS3 CODE:

var flacCodec:Object;
        flacCodec = (new cmodule.flac.CLibInit).init();
        bytes.position = 0;
        var rawData: ByteArray = new ByteArray();
        var flacData : ByteArray = new ByteArray();
        rawData = convert32to16(bytes);
        flacData.endian = Endian.LITTLE_ENDIAN;
        flacCodec.encode(   encodingCompleteHandler, 
            encodingProgressHandler, 
            rawData, 
            flacData, 
            rawData.length, 
            30);            
        function encodingCompleteHandler(event:*):void {            
            var PATH:String = "http://localhost:8080/myproject/speechRecognition/";
            var urlRequest:URLRequest = new URLRequest(PATH);
            var urlLoader:URLLoader = new URLLoader();
            urlRequest.contentType = "audio/x-flac; rate=44000";
            var variables:URLVariables = new URLVariables();
            variables.contents = flacData;
            variables.filename = "test";
            urlRequest.data = variables;
            urlRequest.method = URLRequestMethod.POST;

            urlLoader.dataFormat = URLLoaderDataFormat.TEXT; // default
            urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
            urlLoader.addEventListener(ErrorEvent.ERROR, urlLoader_error);
            urlLoader.load(urlRequest);

JAVA CODE:

    @RequestMapping(value = "/",method = RequestMethod.POST)
public String getSpeechRecognition(ServletRequest req) {
    String filename = req.getParameter("filename");
    byte[] contents = req.getParameter("contents").getBytes();
    request= new SpeechRecognitionRequestVO();
    request.setData(contents);
    try {
        response=((SpeechRecognitionResponseVO) getSpeechRecognitionService().getSpeechRecognition(request));
    } catch (Exception e){
        logger.error(e.toString());
    }
    return "views/sequence";
}

I receive null in this parameter:

req.getParameter("contents")

Anybody knows what is happening?

tludmetal
  • 103
  • 1
  • 8
  • I don't know `actionscript` nor its APIs, but I can tell you this: an in-memory `ByteArray` in `actionscript` is completely different than a `byte[]` in `Java`. – Sotirios Delimanolis Oct 11 '13 at 17:00
  • Shouldn't you better use URLLoaderDataFormat.BINARY? Does it make any change? – Fygo Oct 11 '13 at 22:42
  • I use this structures because I saw in this sample: http://stackoverflow.com/questions/10096774/how-to-send-binary-data-from-as3-through-java-to-a-filesystem and http://nxhoaf.wordpress.com/2012/05/12/speech-to-text-in-action-script-3-part-3/ – tludmetal Oct 14 '13 at 15:54
  • I used URLLoaderDataFormat.BINARY and it doesn't work. Thanks – tludmetal Oct 14 '13 at 15:57
  • I think that it is a problem of Spring MVC because when I use GET instead of POST I receive the parameter. But I am not sure that It would be correct. Any idea? – tludmetal Oct 23 '13 at 16:27

1 Answers1

1

This will send your data as binary:

        var header : URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");

        var url_request : URLRequest = new URLRequest();
        url_request.url = _url;
        url_request.contentType = "binary/octet-stream";
        url_request.method = URLRequestMethod.POST;
        url_request.data = byteArray;
        url_request.requestHeaders.push(header);

        var loader : URLLoader = new URLLoader();
        loader.addEventListener(Event.COMPLETE, loaderCompleteHandler, false, 0, true);
        loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
        loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
        loader.load(url_request);

Setting loader.dataFormat to URLLoaderDataFormat.BINARY, TEXT, VARIABLES is for the response data you get in the Event.COMPLETE

samrad
  • 132
  • 1
  • 7
  • My question now is, where I receive this data on the server. This is my method : @RequestMapping(value = "/",method = RequestMethod.POST) public String getSpeechRecognition(@RequestBody byte[] body) {}. But, the data that I send it is not the same that the data received. – tludmetal Oct 28 '13 at 13:34