2016-04-01 3 views
0

にxmlファイルからbase64文字列をコピーし、私はこのように、base64文字列が含まれているタグを使用してXMLファイルを持っている:問題テキストファイル

<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文字列を読み取る方法はありますか?

+0

base64でエンコードされたブロックの行末を、(readLineを使用して)削除するのではなく、そのままにしてください。 – Berger

+0

"Base64文字列はzipファイルをエンコードします"というテキストエンコード(Base64)のテキストエンコード(XML)がありますか?それは悪い考えのように思える。 – Raedwald

+0

@ Raedwald ""タグ内の文字列は、zipファイルのbase64エンコーディングです。 –

答えて

1

私は何かを見落としてきた場合を除き:手動でzipファイルから読み込まれ、いくつかのデータを符号化されています

String encoded = Base64.encode(buffer); 

次にあなたが壊す何かを避けるために、Base64にバイト配列を格納するために定義されたプロパティを設定しますXMLのルール:

msgPercezioniStorni.setPercezioniEStorniTypeZip(encoded.getBytes()); 

今エンコードされた文字列encodedのバイトが再度エンコードされています。

XMLの文字を1回の復号化ステップで有効なzipにすることはできません。 (2回試してみてください)

非常に良い:最初のエンコードステップを削除します。

+0

あなたは正しいです!スキーマファイルでは、その要素の型は 'type =" xsd:base64Binary "'です。これは文字列が手作業で1つ、スタブ付きクラスの設定者によって2回エンコードされていることを意味します。 ありがとう –

関連する問題