Decode Base64 data in Java

前端 未结 20 1459
时光说笑
时光说笑 2020-11-21 06:04

I have an image that is Base64 encoded. What is the best way to decode that in Java? Hopefully using only the libraries included with Sun Java 6.

20条回答
  •  孤独总比滥情好
    2020-11-21 06:42

    You can write or download file from encoded Base64 string:

    Base64 base64 = new Base64(); 
    String encodedFile="JVBERi0xLjUKJeLjz9MKMSAwIG9iago8PCAKICAgL1R5cGUgL0NhdGFsb2cKICAgL1BhZ2VzIDIgMCBSCiAgIC9QYWdlTGF5b3V0IC9TaW5"; 
                  byte[] dd=encodedFile.getBytes();
                byte[] bytes = Base64.decodeBase64(dd);
    
     response.setHeader("Content-disposition", "attachment; filename=\""+filename+"\"");
                response.setHeader("Cache-Control", "no-cache");
                response.setHeader("Expires", "-1");
    
                // actually send result bytes
                response.getOutputStream().write(bytes);
    

    Worked for me and hopefully for you also...

提交回复
热议问题