Applet的方法从Javascript调用 [英] Applet method calling from Javascript
问题描述
我有一个小程序上传特定文件夹中的一些文件,并删除他们,但事情是错误的,当我把一个小程序功能,从我的javascript code,当我打电话从 init的功能( )
它工作正常。
我的小程序code:
公共类上传扩展的Applet {
串SERVERPATH;
串clientPath;
私人JSObject取胜;
@覆盖
公共无效的init(){
SERVERPATH =的getParameter(SERVERPATH);
clientPath =的getParameter(clientPath);
尝试{
赢= JSObject.getWindow(本);
}赶上(JSException E){
log.warning(无法访问JSObject对象);
}
上传(专题,clientPath);
}
公共无效上传(字符串话题,字符串clientPath){
log.log(Level.SEVERE,上传functiond);
DefaultHttpClient客户端=新DefaultHttpClient();
MultipartEntity形式=新MultipartEntity();
log.log(Level.SEVERE,上传functiond2);
尝试{
文件目录=新的文件(clientPath);
log.log(Level.SEVERE,上传functiond2.2);
文件[] =文件directory.listFiles();
log.log(Level.SEVERE,上传functiond2.5);
INT I = 0;
对于(文件文件:文件){
log.log(Level.SEVERE,上传functiond2.6);
我++;
form.addPart(文件+将String.valueOf(i)中,新FileBody(文件));
的System.out.println(添加文件+将String.valueOf(我)++文件);
log.log(Level.SEVERE,添加文件+将String.valueOf(我)++文件);
}
log.log(Level.SEVERE,上传functiond3);
form.addPart(主题,新StringBody(专题,Charset.forName(UTF-8)));
form.addPart(行动,新StringBody(动作,Charset.forName(UTF-8)));
form.addPart(路径,新StringBody(SERVERPATH,Charset.forName(UTF-8)));
HttpPost后=新HttpPost(SERVERPATH);
....
这是我的javascript code:
document.applet.upload(标题,C:\\扫描);
当我从JavaScript叫只记录打印:
log.log(Level.SEVERE,上传functiond2.2);
请注意,当我从调用初始化
小程序的方法,它工作正常。
我总结我的code到 PriviligedAction
,但前进只有一步,并挂在
log.log(Level.SEVERE,上传functiond2.5);
Java和JS的相互作用复杂的安全性。该JRE不能相信JS,所以决定包括您code是不可信的整个操作链。有一种方法来解决它。
在code需要被包裹在一个的 的PrivilegedAction
,并呼吁使用的 AccessController的
的方法是 doPrivileged的(..)
。看 AccessController的
文档的顶部。 (上述方法)来查看示例用法。
I have an applet to upload some files from specific folder and delete them,but something is wrong when I call an applet function from my javascript code, when I call that function from init()
it works fine.
My applet code :
public class Uploader extends Applet {
String serverPath;
String clientPath;
private JSObject win;
@Override
public void init() {
serverPath = getParameter("serverPath");
clientPath = getParameter("clientPath");
try {
win = JSObject.getWindow(this);
} catch (JSException e) {
log.warning("Can't access JSObject object");
}
upload(topic,clientPath);
}
public void upload(String topic,String clientPath) {
log.log(Level.SEVERE, "upload functiond");
DefaultHttpClient client = new DefaultHttpClient();
MultipartEntity form = new MultipartEntity();
log.log(Level.SEVERE, "upload functiond2");
try {
File directory = new File(clientPath);
log.log(Level.SEVERE, "upload functiond2.2");
File[] files = directory.listFiles();
log.log(Level.SEVERE, "upload functiond2.5");
int i = 0;
for (File file : files) {
log.log(Level.SEVERE, "upload functiond2.6");
i++;
form.addPart("file" + String.valueOf(i), new FileBody(file));
System.out.println("adding file " + String.valueOf(i) + " " + file);
log.log(Level.SEVERE, "adding file " + String.valueOf(i) + " " + file);
}
log.log(Level.SEVERE, "upload functiond3");
form.addPart("topic", new StringBody(topic, Charset.forName("UTF-8")));
form.addPart("action", new StringBody(action, Charset.forName("UTF-8")));
form.addPart("path", new StringBody(serverPath, Charset.forName("UTF-8")));
HttpPost post = new HttpPost(serverPath);
....
and this is my javascript code:
document.applet.upload(title,"c:\scan");
When I called from javascript only log printed:
log.log(Level.SEVERE, "upload functiond2.2");
Note that when I call from init
method of applet it works fine.
I wrap my code into a PriviligedAction
, but goes only one step forward and hang on
log.log(Level.SEVERE, "upload functiond2.5");
The interaction of Java and JS complicates security. The JRE cannot trust the JS, so it decides the entire 'chain of operations' that include your code is untrusted. There is a way to fix it.
The code needs to be wrapped in a PrivilegedAction
and called using one of the AccessController
methods that doPrivileged(..)
. Look at the top of the AccessController
docs. (above the methods) to see example usage.
这篇关于Applet的方法从Javascript调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!