Base64 Decoding issue with Arabic content in java

807 Views Asked by At

I have a spring boot application (2.2.6.RELEASE). In my application I need to decode a base64 string into a xml data. This xml data contains Arabic contents. I am using "apache common" (version 4.5.6) to decode the base64 string. It is successfully decoded to Arabic content while am running the application in the Intellij IDE. But when I am running the final jar build through the command line , it didn't able to decoded the Arabic content from base64. It showing some unreadable characters. OpenJDK 14 is used in this project and Intellij version is 2021.2 COMMUNITY EDITION.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test.app.service</groupId>
    <artifactId>Sample-Service-Application</artifactId>
    <version>1.0.0</version>
    <name>test-app-service</name>
    <properties>
        <java.version>14</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </dependency>
  
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>demoService</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Java File

String data= "Base64StringWithArabicContent"
byte[] valueDecoded = org.apache.commons.codec.binary.Base64.decodeBase64(data.getBytes(StandardCharsets.UTF_8));
String encodedDocumentString = new String(valueDecoded);
1

There are 1 best solutions below

0
Vishnu Viswambharan On

I simply encoded string with "UTF-8". Now it is working in the jar file as well.

String data= "Base64StringWithArabicContent"
byte[] valueDecoded = org.apache.commons.codec.binary.Base64.decodeBase64(data.getBytes(StandardCharsets.UTF_8));
String encodedDocumentString = new String(valueDecoded,"UTF-8");