Encoding as Base64 in Java

前端 未结 17 2476
失恋的感觉
失恋的感觉 2020-11-21 13:44

I need to encode some data in the Base64 encoding in Java. How do I do that? What is the name of the class that provides a Base64 encoder?


I tried to use the <

17条回答
  •  既然无缘
    2020-11-21 14:03

    To convert this, you need an encoder & decoder which you will get from Base64Coder - an open-source Base64 encoder/decoder in Java. It is file Base64Coder.java you will need.

    Now to access this class as per your requirement you will need the class below:

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.InputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class Base64 {
    
        public static void main(String args[]) throws IOException {
            /*
             * if (args.length != 2) {
             *     System.out.println(
             *         "Command line parameters: inputFileName outputFileName");
             *     System.exit(9);
             * } encodeFile(args[0], args[1]);
             */
            File sourceImage = new File("back3.png");
            File sourceImage64 = new File("back3.txt");
            File destImage = new File("back4.png");
            encodeFile(sourceImage, sourceImage64);
            decodeFile(sourceImage64, destImage);
        }
    
        private static void encodeFile(File inputFile, File outputFile) throws IOException {
            BufferedInputStream in = null;
            BufferedWriter out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(inputFile));
                out = new BufferedWriter(new FileWriter(outputFile));
                encodeStream(in, out);
                out.flush();
            }
            finally {
                if (in != null)
                    in.close();
                if (out != null)
                    out.close();
            }
        }
    
        private static void encodeStream(InputStream in, BufferedWriter out) throws IOException {
            int lineLength = 72;
            byte[] buf = new byte[lineLength / 4 * 3];
            while (true) {
                int len = in.read(buf);
                if (len <= 0)
                    break;
                out.write(Base64Coder.encode(buf, 0, len));
                out.newLine();
            }
        }
    
        static String encodeArray(byte[] in) throws IOException {
            StringBuffer out = new StringBuffer();
            out.append(Base64Coder.encode(in, 0, in.length));
            return out.toString();
        }
    
        static byte[] decodeArray(String in) throws IOException {
            byte[] buf = Base64Coder.decodeLines(in);
            return buf;
        }
    
        private static void decodeFile(File inputFile, File outputFile) throws IOException {
            BufferedReader in = null;
            BufferedOutputStream out = null;
            try {
                in = new BufferedReader(new FileReader(inputFile));
                out = new BufferedOutputStream(new FileOutputStream(outputFile));
                decodeStream(in, out);
                out.flush();
            }
            finally {
                if (in != null)
                    in.close();
                if (out != null)
                    out.close();
            }
        }
    
        private static void decodeStream(BufferedReader in, OutputStream out) throws IOException {
            while (true) {
                String s = in.readLine();
                if (s == null)
                    break;
                byte[] buf = Base64Coder.decodeLines(s);
                out.write(buf);
            }
        }
    }
    

    In Android you can convert your bitmap to Base64 for Uploading to a server or web service.

    Bitmap bmImage = //Data
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageData = baos.toByteArray();
    String encodedImage = Base64.encodeArray(imageData);
    

    This “encodedImage” is text representation of your image. You can use this for either uploading purpose or for diplaying directly into an HTML page as below (reference):

    
    
    

    Documentation: http://dwij.co.in/java-base64-image-encoder

提交回复
热议问题