2016-01-20 93 views
1

我有一个hl7文件,其中包含从编码的PDF派生的base64字符串。是否可以从base64字符串创建一个pdf文件?

是否有可能从该base64重新创建PDF?

pdf to base64 --> ok 
---------------------------- 
base64 to pdf --> is this possible? 
+4

简短的回答是肯定的,那是一种BASE64的角度,转换来自文本的二进制数据 – MadProgrammer

回答

1

这是可能的。你可以使用sun.misc.BASE64Decoder。

例子:

import java.io.*; 

import sun.misc.BASE64Decoder; 
/** 
* Kax7ux 
* 
*/ 
public class App 
{ 
    public static void main(String[] args) 
    {  
     String encodedBytes = "yourStringBase64"; 
     try { 
      BASE64Decoder decoder = new BASE64Decoder(); 
      byte[] decodedBytes; 
      FileOutputStream fop; 
      decodedBytes = new BASE64Decoder().decodeBuffer(encodedBytes); 
      File file = new File("path/file.pdf"); 
      fop = new FileOutputStream(file); 

      fop.write(decodedBytes); 

      fop.flush(); 
      fop.close(); 
      System.out.println("Created"); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 
0

@Clem你可以很容易使用来自的Base64类java.util中

import java.util.Base64; 

    public class App 
    { 
     public static void main(String[] args) 
     { 
      String pdfAsArrayByte = "JVBERi/8KNyAwIG9iago8PAovVHlwZS...."; 

      // Decode the Base64 arrayByte to PDF file 
      DataSource source = new ByteArrayDataSource(Base64.getDecoder().decode(pdfAsArrayByte),"application/pdf"); 

      // The ,source, instance is now a true PDF file 
     } 
    } 
相关问题