4

I have tried using the below code which generates a base64 for the file and returns as string. I was able to get if the file size is small.

StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader = 
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                        String line = "";           
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

If there is any other way to get the base64 of the file.The command I am passing is base64 filename. please let me know

Kotesh Malempati
  • 103
  • 2
  • 2
  • 9
  • BTW, what is `command`? – Amadan Aug 27 '14 at 05:17
  • you need to tag the language you want – Keith Nicholas Aug 27 '14 at 05:18
  • Even i have you the MIMEBase64 method from [link](http://www.rgagnon.com/javadetails/java-0083.html) I was able to generate for smaller file( i tried for .xlsx 16kb, .txt 3kb) I was failing for files .jar 480KB .pdf 3MB. Its taking like eternity for giving the string – Kotesh Malempati Aug 27 '14 at 05:20
  • @Amadan its base64 filename. – Kotesh Malempati Aug 27 '14 at 05:23
  • @KeithNicholas its java. tq i taged the language – Kotesh Malempati Aug 27 '14 at 05:24
  • Whose base64 filename? And why are you executing it? And there is no mention of base64 anywhere in code... I have no idea what you are trying to do. – Amadan Aug 27 '14 at 05:25
  • @Amadan In linux we use base64 to get the base64 of the file. so i used the above mentioned java code to execute the command in console and get the output. Apart from this i tried using the algo in method MIMEBase64 [in this link](http://www.rgagnon.com/javadetails/java-0083.html). I am trying to append the base64 string in order to add an attachment to the email. I was able to get the output if the size is small. – Kotesh Malempati Aug 27 '14 at 05:34
  • Java 8 consolidated its Base64 classes in http://stackoverflow.com/questions/469695/decode-base64-data-in-java. Before java 8 see also http://stackoverflow.com/questions/469695/decode-base64-data-in-java – Joop Eggen Aug 27 '14 at 05:34
  • I know what `base64` utility is. You wrote "its base64 filename", not "it's" or "it is", which would have allowed me to understand what you are talking about. I thought it `command` was a filename of your data that was encoded in Base64, which made no sense. Anyway, as icza says, you don't need to launch external processes to encode to Base64. – Amadan Aug 27 '14 at 05:37

2 Answers2

5

You don't need an external program for this, Java has built-in Base64 encoding/decoding capabilities.

This is all it takes:

String base64 = DatatypeConverter.printBase64Binary(Files.readAllBytes(
    Paths.get("path/to/file")));

Edit:

If you're using Java 6, Files and Paths are not availabe (they were added in Java 7.0). Here is a Java 6 compatible solution:

File f = new File("path/to/file");
byte[] content = new byte[(int) f.length()];
InputStream in = null;
try {
    in = new FileInputStream(f);
    for (int off = 0, read;
        (read = in.read(content, off, content.length - off)) > 0;
        off += read);

    String base64 = DatatypeConverter.printBase64Binary(content);
} catch (IOException e) {
    // Some error occured
} finally {
    if (in != null)
        try { in.close(); } catch (IOException e) {}
}
icza
  • 389,944
  • 63
  • 907
  • 827
0

Add the following:

    String result = Base64.encode(output.toString().getBytes());
Andrey E
  • 856
  • 8
  • 18