从AS3发送bytearray到Spring MVC servlet

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

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

}

我收到空在这个参数:

req.getParameter("contents") 

任何人知道发生了什么事?

回答:

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

 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,变量是您在Event.COMPLETE

以上是 从AS3发送bytearray到Spring MVC servlet 的全部内容, 来源链接: utcz.com/qa/257841.html

回到顶部