Applet Java: afficher une image depuis un DD

Fermé
qwark - 26 mars 2012 à 12:50
 qwark - 26 mars 2012 à 14:35
Bonjour,


je suis novice en Java, j'aimerais connaitre un moyen d'intégrer une image sur une page web depuis le disque dur local de l'utilisateur.
J'arrive à récupérer le nom de fichier sélectionné depuis une boite de dialogue javascript (type="file"), ou afficher une image sur le serveur...
J'ai signé mon applet afin qui l'application ait accès directement au contenu local, mais toujours rien :/ ...
Des pistes?


Merci.


voici le code que j'utilise:


import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.event.*; 
import javax.imageio.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; 
import java.lang.System;

// Graphics and Image Classes 
import java.awt.image.*;
import java.io.*; 
import java.io.File; 
import com.sun.image.codec.jpeg.*; 



public class helloworld extends Applet {

    StringBuffer buffer; 
    Graphics graph=null; 


    private Image image = null; // A la construction de notre objet, image est initialisé à null

    public void init() { 
        
        //System.setSecurityManager(null); 
        buffer = new StringBuffer(); 
        buffer.append("Ici l'Applet :"); 
        repaint(); 

	}
    
    public void paint(Graphics g) {
        
        g.drawString( buffer.toString(),20,25); 
        graph=g;   
    
    } 
}
A voir également:

2 réponses

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
26 mars 2012 à 13:28
Tu ne peux pas toucher aux fichiers à moins que tu ne disposes d'un certificat de sécurité (ce dont je doute...)

What Applets Can and Cannot Do :
[Unsigned applets] cannot access client resources such as the local filesystem, executable files, system clipboard, and printers.
0
j'ai finalement finis par trouver ce code qui fonctionne parfaitement pour afficher une image depuis le disque (avec le jar évidemment signé)... maintenant je dois récupérer cette image et faire un "drag n drop" pour l'envoyer dans un champ html...
Si vous avez une idée je suis toujours preneur ;)

<html>
<head>
</head>
<body bgcolor="#ffffff">
<applet codebase="./" code="test.class" archive="test2.jar" name="myApplet" width="500" height="500"></applet>
    <!-- Fonction de test de récupération de données de l'applet  -->
    <a href="javascript:alert(document.myApplet.getString());">chemin du répertoire</a>
</body>
</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());
        }
    }
}

0