1. 对图片文件进行编码
import org.apache.commons.codec.binary.Base64;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ImageBase64Utils {
public static String getImgBase(String imgFile) {
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return Base64.encodeBase64String(data);
}
}
2. 也可直接传入byte[]进行编码,效率更高
byte[] data = XXXXXX;
String s = Base64.encodeBase64String(data);