我是 GWT 中的 n00b。 我想使用 RandomAccessFile 从文件中读取并显示在网页上。
我发现 GWT 客户端不支持 RandomAccessFile,要调用 java 函数,我们必须使用 JSNI。
我有一个 JSNI 函数,它调用具有 RandomAccessFile 代码的函数。 当我运行 Web 项目时,出现以下错误,但页面已加载,并且我能够在文本框中看到文件内容。
错误: 14:58:43.854 [错误] [monitoringactivity] 第 14 行 没有可用于 java.io.RandomAccessFile 类型的源代码;您是否忘记继承所需的模块?
谁能帮我解决这个错误。
此 RandomAccessFile 代码的实现位于与客户端相同的包中的不同 java 文件中。我必须在 gwt.xml 文件中添加任何内容吗?
代码:
package com.monitor.client;
import java.io.RandomAccessFile;
public class JsniExample {
static String res;
public static String testRandomAccessFile()
{
try {
// Connect
RandomAccessFile pWrite = new RandomAccessFile("/tmp/file1", "rw");
String echoText = "Hello World\n";
System.out.println("Write \n");
// write
pWrite.write ( echoText.getBytes() );
pWrite.close();
RandomAccessFile pRead = new RandomAccessFile("/tmp/file2", "rw");
System.out.println("read\n");
// read response
res = pRead.readLine();
System.out.println("Response: " + res );
pRead.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
public static native String jsniCode()
/*-{
var res = @com.monitor.client.JsniExample::testRandomAccessFile()();
return res.toString();
}-*/;
}
从 onModuleLOad 我调用:res = JsniExample.jsniCode();
最佳答案
GWT不支持所有File
相关功能,只能在服务器端使用。 GWT 交叉编译为 Javascript 并在浏览器上运行,您只能访问浏览器中运行的 Javascript 可以访问的内容,而文件系统上没有任何内容。
JSNI
is for creating Javascript "native" code在 GWT 编译器中使用它,它与调用 Java 代码无关。
您必须编写代码,从网站上的文件中读取数据,该文件在服务器上执行,并以某种方式使用RPC
将该数据发送到浏览器。
JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like manipulating files.
关于java - JSNI : GWT :No source code is available for type java. io.RandomAccessFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5795991/