Base64 Encoding in Java

2018-12-31 03:33发布

I'm using Eclipse. I have the following line of code:

wr.write(new sun.misc.BASE64Encoder().encode(buf));

Eclipse marks this line as an error. I imported the required libraries:

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

But again, both of them are shown as errors. I found a similar post here.

I used Apache Commons as the solution suggested by including:

import org.apache.commons.*;

and importing the JAR files downloaded from: http://commons.apache.org/codec/

But the problem still exists. Eclipse still shows the errors previously mentioned; please advise.

标签: java base64
16条回答
爱死公子算了
2楼-- · 2018-12-31 04:07

Use Java 8's never-too-late-to-join-in-the-fun class: java.util.Base64

查看更多
裙下三千臣
3楼-- · 2018-12-31 04:07

To convert this you need Encoder & Decoder which you will get from http://www.source-code.biz/base64coder/java/. It is File Base64Coder.java you will need.

Now to access this class as per your requirement you will need 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 Server/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 HTML Page as below (Reference):

<img alt="" src="data:image/png;base64,<?php echo $encodedImage; ?>" width="100px" />
<img alt="" src="data:image/png;base64,/9j/4AAQ...........1f/9k=" width="100px" />

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

查看更多
余欢
4楼-- · 2018-12-31 04:12

In Java 7 I coded this method

import javax.xml.bind.DatatypeConverter;

public static String toBase64(String data) {
    return DatatypeConverter.printBase64Binary(data.getBytes());
}
查看更多
弹指情弦暗扣
5楼-- · 2018-12-31 04:14

apache commons has nice implementation of base64. you can do this as simply as

// encrypt data on your side using BASE64
byte[]   bytesEncoded = Base64.encodeBase64(str .getBytes());
System.out.println("ecncoded value is " + new String(bytesEncoded ));

// Decrypt data on other side, by processing encoded data
byte[] valueDecoded= Base64.decodeBase64(bytesEncoded );
System.out.println("Decoded value is " + new String(valueDecoded));

you can find more details about base64 encoding at http://faisalbhagat.blogspot.com/2014/06/base64-encoding-using-java-and.html

查看更多
登录 后发表回答