二进制数据和 GWT [英] Binary Data and GWT

查看:34
本文介绍了二进制数据和 GWT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经有几个关于二进制数据和 GWT 的问题.阅读它们后,我仍然不确定以下是否可行(尽管我是一个完整的 GWT 初学者!):

There are a couple of questions about binary data and GWT already. After reading them I am still not sure if the following is possible or not (I am a complete GWT beginner though!):

我有一些非常复杂的数据文件,它们仅以二进制形式存在,我无法将它们转换为 XML 或 JSON 之类的文件.我有一个封闭源代码库,它接受一个 byte[] 并返回一个我可以使用的 Java 对象.为了让我的 GWT 应用程序运行,我打印"了这些二进制数据文件之一,并将生成的字节 [] 硬编码到我从 GWT 应用程序代码访问的 .java 文件中.一切正常.显然,这只是一个测试,在部署的应用程序中,我无法对这些数据文件进行硬编码.我想将它们放在我的 GWT 应用程序所在的目录中,并使用我的 GWT 应用程序加载"它们.

I have some very complicated data-files with only exist in binary form and I cannot convert them to something like XML or JSON. I have a closed source library though that accepts a byte[] and returns a Java object I can use. To get my GWT-app running I 'printed out' one of those binary data files and hard-coded the resulting byte[] in a .java file I access from my GWT-app code. Everything works fine. Obviously this is only a test and in the deployed app I cannot hard-code those data-files. I want to place them in the directory my GWT-app resides and the 'load' them with my GWT app.

我认为我可以使用 GWT 从我的服务器加载"文本文件,对吗?为什么我不能用 GWT 读取二进制数据?或者我可以将二进制数据文件作为文本读取并将字符串读取为字节 [] 吗?我阅读了很多关于 base64 编码的内容,并且 GWT 可以读取它,尽管我不太明白他们在说什么.我可以将我的服务器配置为以 base64 编码的形式提供这些二进制数据文件,然后使用 GWT 读取它们吗?

I take it I can 'load' text files from my server with GWT, right? Why can't I read binary data with GWT? Or can I read the binary-data-files as text and the String into a byte[]? I read a lot about base64 encoding and that GWT can read it, although I don't really understand what they are talking about. Can I configure my server to serve those binary-data-files as base64 encoded and then read them with GWT?

或者有其他解决方案吗?如果我能帮上忙,我不想碰任何 JS 代码.这就是我开始使用 GWT 的原因;)

Or is there some other solution? I wouldn't like to touch any JS code if I can help it. That's why I started using GWT ;)

感谢您的帮助:)

推荐答案

假设我们使用的是 HTML 4.

Let's presume we are on HTML 4.

GWT 客户端无法读取"文件.GWT 客户端是在浏览器上运行的 javascript.浏览器安全性不允许您读取本地文件.您必须让 servlet 代理读取服务器上的文件.

GWT client cannot "read" files. GWT client is javascript running on a browser. Browser security does not allow you to read local files. You have to get the servlet to proxy read the file for you on the server.

您设置文件的 mime 类型是因为您希望浏览器下载文件并调用本地 PC 来调用适当的软件 - 例如,pdf 调用 pdf 阅读器或 xls 调用 ms excel.与 GWT Java 或 Javascript 无关(除了启用下载).

You set the mime type for a file because you want the browser to download a file and invoke the local PC to invoke the appropriate software - for example, pdf to invoke pdf reader or xls to invoke ms excel. Nothing to do with GWT Java or Javascript (except to enable the download).

为什么需要 GWT 客户端来读取二进制文件?如果你这样做,你的架构可能是错误的.错"是一个不友好的词.也许,未对齐是一个更好的词.你的 AJAX 瘦客户端-服务器的概念是错位的.走进GWT的大门,把桌面处理的概念和习惯丢在门口.

Why do you need GWT client to read the binary file? If you do, your architecture is probably wrong. "Wrong" is an unkind word. Perhaps, misaligned is a better word. Your concept of AJAX thin client-server is misaligned. Drop your desktop processing concepts and habits at the door when you enter the door of GWT.

GWT 是 Java 但不是 Java

我不得不一直提醒人们,GWT Java 只是 Javascript 的一种更连贯的表示.当您使用 GWT Java 进行编码时,请始终记住您实际上是使用 Javascript 而不是 Java 进行编码.所有 Java 源代码都被翻译成 Javascript

I keep having to remind people that GWT Java is merely a more coherent representation of Javascript. When you code in GWT Java, always remember you are actually coding in Javascript not Java. All Java source is translated to Javascript

因此,GWT 编译器需要在源代码中提供所有 Java 类.GWT 编译器无法将 Java 字节码 jar/class 文件转换为 Javascript.如果您的库在字节码中,或者您的源库在调用链下游的任何位置调用字节码库,则编译将失败.

Therefore, the GWT compiler needs all Java classes to be supplied in source code. The GWT compiler has no ability to translate Java bytecode jar/class files into Javascript. If your library is in bytecode or your source library calls a bytecode library anywhere down the calling chain, the compilation will fail.

