方式一:只能对本地图片进行转码
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.codec.binary.Base64;
public class Base64Util {
public static void main(String[] args) {
String imgPath = "D:\\ai_02.jpg";//本地图片路径
String basePath= getImg(imgPath);//对图片进行转码
String suffix = imgPath.substring(imgPath.lastIndexOf(".") + 1);
System.out.println("data:image/"+suffix+";base64,"+basePath);
}
public static String getImg(String imgPath) {
//将图片装换为字节数组字符串,并对其进行base64编码处理
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
}catch(IOException e) {
e.printStackTrace();
}
return new String(Base64.encodeBase64(data));
}
}
方式二:当图片在服务器上需要跨域或者进行反向代理时,使用此种方法。
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImgBase64Util {
/**通过Toolkit的方式进行图片转换 注意: 通过Toolkit转base64后,偶尔会出现展示不完整的问题
* 通过图片的url获取图片的base64字符串
* @param imgUrl 图片url
* @return 返回图片base64的字符串
*/
public static String Base64ImgEncod(String url){
String suffix = url.substring(url.lastIndexOf(".") + 1);
String base64Str = null;
try {
URL urls = new URL(url);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Image image = Toolkit.getDefaultToolkit().getImage(urls);
System.out.println("image:"+image);
if(image == null){
return null;
}
BufferedImage biOut = toBufferedImage(image);
ImageIO.write(biOut, suffix, baos);
base64Str = Base64Util.encode(baos.toByteArray());
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e);
}
return base64Str;
}
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null),
image.getHeight(null), transparency);
} catch (HeadlessException e) {
System.out.println(e);
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
/**通过二进制流的方式进行图片转换(推荐使用)
* 通过图片的url获取图片的base64字符串
* @param imgUrl 图片url
* @return 返回图片base64的字符串
*/
public static String image2Base64(String imgUrl) {
URL url = null;
InputStream is = null;
ByteArrayOutputStream outStream = null;
HttpURLConnection httpUrl = null;
try{
url = new URL(imgUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
httpUrl.getInputStream();
is = httpUrl.getInputStream();
outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while( (len=is.read(buffer)) != -1 ){
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
// 对字节数组Base64编码
return Base64Util.encode(outStream.toByteArray());
}catch (Exception e) {
e.printStackTrace();
}
finally{
if(is != null) {
try {
is.close();
} catch (IOException e) {
System.out.println(e);
}
}
if(outStream != null){
try {
outStream.close();
} catch (IOException e) {
System.out.println(e);
}
}
if(httpUrl != null){
httpUrl.disconnect();
}
}
return imgUrl;
}
}