如何从AS3通过Java二进制数据发送到一个文件系统? [英] How to send binary data from AS3 through Java to a filesystem?

查看:183
本文介绍了如何从AS3通过Java二进制数据发送到一个文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在AS3需要被COM pressed,验证了我的Java的谷歌应用程序引擎的servlet然后将其保存到谷歌云存储文件的XML数据。后来该文件将被打开,并DECOM pressed的AS3客户端。这个过程是,如果我这样做,与纯XML或文本,但如果我的ByteArray#COM preSS数据,这期间的ByteArray#uncom preSS死了时出错DECOM pressing数据 。

我试过设置在各点内容类型和MIME类型,以及与Base64编码,但每一次尝试似乎以不同的方式来打破我从来没有得到相同的XML回来,我发送,我是否需要使用多?我应该COM服务器上的preSS?什么是最好的做法这样做呢?

从AS3发送数据:

  // COM preSS XML使用zlib的
VAR的xml:XML =<内容><东西>值小于/事>< /内容&gt ;;
VAR字节:的ByteArray =新的ByteArray();
bytes.writeObject(XML);
bytes.position = 0;
bytes.com preSS();

VAR要求:的URLRequest =新的URLRequest(URL);
VAR的URLVariables:使用URLVariables =新的URLVariables();
urlVariables.filename =somefile.bin;
urlVariables.contents =字节;
request.data =使用URLVariables;
request.method = URLRequestMethod.POST;
装载机=新的URLLoader();
loader.load(要求);
 

接收它在Java servlet和创建文件:

 公共无效的doPost(HttpServletRequest的REQ,HttpServletResponse的RESP)抛出IOException异常{
    字符串文件名= req.getParameter(文件名);
    。byte []的内容= req.getParameter(内容)的GetBytes();

    GSFileOptionsBuilder optionsBuilder =新GSFileOptionsBuilder()
        .setBucket(bucketname)
        .setKey(文件名)
        .setAcl(大众阅读的)
        .setMimeType(二进制/八位字节流);
    AppEngineFile writableFile = fileService.createNewGSFile(optionsBuilder.build());
    布尔lockForWrite = TRUE;
    FileWriteChannel writeChannel = fileService.openWriteChannel(writableFile,lockForWrite);
    writeChannel.write(ByteBuffer.wrap(内容));
    writeChannel.closeFinally();
}
 

打开新的文件在AS3:

 变种网址:字符串=htt​​p://commondatastorage.googleapis.com/bucketname/somefile.bin;
VAR要求:的URLRequest =新的URLRequest(URL);
request.method = URLRequestMethod.GET;
装载机=新的URLLoader();
loader.addEventListener(引发Event.COMPLETE,handleComplete);
loader.load(要求);

保护功能handleComplete(事件:事件):无效{
    VAR字节:的ByteArray =新的ByteArray();
    bytes.writeObject(event.target.data);

    //死在这条线与发生错误DECOM pressing的数据。
    bytes.uncom preSS();

    VAR的xml:XML =新的XML(新的字符串(字节));
    跟踪(XML);
}
 

解决方案

我的base64 EN code您从客户端发表如果在此之前,将其存储在一个TextProerty那样,那么的base64德code / DECOM当在客户端收回preSS。如果你想将它保存为二进制文件的GAE,然后BASE64德code到一个斑点。下面是一些code段我拼凑在一起使用code,我也使用HttpService的类似的东西 - 提前道歉没有广泛打样它。 HTH。

 私人变种_serviceHttp:的HTTPService =新的HTTPService;

私有函数postBytes():无效{
    VAR的xml:XML =<内容><东西>值小于/事>< /内容&gt ;;
    VAR字节:的ByteArray =新的ByteArray();
    bytes.writeObject(XML);
    bytes.position = 0;
    bytes.com preSS();
    VAR ENC:Base64En codeR =新Base64En codeR();
    enc.en codeBytes(字节,0,bytes.length);
    VAR MyObj中:对象=新的对象();
    MyObj中[字节] = enc.toString();
    // MyObj中[other_variables] = your_other_varaibles;
    _serviceHttp.method =POST;
    _serviceHttp.resultFormat =Flash变量;
    _serviceHttp.url = your_url_here;
    _serviceHttp.addEventListener(ResultEvent.RESULT,urlHandler);
    _serviceHttp.addEventListener(FaultEvent.FAULT,urlErrorHandler);
    _serviceHttp.send(MyObj中);
}
 

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 opened and decompressed by the AS3 client. The process works if I do it with plain XML or text, but if I ByteArray#compress the data, it dies during ByteArray#uncompress with "There was an error decompressing the data".

I've tried setting the content type and mime type at various points, as well as encoding with Base64, but every attempt seems to break in a different way and I never get the same XML back that I sent in. Do I need to use multipart? Should I compress on the server? What's the best practice for doing this?

Sending the data from AS3:

// compress xml using zlib
var xml:XML = <contents><thing>value</thing></contents>;
var bytes:ByteArray = new ByteArray();
bytes.writeObject(xml);
bytes.position = 0;
bytes.compress();

var request:URLRequest = new URLRequest(url);
var urlVariables :URLVariables = new URLVariables();
urlVariables.filename = "somefile.bin";
urlVariables.contents = bytes;
request.data = urlVariables;
request.method = URLRequestMethod.POST;
loader = new URLLoader();
loader.load(request);

Receiving it in the Java servlet and creating the file:

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String filename = req.getParameter("filename");
    byte[] contents = req.getParameter("contents").getBytes();

    GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
        .setBucket("bucketname")
        .setKey(filename)
        .setAcl("public-read")
        .setMimeType("binary/octet-stream");
    AppEngineFile writableFile = fileService.createNewGSFile(optionsBuilder.build());
    boolean lockForWrite = true;
    FileWriteChannel writeChannel = fileService.openWriteChannel(writableFile, lockForWrite);
    writeChannel.write(ByteBuffer.wrap(contents));
    writeChannel.closeFinally();
}

Opening the new file in AS3:

var url :String = "http://commondatastorage.googleapis.com/bucketname/somefile.bin";
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.GET;
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handleComplete);
loader.load(request);

protected function handleComplete (event:Event):void {
    var bytes:ByteArray = new ByteArray();
    bytes.writeObject(event.target.data);

    // dies on this line with "There was an error decompressing the data."
    bytes.uncompress();

    var xml:XML = new XML(new String(bytes));
    trace(xml);
}

解决方案

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

这篇关于如何从AS3通过Java二进制数据发送到一个文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