0

我需要将 ByteArray 从 Flex 发送到 Spring MVC servlet,但它不起作用。我录制了一个音频样本,在 AS3 中转换为 bytearray 并发送到 SpringMVC servlet,但接收 Java 的数据为空。

AS3 代码:

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代码:

    @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";
}

我在此参数中收到 null :

req.getParameter("contents")

有人知道发生了什么吗?

4

1 回答 1

1

这会将您的数据作为二进制发送:

        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);

将 loader.dataFormat 设置为 URLLoaderDataFormat.BINARY, TEXT, VARIABLES 是针对您在 Event.COMPLETE 中获得的响应数据

于 2013-10-25T10:31:12.513 回答