方法一、 String fileType =".png";
String inputFilePath="C:/Users/zhang/Downloads/html2canvas-0.4.1/build/";
String fileName = System.currentTimeMillis()+".png";
//注意点:实际的图片数据是从 data:image/jpeg;base64, 后开始的
Base64 base64 = new Base64();
String data = request.getParameter("data");
String base64Str = data.substring("data:image/jpeg;base64,".length()-1);
byte[] k = base64.decode(base64Str);
InputStream is = new ByteArrayInputStream(k);
BufferedImage image = ImageIO.read(is);
int newWidth = (int) (image.getWidth() * 1);
int newHeight = (int) (image.getHeight() * 1);
Image newimage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(newimage, 0, 0, null);
g.dispose();
ImageIO.write(tag, "jpg", new File(inputFilePath+fileName));
方法二、 static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
public static void base64StringToImage(String base64String,String path){
try {
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 =ImageIO.read(bais);
File w2 = new File(path);//可以是jpg,png,gif格式
ImageIO.write(bi1, "png", w2);//不管输出什么格式图片,此处不需改动
} catch (IOException e) {
e.printStackTrace();
}
}