<html> <head> </head> [javascript:alert(document.myApplet.getString()); chemin du répertoire] </html>
import java.applet.*; import java.awt.*; import java.util.*; import java.lang.*; import java.text.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import java.net.URLDecoder; public class test extends Applet { String sFileName; ImageIcon icon; Image img; String sjsFileName; String sjsFilePath; public test() { Panel p = new Panel(); Font f; String osname = System.getProperty("os.name",""); if (!osname.startsWith("Windows")) { f = new Font("Arial",Font.BOLD,10); } else { f = new Font("Verdana",Font.BOLD,12); } p.setFont(f); p.add(new Button("Parcourir")); p.setBackground(new Color(255, 255, 255)); add("North",p); } public boolean action(Event evt, Object arg) { if (arg.equals("Parcourir")) { System.out.println("OPEN CLICKED"); // cette méthode fonctionne Frame parent = new Frame(); FileDialog fd = new FileDialog(parent, "Répertoire de l'image:", FileDialog.LOAD); fd.show(); String selectedItem = fd.getFile(); if (selectedItem == null) { // no file selected } else { // read the file //System.out.println("reading file " + fd.getDirectory() + File.separator + fd.getFile() ); sFileName = fd.getDirectory() + File.separator + fd.getFile(); displayFile(sFileName); } } else return false; return true; } public void paint(Graphics g) { int width, height; if (img!=null) { width = img.getWidth(this); height = img.getHeight(this); if (width < height) { if (height <= 500) { g.drawImage(img, 0, 40, this); }else { double ratio = 500 / (double) height; double temp2 = width*ratio; int temp = (int) temp2; g.drawImage(img, 0, 40, temp, 500, this); } } else { if (width <= 500) { g.drawImage(img, 0, 40, this); }else { double ratio = 500 / (double) width; double temp2 = height*ratio; int temp = (int) temp2; g.drawImage(img, 0, 40, 500, temp, this); } } } } public void displayFile (String sFileName){ System.out.println("file name : " + sFileName); try { String slocalFileName = URLDecoder.decode(sFileName, "UTF-8"); System.out.println("slocalFileName : " + slocalFileName); icon = new ImageIcon(slocalFileName); img = icon.getImage(); repaint(); } catch (Exception e) { System.out.println("erreur " + e.toString()); } } // quand j'appelle cette fonction directement de la page html, ça génère l'exception public void displayFile2 (String sPathName){ System.out.println("path name : " + sPathName); try { String slocalFileName = URLDecoder.decode(sPathName, "UTF-8"); System.out.println("slocalFileName : " + slocalFileName); icon = new ImageIcon(slocalFileName); img = icon.getImage(); repaint(); } catch (Exception e) { System.out.println("erreur " + e.toString()); } } public String getString() { return sFileName; } public void setFileName(String s) throws Exception { try { sjsFileName = new String( s ); System.out.println("sjsFileName : " + sjsFileName); return ; } catch (Exception e) { System.out.println("erreur " + e.toString()); } } public void setFilePath(String s) throws Exception { try { sjsFilePath = new String( s ); System.out.println("sjsFilePath (encodé): " + sjsFilePath); System.out.println("sjsFilePath (décodé): " + URLDecoder.decode(sjsFilePath, "UTF-8")); sjsFilePath = new String( URLDecoder.decode(sjsFilePath, "UTF-8") ); return ; } catch (Exception e) { System.out.println("erreur " + e.toString()); } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question