tludmetal
tludmetal

Reputation: 103

Sending bytearray from AS3 to Spring MVC servlet

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?

Upvotes: 0

Views: 510

Answers (1)

samrad
samrad

Reputation: 130

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

Upvotes: 1

Related Questions