2013-09-16 71 views
1

我不知道在Flash或ActionScript中編程。其實我是一個Java EE開發人員。從actionscript發送文件到servlet

在一個Flash文件我有這樣的方法:

private function recordComplete(e:Event):void 
    { 
     fileReference.save(recorder.output, "recording.wav"); 

    } 

此方法將保存錄制的聲音「爲recording.wav」我們將指定文件夾中。

我想要做的是通過將錄製的聲音發送到Java Servlet來將保存更改爲磁盤。

我發現這個代碼,但我不知道如何插入recorder.output在HTTP請求發送PARAMS:

var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1:8080/uploading/upservlet"); 
     uploadRequest.method = URLRequestMethod.POST; 
     uploadRequest.contentType = "multipart/form-data"; 
     uploadRequest.data = myByteArray; 

     var uploader:URLLoader = new URLLoader; 
     uploader.addEventListener(ProgressEvent.PROGRESS, onUploadProgress); 
     uploader.addEventListener(Event.COMPLETE, onUploadComplete); 
     uploader.dataFormat = URLLoaderDataFormat.BINARY; 
     uploader.load(uploadRequest); 

請幫助。

回答

3

默認情況下,flash無法通過參數創建multipart請求,您必須手動構建它。下面是我在projecs使用的簡單實用的方法:

private static const BOUNDARY:String = "boundary"; 

/** 
* Create multipart request for URLLoader 
* NOTE: Don't forget to set the URLLoader.dataFormat = URLLoaderDataFormat.BINARY; 
* @param url upload url 
* @param bytes bytes to upload 
*/ 
public static function createMultiPartRequest(url:String, bytes:ByteArray, fileProp:String="file1", fileName:String="file1.png", params:Object=null):URLRequest 
{ 
    var request:URLRequest = new URLRequest(url); 

    var header1:String = "\r\n--" + BOUNDARY + "\r\n" + 
     "Content-Disposition: form-data; name=\""+fileProp+"\"; filename=\""+fileName+"\"\r\n" + 
     "Content-Type: image/png\r\n" + "\r\n"; 
    var headerBytes1:ByteArray = new ByteArray(); 
    headerBytes1.writeMultiByte(header1, "ascii"); 
    var postData:ByteArray = new ByteArray(); 
    postData.writeBytes(headerBytes1, 0, headerBytes1.length); 

    if(bytes) 
     postData.writeBytes(bytes, 0, bytes.length); 

    if (!params) 
     params = {}; 
    if (!params.Upload) 
     params.Upload = "Submit Query"; 
    for (var prop:String in params) { 
     var header:String = "--" + BOUNDARY + "\r\n" + "Content-Disposition: form-data; name=\""+prop+"\"\r\n" + "\r\n" + params[prop]+"\r\n" + "--" + BOUNDARY + "--"; 
     var headerBytes:ByteArray = new ByteArray(); 
     headerBytes.writeMultiByte(header, "ascii"); 
     postData.writeBytes(headerBytes, 0, headerBytes.length); 
    } 
    request.data = postData; 
    request.method = URLRequestMethod.POST; 
    request.contentType = "multipart/form-data; boundary=" + BOUNDARY; 

    return request; 
} 

所以,你應該修改代碼以這樣的方式:

var uploadRequest:URLRequest = createMultiPartRequest("http://127.0.0.1:8080/uploading/upservlet", myByteArray, "file1", recorder.output, {param1:value1}); 

    var uploader:URLLoader = new URLLoader; 
    uploader.addEventListener(ProgressEvent.PROGRESS, onUploadProgress); 
    uploader.addEventListener(Event.COMPLETE, onUploadComplete); 
    uploader.dataFormat = URLLoaderDataFormat.BINARY; 
    uploader.load(uploadRequest); 
+0

偉大的答案! +1 –