2

Am getting base64 encoded data as String format. Am trying to decode the base64 and want to download as a file. I have commented the below few lines of code, where am getting error out of those line.

Am not sure how to decode the data.

String contentByte=null;
for (SearchHit contenthit : contentSearchHits) {

    Map<String, Object> sourceAsMap = contenthit.getSourceAsMap();
    fileName=sourceAsMap.get("Name").toString();
    System.out.println("FileName ::::"+fileName);
    contentByte =  sourceAsMap.get("resume").toString();

}
System.out.println("Bytes --->"+contentByte);

 File file = File.createTempFile("Testing",".pdf", new File("D:/") );
 file.deleteOnExit();
 BufferedWriter out = new BufferedWriter(new FileWriter(file));
  out.write(Base64.getDecoder().decode(contentByte)); //getting error on this line

Please find the below compilation error am getting.

The method write(int) in the type BufferedWriter is not applicable for the arguments (byte[])

Am using Java 8 version

Karthikeyan
  • 211
  • 1
  • 4
  • 15

2 Answers2

3

Writers are used for writing characters, not bytes. To write bytes, you should use some flavor of OutputStream. See Writer or OutputStream?

But if all you want is to write a byte array to a file, Files class provides a Files.write method that does just that:

byte[] bytes = Base64.getDecoder().decode(contentByte);
Files.write(file.toPath(), bytes);
Misha
  • 27,433
  • 6
  • 62
  • 78
0
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Example {

public static void main(String[] args) {

    String contentByte="Simple text send from server";
    byte[] bytes = 
    Base64.getEncoder().encode(contentByte.getBytes(StandardCharsets.UTF_8));
    //Data received by you at server end(base64 encoded data as string)
    contentByte = new String(bytes);

    System.out.println(new String(bytes));

    BufferedWriter out = null;
    System.out.println("Bytes --->"+contentByte);
    try {
        File file = File.createTempFile("Testing",".pdf", new File("/tmp/") );
       // file.deleteOnExit(); // this line will remove file and your data will not going to save to file. So remove this line.
        out = new BufferedWriter(new FileWriter(file));
        byte[] decodedImg = 
      Base64.getDecoder().decode(contentByte.getBytes(StandardCharsets.UTF_8));
        out.write(new String(decodedImg)); //getting error on this line

    }catch (Exception e)
    {
        e.printStackTrace();
    }finally {
        if(out!=null)
        {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
   }
 }

Might above solution with help you.

  • You should learn about the [`try`-with-resources statement](https://docs.oracle.com/javase/8/docs/technotes/guides/language/try-with-resources.html)… – Holger Jun 24 '18 at 12:27