2016-08-31 4 views
1

CryptUrlというクラスを定義し、文字列の暗号化と復号化にはBasicTextEncryptorを使用しました。 私は私のサーブレットでdecrypt機能を呼び出そうとしましたが、私はjava.lang.NoClassDefFoundError:org/jasypt/util/text/BasicTextEncryptor

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; 
    } 
    } 

}

と、これは私サーブレットです関数を呼び出す

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

ランタイムクラスパスに関連するJARがないような音がします。 –

+0

IDEを使用しているのか、Terminalでコンパイルしていますか?外部のjarファイルをインポートする必要があります –

+0

サーブレットコンテナの 'lib'に' jasypt.jar'を入れてください –

答えて

0

以下のjars iがあることを確認してくださいあなたのクラスパス。バージョンは異なる可能性があります

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

私はtomcatライブラリフォルダに追加してくれてありがとうございます –

+0

あなたはあなたにその助けがあれば答えを受け入れることができます:) –