Accès fichiers client depuis applet
Bonjour,
j'ai besoin de pouvoir ouvrir un répertoire sur le client (à l'aide de l'explorateur), et même, plus tard, d'écrire des fichiers sur le client.
J'utilise donc une applet.
Voici comment je procède :
L'applet :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package mypackage;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.IOException;
import javax.swing.JApplet;
import javax.swing.JLabel;
public class Applet1 extends JApplet {
private JLabel label = new JLabel();
public void init(){
this.setSize(200, 100);
label.setHorizontalAlignment(JLabel.CENTER);
label.setForeground(Color.blue);
label.setText("Applet chargée");
this.getContentPane().add(label, BorderLayout.NORTH);
}
public void openFolder(String path){
label.setText(path);
try {
Runtime.getRuntime().exec("cmd /c start "+path);
}
catch(IOException e){ e.printStackTrace(); }
}
} |
La fonction openFolder sera appelée depuis JavaScript.
J'y ai intégré un JLabel pour voir si l'appel fonctionne.
Ainsi depuis le Javascript, je l'appel comme ceci sur le clic d'un bouton :
Code:
document.applets[0].openFolder('C:\\jan');
Lorsque j'exécute cmd /c start C:\\jan directement dans "Démarrer -> Exécuter", cela m'ouvre bien le répertoire, donc la commande est bonne.
Signature de l'applet :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
C:\Documents and Settings\moi>cd C:\DevSuiteHome_1\jdk\bin
C:\DevSuiteHome_1\jdk\bin>keytool -genkey -alias MyApplet
Enter keystore password: mypwd
What is your first and last name?
[Unknown]: My Name
What is the name of your organizational unit?
[Unknown]: My Unit
What is the name of your organization?
[Unknown]: My Organization
What is the name of your City or Locality?
[Unknown]: Geneva
What is the name of your State or Province?
[Unknown]: GE
What is the two-letter country code for this unit?
[Unknown]: CH
Is CN=My Name, OU=My Unit, O=My Organization, L=Geneva, ST=GE, C=CH correct?
[no]: y
Enter key password for <MyApplet>
(RETURN if same as keystore password): myappletpwd
C:\DevSuiteHome_1\jdk\bin>jarsigner -verbose archive1.jar MyApplet
Enter Passphrase for keystore: mypwd
Enter key password for MyApplet: myappletpwd
updating: META-INF/MANIFEST.MF
adding: META-INF/GEODEAPP.SF
adding: META-INF/GEODEAPP.DSA
signing: mypackage/Applet1$1.class
signing: mypackage/Applet1.class
C:\DevSuiteHome_1\jdk\bin>jarsigner -verify -verbose archive1.jar
221 Wed Jun 22 14:20:56 CEST 2011 META-INF/MANIFEST.MF
278 Wed Jun 22 14:20:56 CEST 2011 META-INF/GEODEAPP.SF
995 Wed Jun 22 14:20:56 CEST 2011 META-INF/GEODEAPP.DSA
smk 1180 Tue Jun 21 10:01:08 CEST 2011 mypackage/Applet1$1.class
smk 1537 Wed Jun 22 09:31:20 CEST 2011 mypackage/Applet1.class
s = signature was verified
m = entry is listed in manifest
k = at least one certificate was found in keystore
i = at least one certificate was found in identity scope
jar verified.
C:\DevSuiteHome_1\jdk\bin> |
Donc le jar est bien signé.
Code HTML de l'applet :
Code:
1 2 3
| <applet archive="monchemin/archive1.jar" code="mypackage/Applet1.class" width="200" height="100">
Applet Java
</applet> |
Voilà, tout à l'air bon, néanmoins lorsque j'exécute ma page avec Firefox (3.6), j'ai bien l'avertissement de sécurité («The application's digital signature cannot be verified. Do you want to run the application»). Je répond "run" bien sûr.
Le JLabel de l'applet affiche bien "Applet chargée".
Puis quand je clic sur mon bouton qui lance la fonction javascript, j'ai bien "C:\jan" qui s'affiche dans le JLabel. Donc la communication fonctionne bien.
Seulement rien ne se passe, pas d'ouverture de répertoire, et dans la console Firefox j'ai l'erreur :
Citation:
Error: uncaught exception: java.security.AccessControlException: access denied (java.io.FilePermission <<ALL FILES>> execute) à chaque fois aue je clic sur le bouton.
Aucune erreur dans la console Java.
Aurais-je oublié quelque-chose ?
Merci pour votre aide.