将 base64 字符串从 xml 文件复制到文本文件时出现问题

Problems copying base64 string from xml file into text file

我有一个 xml 文件,其标签包含 base64 字符串,如下所示:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <PercezioniEStorni>
           <MsgPercezioniStorni>  
               <ApplicazioneChiamante>PEI</ApplicazioneChiamante> 
               <DataElaborazione>31/03/2016</DataElaborazione> 
               <CodiceSede>3700203</CodiceSede> 
               <Esercizio>2015</Esercizio>
               <Trimestre>4</Trimestre>
               <PeriodoDal>01/10/2015</PeriodoDal>
               <PeriodoAl>31/12/2015</PeriodoAl>
               <DataValidazione>31/03/2016</DataValidazione>             
               <UtenteValidatore>giuseppe</UtenteValidatore>
               <CodFiscFirmatario>VGRGPP79E08E919T</CodFiscFirmatario>  
               <PercezioniEStorniTypeZip>          
                   <!-- Base64 example String -->
                   VUVzREJCUUFDQUFJQUNOcWYwZ0FBQUFBQUFBQUFBQUFBQUFaQUFBQWNtVm5hWE4wY204dE1Ua3lMVEl3TVRZeE1TMHhMbmh0Yk96ZGE1UGt4blh1KzYrUzRUZDh4   V0YzOWQzQm80aFZCVlFqdTNBWkpnRHlWTDFqeUF5WkVUS3BFR2w3Ny9QcFQ2Sm1lTEZrdTFkM3I4UWdoWC9FRG0xcWhxWmt6eTlYTCtTVEsvUExuMzc4OWkvZi9mQWYvMX    rOEIvZi9mbkh2M3puL3MrLy9mbUhuLzc1NHkvL1AvLzByei8vL0pkLy91S0xuLzc0cjkvOTI3Yy92WXUvTy8zV3V4Ly8rcWN2cHIvNDRydVAvM05mL05QSC84bi84OU8vL1BvLzlaLy8rWi92L3ZQcS9=
               </PercezioniEStorniTypeZip>
           </MsgPercezioniStorni>
       </PercezioniEStorni>
    </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

Base64字符串编码一个zip文件(只是一个例子,并不真正包含一个zip文件,真正的字符串太长了)。 我通过 JAXB 创建了这个 xml 以这种方式生成 类:

    FileInputStream zipFis = new FileInputStream(fileZip);
    buffer = new byte[(new Long(fileZip.length())).intValue()];
    zipFis.read(buffer);
    zipFis.close();
    String encoded = Base64.encode(buffer);

    PercezioniEStorni percezioniStorni = new PercezioniEStorni();
    /** ...set other properties... **/
    MsgPercezioniStorni msgPercezioniStorni = new MsgPercezioniStorni();
    /** ...set other properties... **/
    msgPercezioniStorni.setPercezioniEStorniTypeZip(encoded.getBytes());
    percezioniStorni.setMsgPercezioniStorni(msgPercezioniStorni);

    JAXBContext jaxbContext = JAXBContext.newInstance(PercezioniEStorni.class,percezioniStorni);
    Marshaller marshaller = jaxbContext.createMarshaller();
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    marshaller.marshal(element, document);
    MessageFactory mf = MessageFactory.newInstance();
    SOAPMessage message = mf.createMessage();
    message.getSOAPBody().addDocument(document);

    File fileXml = new File(xmlPath);
    file.createNewFile();
    FileOutputStream fileOutput = new FileOutputStream(fileXml);
    message.writeTo(fileOutput);
    fileOutput.close(); 

然后我反转了这个过程:

    File file = new File(xmlPath);
    FileInputStream fis = new FileInputStream(file);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
    String xml = "";
    while(br.ready()){
        xml += br.readLine();
    }
    br.close();

    MessageFactory mf = MessageFactory.newInstance();
    SOAPMessage message = mf.createMessage();
    SOAPPart soapPart = message.getSOAPPart();

    InputSource is = new InputSource();
    is.setByteStream(new ByteArrayInputStream(xml.getBytes("UTF-8")));

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    DocumentBuilder builder = dbFactory.newDocumentBuilder();
    Document document = builder.parse(is);
    DOMSource domSource = new DOMSource(document);

    soapPart.setContent(domSource);
    message.saveChanges();
    PercezioniEStorni perc = SOAPUtil.unmarshal(PercezioniEStorni.class,message);
    byte[] decoded = Base64.decode(new String(perc.getMsgPercezioniStorni().getPercezioniEStorniTypeZip()));
    File zipFile = new File(zipPath);
    zipFile.createNewFile();
    FileOutputStream out = new FileOutputStream(zipFile);
    out.write(decoded);
    out.close();

一切正常。压缩包解码成功,可以解压了

后来我手动将 xml 文件中的 Base64 字符串复制到另一个文本文件中。 我以这种方式从 java 读取这个文件:

    File txtFile = new File(textFilePath);
    FileInputStream fis = new FileInputStream(txtFile);
    Reader r = new InputStreamReader(fis,"UTF-8");
    StringBuilder sb = new StringBuilder();
    int buffer = 0;
    while ((buffer= r.read())!=-1) {
        sb.append((char)buffer);
    }
    fis.close();
    File zipFile = new File(zipPath);
    zipFile.createNewFile();
    FileOutputStream out = new FileOutputStream(zipFile);
    Base64.decode(sb.toString(),out);
    out.close();

这次 zip 存档已损坏。大小也不一样。 为什么?有没有办法从另一个文件中读取相同的 Base64 字符串?

除非我忽略了一些事情:您正在手动编码从 zip 文件中读取的一些数据:

String encoded = Base64.encode(buffer);

然后你设置一个属性,它被定义为在Base64中存储一个字节数组,以避免任何破坏XML规则的事情:

msgPercezioniStorni.setPercezioniEStorniTypeZip(encoded.getBytes());

现在编码字符串encoded的字节被再次编码。

难怪 XML 中的字符无法通过单个解码步骤制成有效的 zip。 (尝试两个。)

好多了:放弃第一个编码步骤。