Domanda

Io uso javax.mail.internet.mimebody * Versione 1.4.1

Il mio programma vuole inviare alcuni dati binari con più livelli di nidificazione da un server al client utilizzando MimemultiPart. Ho osservato che se su un livello se usiamo getcontent che corrompe i dati. Sono stato in grado di riprodurre questo problema con questo snippet

  public static void CreateResponse() throws Exception {
        //Simulate the Server side
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MimeMultipart multiPartValues = new MimeMultipart();
        MimeBodyPart valueBody = new MimeBodyPart();

        byte[] firstKeyValue = new byte[] { (byte)0x8c};
        valueBody.setContent(firstKeyValue,"application/octet-stream");
        valueBody.addHeader(RestMessageHeaders.CONTENT_LENGTH,
                Integer.toString(firstKeyValue.length));
        multiPartValues.addBodyPart(valueBody);

        Object input = valueBody.getContent();
        System.out.println(String.format("input %02X", ((byte[])input)[0]));
        multiPartValues.writeTo(outputStream);

        //Simulate the client side
        byte[] mimeOutput = outputStream.toByteArray();

        ByteArrayDataSource ds = new ByteArrayDataSource(mimeOutput,
                "multipart/mixed");
        MimeMultipart mp = new MimeMultipart(ds);
        MimeBodyPart part = (MimeBodyPart) mp.getBodyPart(0);

        byte[] myOutput = new byte[1];
        //Verified that getContent returns a String, why ?? 
        Object output = part.getContent();
        System.out.println("getContent type " + output.getClass());
        String result = (String)output;
        ByteArrayDataSource partDS = new ByteArrayDataSource(result, "multipart/mixed");
        partDS.getInputStream().read(myOutput); 
        System.out.println(String.format("getContent %02X %02X", result.getBytes()[0],result.getBytes()[1]));
        System.out.println(String.format("getContent %02X", myOutput[0]));

        part.getInputStream().read(myOutput);
        System.out.println(String.format("getInputStream %02X", myOutput[0]));

        part.getRawInputStream().read(myOutput);
        System.out.println(String.format("getRawInputStream %02X", myOutput[0]));

}
.

Ecco l'output

        input 8C
        getContent type class java.lang.String
        getContent C2 8C
        getContent C2
        getInputStream 8C
        getRawInputStream 8C
.

Ho completamente semplificato il codice qui e sembra ovvio da usare Get (RAW) Inputstream, ma abbiamo nididmultibart e il livello superiore stava facendo getcontent che lo ha fatto fallire per alcuni casi.

    .
  1. L'ingresso è di tipo Byte Array, ma sul client Getcontent risponde con la stringa. Il server imposta il contenuto su applicazione / ottet-stream ma sul lato client viene emesso come stringa. Cosa sta andando male qui?
  2. Non sono sicuro del perché il byte c2 viene aggiunto prima dell'8c. Cosa è così speciale del personaggio 8c?
  3. Qual è la differenza tra GetInputStream e GetrawInputStream. Quando usare uno sopra l'altro?
È stato utile?

Soluzione

Che cosa consente il flusso completo che il server sta creando e il client sta leggendo?

Si noti che usando MimemultiPart senza mimemessage, ti manca alcune delle cose che vengono eseguite automaticamente per te da Mimemessage, in particolare ti manca la chiamata a mimemultipart.updateheader ().Poiché il metodo è protetto, è necessario per sottoclasse mimemultipart e chiamare quel metodo prima di chiamare writeto.Se ciò non risolve il problema, mostraci i dati esatti che vengono scritti e letti sul flusso.

Come menzionato sopra, se ti aspettano dati binari, si desidera certamente usare GetInputStream.GetrawInputStream offre i dati prima che sia stato decodificato, ad es. L'ingresso base64 anziché l'output binario.

Altri suggerimenti

Non utilizzare Getcontent () se restituisce una stringa e si desidera binario.La stringa non è un contenitore per i dati binari.Utilizzare GetInputStream () e copia i byte.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top