服务器端和客户端 GWT 之间的混淆

GWT RPC 有时会让 GWT 新手感到困惑.他们似乎没有意识到远程 servlet 是唯一被编译成字节码的部分,因为它在服务器上运行.特别是,如果您使用 Vaadin - 因为他们故意模糊了服务器和浏览器之间的界限.所以 GWT 新手开始想知道,为什么我的字节码库只能在应用的某些部分工作?"

GWT RPC is sometimes source of confusion for GWT newbies. They don't seem to realise that the remote servlet is the only part that is compiled into bytecode because it is running on the server. Especially so, if you are using Vaadin - because they have so intentionally blurred the line between server and browser. And so the GWT newbie goes off wondering, "why do my bytecode libraries work at certain parts of the app only?"

ajax 客户端服务器架构

GWT 只是一个支持 Web 的用户界面.为什么你不能在服务器上做任何你想做的事情,让服务器将它正在做什么或已经做什么反映到 UI 上?为什么必须在浏览器上完成?

GWT is merely a web enabled UI. Why can't you do whatever you want to do on the server and let the server reflect what it is doing or has done to the UI? Why must it be done on the browser?

把您的 GWT 界面想象成一个强化的 JSP.假设您正在编写一个 JSP.您是否让 JSP 将二进制数据吸入浏览器并让 JSP 生成 Javascript 以在那里分析二进制数据?

Just imagine your GWT interface as a souped up JSP. Imagine you are writing a JSP. Do you get your JSP to suck your binary data into the browser and get the JSP to generate Javascript to analyse the binary data there?

我编写了复杂的统计分析,我只是将浏览器用作服务器上正在执行的操作的反映.工程师认为他/她正在他/她的 PC 上运行分析.生成图表/报告.但这一切都是通过调用SAS在服务器上完成的.

I have written complex statistical analyses and I merely used the browser as a reflection of what is being done on the server. The engineer thinks he/she is running the analysis on his/her PC. Charts/reports are generated. But it's all done on the server by calling SAS.

面向服务的模式/架构

您的服务器将提供服务.您的浏览器 GWT 客户端将请求这些服务.打开文件,读取文件,分析文件,生成分析的视觉/mime 表示并将其传递给浏览器.只需将 GWT 浏览器客户端视为基于服务器的操作的显示监视器.GWT 是魔术师的伎俩,可以帮助我创造一种错觉,让工程师感觉他们是在本地 PC 上执行分析.当然,作为工程师,他们中的大多数人都知道浏览器实际上并没有在做这项工作.

Your server will present services. Your browser GWT client will request for those services. Open a file, read the file, analyse the file, generate a visual/mime representation of analysis and pass it to the browser. Simply think of the GWT browser client as the display monitor for your server based manipulation. GWT is a magician's trick to help me conjure the illusion to let the engineers feel they are performing analysis on the local PC. Being engineers, of course, most of them know the browser is not actually doing the work.

当您的用户对分析感到满意时,让您的服务生成结果的 mime 表示,以便浏览器可以下载它以调用由 mime 映射的适当本地 PC 软件.

When your user is satisfied with the analysis, get your service to generate a mime-representation of the results so that it could be downloaded by the browser to invoke the appropriate local PC software as mapped by the mime.

在服务器上执行并在浏览器上反映.

Do it on the server and reflect it on the browser.

进一步编辑:关于二进制数据......

Further Edits: Concerning binary data ...

在网络应用中使用 base64 编码背后的动机:身份验证令牌、图片、音频文件的传输 - 以便它们的二进制表示和排序不会被像字节序这样的架构细微差别弄乱.

The motivation behind base64 encoding being used in web apps: transmission of auth tokens, picture, audio files - so that their binary representation and sequencing would not be messed up by architectural nuances like endianness.

例如,不要尝试编写浏览器应用程序来读取原始二进制电子表格 - 在将其发送到浏览器应用程序之前,始终让服务器将其转换为 XML 或 JSON(最好是 JSON),其中任何二进制元素都应进行 base64 编码.或者,如果您的人生目的是攀登珠穆朗玛峰,请发明一种与架构无关的编码来代替 base64 来传输二进制数据.

e.g., do not attempt writing a browser app to read a raw binary spreadsheet - always have the server translate it into XML or JSON (preferably JSON) where any binary element should be base64 encoded, before sending it to the browser app. Or if the purpose of your life is to climb Mt Everest, invent an architecture-agnostic encoding in place of base64 to transmit binary data.

如果用于浏览器的操作系统处理(如音频、图片、pdf),则仅使用二进制信息.发送仅由 javascript 例程处理的二进制数据毫无意义.javascript 例程将不得不使用无关的处理时间来翻译它(除非再次,如果你的生活目的是攀登......).

Use only binary info if it was for the browser's OS processing (like audio, pictures, pdfs). No point in sending binary data to be processed solely by a javascript routine. The javascript routine would have to use extraneous processing time to translate it (unless again, if the purpose in your life is to climb ... ).

这篇关于二进制数据和 GWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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