我需要從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")
任何人知道發生了什麼事?
我不得到響應數據'不知道'actionscript'和它的API,但是我可以告訴你:'actionscript'中的內存中的ByteArray'與'Java'中的byte []'完全不同。 –
你不應該更好地使用URLLoaderDataFormat.BINARY嗎?它有任何改變嗎? – Fygo
我使用這種結構,因爲我在本示例中看到: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