简体   繁体   中英

Error encoding files with MIME

I have made a class to encode and decode files for base64. This class works. Im trying to do the same with MIME encoders and decoders but it doesnt´work and i dont know why.

  public void encode64(File input, File output) throws IOException{

    FileInputStream inputStream = new FileInputStream( input);
    FileOutputStream outputStream = new FileOutputStream( output);

    byte buff[] = new byte[300];
    int r = 0;

    while ( ( r = inputStream.read( buff)) > 0 ) {
      byte[] realBuff = Arrays.copyOf( buff, r );
      String b64 = Base64.getEncoder().encodeToString( realBuff);
      outputStream.write( b64.getBytes());
    }

    inputStream.close();
    outputStream.close();

}

public void decode64(File input, File output) throws IOException{

  FileInputStream inputStream = new FileInputStream( input);
  FileOutputStream outputStream = new FileOutputStream( output);

  byte buff[] = new byte[300];
  int r = 0;

  while ( ( r = inputStream.read( buff)) > 0 ) {
    byte[] realBuff = Arrays.copyOf( buff, r );
    outputStream.write( Base64.getDecoder().decode( realBuff));
  }

  inputStream.close();
  outputStream.close();

}

i have change the line from encode64 method

String b64 = Base64.getEncoder().encodeToString( realBuff);

for

String b64 = Base64.getMimeEncoder().encodeToString( realBuff);

and from the line of decode64 method

outputStream.write( Base64.getDecoder().decode( realBuff));

for

outputStream.write( Base64.getMimeDecoder().decode( realBuff));

And my class only works with txt files.

The method "Base64.getMimeEncoder().encodeToString(xxxx)" returns the Encoder that encodes using MIME type base64 encoding scheme while "Base64.getEncoder().encodeToString(xxxx)" uses Basic Type base64 encoding scheme. By default mime is: text/plain. Please refer url: https://en.wikipedia.org/wiki/MIME for further reference

I would try to use the wrap(...) method of Encoder/Decoder, ie

public void encode64(File input, File output) throws IOException {
    FileInputStream inputStream = new FileInputStream(input);
    FileOutputStream outputStream = new FileOutputStream(output);
    OutputStream encodedStream = Base64.getEncoder().wrap(outputStream);

    byte buff[] = new byte[300];
    int r = 0;
    while ((r = inputStream.read(buff)) > 0) {
      byte[] realBuff = Arrays.copyOf(buff, r);
      encodedStream.write(realBuff);
    }

    inputStream.close();
    encodedStream.close();
}

public void decode64(File input, File output) throws IOException {
  FileInputStream inputStream = new FileInputStream(input);
  FileOutputStream outputStream = new FileOutputStream(output);
  OutputStream decodedStream = Base64.getDecoder().wrap(outputStream);

  byte buff[] = new byte[300];
  int r = 0;
  while ((r = inputStream.read(buff)) > 0) {
    byte[] realBuff = Arrays.copyOf(buff, r);
    decodedStream.write(realBuff);
  }

  inputStream.close();
  decodedStream.close();
}

Disclaimer: written out of my head, not tested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM