2016-11-30 3 views
-1

Ich versuche (In Java), um ein Dateidokument dann in BitArray danach zu einem String mit ähnlicher Darstellung, dann zurück zu konvertieren Original-Bit-Array und schließlich zum ursprünglichen Abschlussdokument.Konvertieren Sie Datei in Byte - Array, dann zu String dann zu Byte - Array dann zu der ursprünglichen Datei

Hier ist mein Code, aber die erzeugte Datei ist in diesem Fall das Bild nicht sichtbar.

try { 
     File file = new File("C:/Users/dkimigho/Downloads/kenyapowerlogo.jpg"); 

     FileInputStream fis = null; 
     try { 
      fis = new FileInputStream(file); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     //create FileInputStream which obtains input bytes from a file in a file system 
     //FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. 

     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     byte[] buf = new byte[1024]; 
     try { 
      for (int readNum; (readNum = fis.read(buf)) != -1;) { 
       //Writes to this byte array output stream 
       bos.write(buf, 0, readNum); 
       System.out.println("read " + readNum + " bytes,"); 
      } 
     } catch (IOException ex) { 
      Logger.getLogger(ARRAYBITStoPDF.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     byte[] bytes = bos.toByteArray(); 

     System.out.println("byte1"); 
     for (int i = 0; i < bytes.length; i++) { 
      System.out.print(bytes[i]); 
     } 
     //We have the bytes now convert to String 
     String stringbytearray=new String(bytes); 

     System.out.println("stringbytearray: "+stringbytearray); 

     //We have the bytes now convert to String 

     byte[] content = stringbytearray.getBytes(); 
     System.out.println("byte2"); 
     for (int i = 0; i < content.length; i++) { 
      System.out.print(content[i]); 
     } 

     int size = bytes.length; 
     InputStream isfilecontent = null; 
     byte[] b = new byte[size]; 
     isfilecontent = new ByteArrayInputStream(content); 

     //writing the downloaded data into a PDF file 
     FileOutputStream fileOutputpdf = new FileOutputStream("C:/Users/dkimigho/Downloads/mykenyapowerlogo.jpg"); 

     /* use binary I/O to prevent line based operation messing with the encoding.*/ 
     byte[] buf2 = new byte[2048]; 
     int b_read = 0; 
     while ((b_read = isfilecontent.read(buf2)) > 0) { 
      fileOutputpdf.write(buf2, 0, b_read); 
     } 
     fileOutputpdf.flush(); 
     //closed the output stream 
     fileOutputpdf.close(); 

    } catch (IOException e) { 
     // handle IOException 
     System.out.println(e.getMessage()); 
    } 

Irgendwelche Hilfe auf das Zeigen, was ich falsch mache? Eine Korrektur meines Codes zu einem Arbeitscode kann wichtig sein.

+0

Irgendwelche Hilfe, die zeigt, was nicht funktioniert? Sie sollten über Methode pro Funktionality verfügen. Dies wäre einfacher zu testen, was funktioniert und was nicht. Und Sie sollten mit einer einfachen Textdatei beginnen ... ein JPG ist komplexer, um den Wert Schritt für Schritt zu überprüfen. – AxelH

+0

Java doc: 'String (byte [] bytes) Konstruiert einen neuen String durch ** Dekodieren ** des angegebenen Array von Bytes ** unter Verwendung des Standard-Zeichensatzes der Plattform **.' Ich denke, das ändert Ihren Inhalt – Fefux

+1

Konvertieren von Bytes in einen String korrumpiert/verändert sie. Sie können Bytes in einer Zeichenfolge in C speichern, dies ist jedoch in Java nicht möglich. In Java speichern Sie Bytes in einem Byte-Array oder [ByteBuffer] (http://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html). Benutze hier keine Strings. – VGR

Antwort

0

Ich fand eine Antwort man muss JAVA 8 java.util.Base64 verwenden, um die Bytes zu codieren und zu dekodieren, ohne Informationen über das Dokument zu verlieren. Ich hoffe, dass es jemandem helfen wird.

/* 
    * 1. How to convert an image file to byte array? 
    */ 
    try { 
     File file = new File("C:/Users/qwerty/Downloads/factura.pdf"); 

     FileInputStream fis = null; 
     try { 
      fis = new FileInputStream(file); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     //create FileInputStream which obtains input bytes from a file in a file system 
     //FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. 

     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     byte[] buf = new byte[1024]; 
     try { 
      for (int readNum; (readNum = fis.read(buf)) != -1;) { 
       //Writes to this byte array output stream 
       bos.write(buf, 0, readNum); 
       // System.out.println("read " + readNum + " bytes,"); 
      } 
     } catch (IOException ex) { 
      Logger.getLogger(ARRAYBITStoPDF.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     byte[] bytes = bos.toByteArray(); 
     bos.close(); // should be inside a finally block 
     //We have the bytes now convert to String 

     // ENCODING 
     String encodedDoc= Base64.getEncoder().encodeToString(bytes); 

     System.out.println(encodedDoc); 

     // DECODING 
     int size = bytes.length; 
     InputStream isfilecontent = null; 
     //byte[] b = new byte[size]; 
     isfilecontent = new ByteArrayInputStream(Base64.getDecoder().decode(encodedDoc)); 

     //writing the downloaded data into a PDF file 
     FileOutputStream fileOutputpdf = new FileOutputStream("C:/Users/qwerty/Downloads/myfactura.pdf"); 

     /* use binary I/O to prevent line based operation messing with the encoding.*/ 
     byte[] buf2 = new byte[2048]; 
     int b_read = 0; 
     while ((b_read = isfilecontent.read(buf2)) > 0) { 
      fileOutputpdf.write(buf2, 0, b_read); 
     } 
     fileOutputpdf.flush(); 
     //closed the output stream 
     fileOutputpdf.close(); 

    } catch (IOException e) { 
     // handle IOException 
     System.out.println(e.getMessage()); 
    } 
Verwandte Themen