Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

java - Send file from actionscript to servlet

Ihave no idea about programming in Flash or Actionscript. Actually I am a Java EE Developer.

In a flash file I have this method:

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

    }

This method will save a recorded sound to "recording.wav" in the folder that we will specify.

What I want to do is to change the save to the disk by sending the recorded sound to a Java Servlet.

I found this code, but I dont know how to insert the recorder.output in params sent in the HTTP Request:

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

Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By default flash can't create multipart request with parameters, you have to construct it manually. Here is the simple utility method I used in my 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 = "
--" + BOUNDARY + "
" + 
        "Content-Disposition: form-data; name=""+fileProp+""; filename=""+fileName+""
" + 
        "Content-Type: image/png
" + "
";
    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 + "
" + "Content-Disposition: form-data; name=""+prop+""
" + "
" + params[prop]+"
" + "--" + 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;
}

So you should modify your code in such way:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...