有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAlang.NoClassDefFoundError:org/jasypt/util/text/basictExtendCryptor

我定义了一个名为CryptUrl的类,它使用BasicTextEncryptor对字符串进行加密和解密。 我试图在servlet中调用decrypt函数,但最终出现了错误

java.lang.NoClassDefFoundError: org/jasypt/util/text/BasicTextEncryptor

这是我的地下室课程:

import org.jasypt.util.text.BasicTextEncryptor;

public class CryptUrl {
    static String myEncryptionPassword ="key";
    static String message ="message";        

    public static String encrypt(String text) {
        try {   
            BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
            textEncryptor.setPassword(myEncryptionPassword);
            String myEncryptedPassword = textEncryptor.encrypt(text);            

            return myEncryptedPassword;

        } catch (Exception e) {
            return null;
        }
    }

    public static String decrypt(String text) {
        try {
            BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
            textEncryptor.setPassword(myEncryptionPassword);
            String plainText = textEncryptor.decrypt(text);
            return plainText;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }
}

这就是我调用函数的servlet

public class Download extends HttpServlet {

    private static final long serialVersionUID = 1L;
    public static final int TAILLE_TAMPON = 10240; // 10ko
    public static String message ;

    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        /*
         * Lecture du paramètre 'chemin' passé à la servlet via la déclaration
         * dans le web.xml
         */
        String chemin = this.getServletConfig().getInitParameter( "chemin" );

        /*
         * Récupération du chemin du fichier demandé au sein de l'URL de la
         * requête
         */
        String fichierRequis = request.getPathInfo(); 
        Path p = Paths.get(fichierRequis);
     //   System.out.println(p.subpath(0, p.getNameCount()));
        message =CryptUrl.decrypt(p.subpath(0, p.getNameCount()).toString());

        /* Vérifie qu'un fichier a bien été fourni */
        if ( fichierRequis == null ) {
            /*
             * Si non, alors on envoie une erreur 404, qui signifie que la
             * ressource demandée n'existe pas
             */
            response.sendError( HttpServletResponse.SC_NOT_FOUND );
            return;
        }

        /*
         * Décode le nom de fichier récupéré, susceptible de contenir des
         * espaces et autres caractères spéciaux, et prépare l'objet File
         */


        fichierRequis = URLDecoder.decode( fichierRequis, "UTF-8" );
        File fichier = new File( chemin, fichierRequis);

        /* Vérifie que le fichier existe bien */
        if ( !fichier.exists() ) {
            /*
             * Si non, alors on envoie une erreur 404, qui signifie que la
             * ressource demandée n'existe pas
             */
            response.sendError( HttpServletResponse.SC_NOT_FOUND );
            return;
        }

        /* Récupère le type du fichier */
        String type = getServletContext().getMimeType( fichier.getName() );

        /*
         * Si le type de fichier est inconnu, alors on initialise un type par
         * défaut
         */
        if ( type == null ) {
            type = "application/octet-stream";
        }

        /* Initialise la réponse HTTP */
        response.reset();
        response.setBufferSize( TAILLE_TAMPON );
        response.setContentType( type );
        response.setHeader( "Content-Length", String.valueOf( fichier.length() ) );
        response.setHeader( "Content-Disposition", "attachment; filename=\"" + fichier.getName() + "\"" );

        /* Prépare les flux */
        BufferedInputStream entree = null;
        BufferedOutputStream sortie = null;
        try {
            /* Ouvre les flux */
            entree = new BufferedInputStream( new FileInputStream( fichier ), TAILLE_TAMPON );
            sortie = new BufferedOutputStream( response.getOutputStream(), TAILLE_TAMPON );

            /* Lit le fichier et écrit son contenu dans la réponse HTTP */
            byte[] tampon = new byte[TAILLE_TAMPON];
            int longueur;
            while ( ( longueur = entree.read( tampon ) ) > 0 ) {
                sortie.write( tampon, 0, longueur );
            }
        } finally {
            sortie.close();
            entree.close();
        }
    }

共 (1) 个答案

  1. # 1 楼答案

    确保在你的类路径中有下面的JAR。然而,版本可能不同

        commons-codec-1.1.jar
        commons-lang-2.1.jar
        jasypt-1.6.jar