如何写在Java小程序的文件? [英] How to write to a file in applets in java?
问题描述
由于小程序在浏览器中沙盒模式下运行,我使用AccessController.doPrivileged写入一个文件。它写道:当我在Eclipse中运行该文件,但是当我在浏览器中访问Applet不写。
我在想什么?这里是code:
公共类的HelloWorld扩展的Applet { 公共无效漆(图形G){
在AccessController.doPrivileged(新的PrivilegedAction<布尔>(){
公共布尔的run(){
尝试{
的System.out.println(System.getProperty(的user.home));
串USERHOME = System.getProperty(的user.home);
FileWriter的FW =新的FileWriter(USERHOME +文件分割符
+测试+文件分割符+area.txt);
fw.write(面积为20m);
fw.flush();
fw.close(); }赶上(IOException异常IOE){
通信System.err.println(IOE);
}
返回Boolean.TRUE;
}
});
}
}
AccessController.doPrivileged
不会做你认为 1 。
但首先到一个小程序可以访问本地文件系统中的两(实用)的方式。
- 数字签名的小程序,然后在提示时让用户确定该小程序。
- 嵌入式小程序在运行的1.6.0_10 + JRE还可以访问JNLP API的服务,其中包括JNLP API文件服务。他们可以在一个沙箱应用工作。 - 他们只是提示用户,当小程序去加载或保存文件。当然,一个的自由浮动的小程序使用JWS可以做,因为Java 1.2的相同的推出,但因为1.6.0_10,同样是这些小程序可以保持嵌入。请参阅演示。该文件的服务的一个小应用程序。附带完整的源代码,这或其他小GIF动画制造商它在使用内嵌小应用程序。
您可能注意到,我没有列出切实可行的方法列表中选择调整策略文件/设置。这是因为它不是真正的实用。至少不会超出一个封闭的内部网络中部署他们的人控制目标机器什么(安培,从而可以安装一个策略文件以允许该applet信任)。但在这种情况下,一个小程序的好处是严重侵蚀在任何情况下。
- 它的作用是允许一个小程序的已经值得信赖的使用非可信源,如JavaScript调用。如果添加,实际上确实改变一个applet的安全环境,而大量的钟声和口哨声警告最终用户,这将是一个的安全漏洞。的
Since Applets run in sandbox mode in browsers, I am using AccessController.doPrivileged to write to a file. It writes to the file when I run it in Eclipse, but doesn't write when I access the applet in browser. What am I missing? Here is the code:
public class HelloWorld extends Applet {
public void paint(Graphics g) {
AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
try {
System.out.println(System.getProperty("user.home"));
String userHome = System.getProperty("user.home");
FileWriter fw = new FileWriter(userHome + File.separator
+ "test" + File.separator + "area.txt");
fw.write("The area is 20m");
fw.flush();
fw.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
return Boolean.TRUE;
}
});
}
}
AccessController.doPrivileged
does not do what you think1.
But first to the two (practical) ways that an applet can access the local file system.
- Digitally sign the applet, then have the user OK that applet when prompted.
- Embedded applets running in a 1.6.0_10+ JRE can also access the services of the JNLP API, which include the JNLP API file services. They can work in a sand-boxed app. - they simply prompt the user when the applet goes to load or save a file. Of course, a free floating applet launched using JWS could do the same since Java 1.2, but since 1.6.0_10, those same applets can remain embedded. See the demo. of the file services in a small app. that comes complete with source, or this other small animated GIF maker for it used in an embedded applet.
You might note that I did not list 'adjust policy files/settings' in the list of practical ways. That is because it is not really practical. At least not for anything beyond a closed intranet in which the person deploying them controls the target machines (& can thereby install a policy file to allow the applet trust). But then in that situation, the benefits of an applet are severely eroded in any case.
- What it does is allow an applet that is already trusted to be called using a non-trusted source such as JavaScript. If adding that actually did change the security environment of an applet without lots of bells and whistles warning the end user, it would be a security bug.
这篇关于如何写在Java小程序的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!