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

时间:2016-04-01 12:34:31

标签: java xml jaxb base64 marshalling

我有一个包含base64字符串的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(); 

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

    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();

然后我改变了这个过程:

    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();

一切都很好。存档已成功解码,我可以解压缩。

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

{{1}}

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

1 个答案:

答案 0 :(得分:1)

除非我忽略了某些内容:您手动编码从zip文件中读取的一些数据:

String encoded = Base64.encode(buffer);

然后设置一个属性,该属性被定义为在Base64中存储一个字节数组,以避免任何违反XML规则的行为:

msgPercezioniStorni.setPercezioniEStorniTypeZip(encoded.getBytes());

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

难怪通过单个解码步骤无法将XML中的字符转换为有效的zip。 (试试两个。)

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