devstory

Exemple de Java encoding et decoding utilisant Apache Base64

  1. Ví dụ Encode và Decode

1. Ví dụ Encode và Decode

Un exemple utilisant Apache Axis pour coder (encode) et décoder (decode) un extrait de texte :
La bibliothèque Apache Axis, voú trouverez le lien de téléchargement dans le lien ci-dessous :
Si vous utilisez Maven:
* pom.xml *
<!-- http://mvnrepository.com/artifact/org.apache.axis/axis -->
<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>
EncodeDecodeExample.java
package org.o7planning.example.encode;

import java.io.UnsupportedEncodingException;

import org.apache.axis.encoding.Base64;

public class EncodeDecodeExample {

   // Encode
   public static String encodeString(String text)
           throws UnsupportedEncodingException {
       byte[] bytes = text.getBytes("UTF-8");
       String encodeString = Base64.encode(bytes);
       return encodeString;
   }

   // Decode
   public static String decodeString(String encodeText)
           throws UnsupportedEncodingException {
       byte[] decodeBytes = Base64.decode(encodeText);
       String str = new String(decodeBytes, "UTF-8");
       return str;
   }
   
   
   public static void main(String[] args) throws UnsupportedEncodingException  {
       String text = "Example Vietnamese text - Tiếng Việt";
       
       System.out.println("Text before encode: "+ text);
       
       String encodeText = encodeString(text);
       System.out.println("Encode text: "+ encodeText);
       
       String decodeText =  decodeString(encodeText);
       System.out.println("Decode text: "+ decodeText);        
   }

}
Les résultats de l'exécution d'exemple :

Java Basic

Show More