*本事例主要讲了如下几点:
* 1:将图片转换为BASE64加密字符串.
* 2:将图片流转换为BASE64加密字符串.
* 3:将BASE64加密字符串转换为图片.
* 4:在jsp文件中以引用的方式和BASE64加密字符串方式展示图片.
首先看工具类:
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* @author IluckySi
* @since 20150122
*/
public class ImageUtil {
private static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
private static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
/**
* 将图片转换为BASE64加密字符串.
* @param imagePath 图片路径.
* @param format 图片格式.
* @return
*/
public String convertImageToByte(String imagePath, String format) {
File file = new File(imagePath);
BufferedImage bi = null;
ByteArrayOutputStream baos = null;
String result = null;
try {
bi = ImageIO.read(file);
baos = new ByteArrayOutputStream();
ImageIO.write(bi, format == null ? "jpg" : format, baos);
byte[] bytes = baos.toByteArray();
result = encoder.encodeBuffer(bytes).trim();
System.out.println("将图片转换为BASE64加密字符串成功!");
} catch (IOException e) {
System.out.println("将图片转换为 BASE64加密字符串失败: " + e);
} finally {
try {
if(baos != null) {
baos.close();
baos = null;
}
} catch (Exception e) {
System.out.println("关闭文件流发生异常: " + e);
}
}
return result;
}
/**
* 将图片流转换为BASE64加密字符串.
* @param imageInputStream
* @param format 图片格式.
* @return
*/
public String convertImageStreamToByte(InputStream imageInputStream, String format) {
BufferedImage bi = null;
ByteArrayOutputStream baos = null;
String result = null;
try {
bi = ImageIO.read(imageInputStream);
baos = new ByteArrayOutputStream();
ImageIO.write(bi, format == null ? "jpg" : format, baos);
byte[] bytes = baos.toByteArray();
result = encoder.encodeBuffer(bytes).trim();
System.out.println("将图片流转换为BASE64加密字符串成功!");
} catch (IOException e) {
System.out.println("将图片流转换为 BASE64加密字符串失败: " + e);
} finally {
try {
if(baos != null) {
baos.close();
baos = null;
}
} catch (Exception e) {
System.out.println("关闭文件流发生异常: " + e);
}
}
return result;
}
/**
* 将BASE64加密字符串转换为图片.
* @param base64String
* @param imagePath 图片生成路径.
* @param format 图片格式.
*/
public void convertByteToImage(String base64String, String imagePath, String format) {
byte[] bytes = null;
ByteArrayInputStream bais = null;
BufferedImage bi = null;
File file = null;
try {
bytes = decoder.decodeBuffer(base64String);
bais = new ByteArrayInputStream(bytes);
bi = ImageIO.read(bais);
file = new File(imagePath);
ImageIO.write(bi, format == null ? "jpg" : format, file);
System.out.println("将BASE64加密字符串转换为图片成功!");
} catch (IOException e) {
System.out.println("将BASE64加密字符串转换为图片失败: " + e);
} finally {
try {
if(bais != null) {
bais.close();
bais = null;
}
} catch (Exception e) {
System.out.println("关闭文件流发生异常: " + e);
}
}
}
}
然后看测试类:
import java.io.InputStream;
import com.ilucky.util.image.ImageUtil;
/**
* 本事例主要讲了如下几点:
* 1:将图片转换为BASE64加密字符串.
* 2:将图片流转换为BASE64加密字符串.
* 3:将BASE64加密字符串转换为图片.
* 4:在jsp文件中以引用的方式和BASE64加密字符串方式展示图片.
* @author IluckySi
* @since 20150122
*/
public class MainTest {
public static void main(String[] args) {
ImageUtil imageUtil = new ImageUtil();
String base64String = imageUtil.convertImageToByte("D:\\test.png", "png");
System.out.println(base64String);
imageUtil.convertByteToImage(base64String, "D:\\test2.png", "png");
InputStream is = MainTest.class.getResourceAsStream("test.png");
String base64String2 = imageUtil.convertImageStreamToByte(is, "png");
System.out.println(base64String2);
imageUtil.convertByteToImage(base64String2, "D:\\test3.png", "png");
}
}
最后看jsp文件:
index.html