使用 JNLP API 访问 Client 端
通过使用 Java 网络启动协议(JNLP)启动时,富 Internet 应用程序(RIA)可以在用户许可下访问 Client 端。考虑 Text Editor applet 示例,以了解如何使用基于 JNLP API 的服务。文本编辑器有一个文本区域和标有“打开”,“保存”和“另存为”的按钮。文本编辑器可用于打开现有文本文件,对其进行编辑,然后将其保存回磁盘。
接下来显示“文本编辑器”Servlets。
A browser with JavaScript enabled is required for this page to operate properly\.
Note:
如果看不到 Servlets 正在运行,则需要至少安装Java SE 开发套件(JDK)6 更新 10版本。
Note:
如果看不到示例正在运行,则可能需要在浏览器中启用 JavaScript 解释器,以便 Deployment Toolkit 脚本能够正常运行。
TextEditor和TextEditorApplet类布置用户interface并将其显示为 applet。 FileHandler类包含有关使用基于 JNLP API 的服务的核心功能。
请记住,本主题中描述的技术也适用于 Java Web Start 应用程序。
要使用 JNLP 服务,请首先检索对该服务的引用。 FileHandler
类的initialize
方法检索对 JNLP 服务的引用,如以下代码片段所示:
private static synchronized void initialize() {
...
try {
fos = (FileOpenService)
ServiceManager.lookup("javax.jnlp.FileOpenService");
fss = (FileSaveService)
ServiceManager.lookup("javax.jnlp.FileSaveService");
} catch (UnavailableServiceException e) {
...
}
}
引用所需的服务后,请在服务上调用方法以执行必要的操作。 FileHandler
类的open
方法调用FileOpenService类的openFileDialog
方法以显示文件 selectors。 open
方法返回所选文件的内容。
public static String open() {
initialize();
try {
fc = fos.openFileDialog(null, null);
return readFromFile(fc);
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
return null;
}
}
类似地,FileHandler
类的save
和saveAs
方法调用FileSaveService类的相应方法,以使用户能够选择文件名并将文本区域的内容保存到磁盘。
public static void saveAs(String txt) {
initialize();
try {
if (fc == null) {
// If not already saved.
// Save-as is like save
save(txt);
} else {
fc = fss.saveAsFileDialog(null, null,
fc);
save(txt);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
}
在运行时,当 RIA try打开或保存文件时,用户会看到一个安全对话框,询问他们是否要允许该操作。仅当用户允许 RIA 访问其环境时,该操作才会 continue 进行。
接下来显示FileHandler类的完整来源。
// add javaws.jar to the classpath during compilation
import javax.jnlp.FileOpenService;
import javax.jnlp.FileSaveService;
import javax.jnlp.FileContents;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;
import java.io.*;
public class FileHandler {
static private FileOpenService fos = null;
static private FileSaveService fss = null;
static private FileContents fc = null;
// retrieves a reference to the JNLP services
private static synchronized void initialize() {
if (fss != null) {
return;
}
try {
fos = (FileOpenService) ServiceManager.lookup("javax.jnlp.FileOpenService");
fss = (FileSaveService) ServiceManager.lookup("javax.jnlp.FileSaveService");
} catch (UnavailableServiceException e) {
fos = null;
fss = null;
}
}
// displays open file dialog and reads selected file using FileOpenService
public static String open() {
initialize();
try {
fc = fos.openFileDialog(null, null);
return readFromFile(fc);
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
return null;
}
}
// displays saveFileDialog and saves file using FileSaveService
public static void save(String txt) {
initialize();
try {
// Show save dialog if no name is already given
if (fc == null) {
fc = fss.saveFileDialog(null, null,
new ByteArrayInputStream(txt.getBytes()), null);
// file saved, done
return;
}
// use this only when filename is known
if (fc != null) {
writeToFile(txt, fc);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
}
// displays saveAsFileDialog and saves file using FileSaveService
public static void saveAs(String txt) {
initialize();
try {
if (fc == null) {
// If not already saved. Save-as is like save
save(txt);
} else {
fc = fss.saveAsFileDialog(null, null, fc);
save(txt);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
}
private static void writeToFile(String txt, FileContents fc) throws IOException {
int sizeNeeded = txt.length() * 2;
if (sizeNeeded > fc.getMaxLength()) {
fc.setMaxLength(sizeNeeded);
}
BufferedWriter os = new BufferedWriter(new OutputStreamWriter(fc.getOutputStream(true)));
os.write(txt);
os.close();
}
private static String readFromFile(FileContents fc) throws IOException {
if (fc == null) {
return null;
}
BufferedReader br = new BufferedReader(new InputStreamReader(fc.getInputStream()));
StringBuffer sb = new StringBuffer((int) fc.getLength());
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
br.close();
return sb.toString();
}
}
Note:
要编译引用了javax.jnlp
包中的类的 Java 代码,请在 Classpath 中包含<your JDK path>/jre/lib/javaws.jar
。在运行时,Java Runtime Environment 软件自动将这些类提供给 RIA。
下载源代码用于“文本编辑器 Servlets”示例,以进行进一步试验。