2013-10-11 75 views
0

我需要從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") 

任何人知道發生了什麼事?

+0

我不得到響應數據'不知道'actionscript'和它的API,但是我可以告訴你:'actionscript'中的內存中的ByteArray'與'Java'中的byte []'完全不同。 –

+0

你不應該更好地使用URLLoaderDataFormat.BINARY嗎?它有任何改變嗎? – Fygo

+0

我使用這種結構,因爲我在本示例中看到:http://stackoverflow.com/questions/10096774/how-to-send-binary-data-from-as3-through-java-to-a-filesystem和http: //nxhoaf.wordpress.com/2012/05/12/speech-to-text-in-action-script-3-part-3/ – tludmetal

回答

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

+0

我現在的問題是,我在服務器上收到這些數據。這是我的方法:@RequestMapping(value =「/」,method = RequestMethod.POST) \t public String getSpeechRecognition(@RequestBody byte [] body){}。但是,我發送的數據與收到的數據不一樣。 – tludmetal