How to send binary data from AS3 through Java to a filesystem?

后端 未结 2 381
余生分开走
余生分开走 2021-01-24 11:33

I\'ve got XML data in AS3 that needs to be compressed, validated on my Java Google App Engine servlet then saved to a file in Google Cloud Storage. Later that file will be opene

相关标签:
2条回答
  • 2021-01-24 12:14

    Here is the code that I use to save an xml. I send the data to PHP but I would think it would work the same way for you... I haven't had any trouble with it.

    var createXMLURL:URLRequest = new URLRequest("createXML.php");
    createXMLURL.method = URLRequestMethod.POST;
    var Variables:URLVariables = new URLVariables();
    Variables.xmlString = xml.toXMLString();
    Variables.filename = filename;
    createXMLURL.data = Variables;
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = "VARIABLES";
    loader.addEventListener(Event.COMPLETE, xmlCreated);
    loader.load(createXMLURL);
    

    Let me know if you have any questions about what some of the variables are since I did not include their declarations (I think they are pretty easy to figure out).

    Now this doesn't send that data in binary format like you were asking for, but I don't know why you wouldn't be able to convert the string to binary once you receive it in java if you really need the raw bytes.

    0 讨论(0)
  • 2021-01-24 12:38

    I would base64 encode before you POST if from the client, store it that way in a TextProerty, then base64 decode / decompress when received back at the client. If you want to store it as binary on GAE, then base64 decode it into a Blob. Here are some code snippets I pieced together using your code, and something similar I do using HttpService -- apologies in advance for not extensively proofing it. HTH.

    private var _serviceHttp:HTTPService = new HTTPService;
    
    private function postBytes():void {
        var xml:XML = <contents><thing>value</thing></contents>;
        var bytes:ByteArray = new ByteArray();
        bytes.writeObject(xml);
        bytes.position = 0;
        bytes.compress();
        var enc:Base64Encoder = new Base64Encoder();
        enc.encodeBytes(bytes, 0, bytes.length);
        var myObj:Object = new Object();
        myObj["bytes"] = enc.toString();
        // myObj["other_variables"] = your_other_varaibles;
        _serviceHttp.method = "POST";
        _serviceHttp.resultFormat = "flashvars";
        _serviceHttp.url = your_url_here;
        _serviceHttp.addEventListener(ResultEvent.RESULT, urlHandler);
        _serviceHttp.addEventListener(FaultEvent.FAULT, urlErrorHandler);
        _serviceHttp.send(myObj);
    }
    
    0 讨论(0)
提交回复
热议问题