Mục lục
Ví dụ về Java encoding và decoding sử dụng Apache Base64
Xem thêm các chuyên mục:

Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.


Một ví dụ sử dụng thư viện Apache Axis để mã hóa (encode) và giải mã (decode) một đoạn văn bản:
Thư viện Apache Axis, bạn có thể download tại:
Nếu bạn sử dụng 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 {
// Mã hóa một đoạn text
// Encode
public static String encodeString(String text)
throws UnsupportedEncodingException {
byte[] bytes = text.getBytes("UTF-8");
String encodeString = Base64.encode(bytes);
return encodeString;
}
// Giải mã hóa một đoạn text (Đã mã hóa trước đó).
// 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);
}
}
Kết quả chạy ví dụ:
