base64和图片的互转(HTML5的File实现)

base64和图片的互转(HTML5的File实现)
2013-08-02       0  个评论       作者:qklin
收藏     我要投稿

刚接触到一个内联图片的概念,内联图片即使把图片文件编码成base64 看下面代码即是内联问题

可以减少http的请求,缺点是不能跨域缓存!


 

?
1
2
3
< img src = "data:image/jpeg;base64,/9j/4QqsRX..." alt = "" >
 
< img src = "data:image/jpeg;base64,/9j/4QqsRX..." alt = "" >

然后在线如何把图片转化成base64

如果只依靠单纯的javascript是有权限问题的 js不允许操作本地的file文件或文件夹 为了安全问题

现在html5来了 百度了下有不少资料 中文的也不少   现在我们用html5的file api里的 readAsDataURL函数 这是一个把文件转化成base64编码的


 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
?<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
< title >简单的html5 File测试 for pic2base64</ title >
< style >
</ style >
< script >
window.onload = function(){
     var input = document.getElementById("demo_input");
     var result= document.getElementById("result");
     var img_area = document.getElementById("img_area");
     if ( typeof(FileReader) === 'undefined' ){
             result.innerHTML = "抱歉,你的浏览器不支持 FileReader,请使用现代浏览器操作!";
             input.setAttribute( 'disabled','disabled' );
     } else {
             input.addEventListener( 'change',readFile,false );}
}
function readFile(){
         var file = this.files[0];
//这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件  
         if(!/image\/\w+/.test(file.type)){  
                 alert("请确保文件为图像类型");
                 return false;
         }
         var reader = new FileReader();
         reader.readAsDataURL(file);
         reader.onload = function(e){
                 result.innerHTML = '< img src = "'+this.result+'" alt = "" />';
                 img_area.innerHTML = '< div class = "sitetip" >图片img标签展示:</ div >< img src = "'+this.result+'" alt = "" />';
         }
}
</ script >
</ head
< body >
< input type = "file" value = "sdgsdg" id = "demo_input" />
< textarea id = "result" rows = 30 cols = 300 ></ textarea >
< p id = "img_area" ></ p >
</ body >
</ html >
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
< title >简单的html5 File测试 for pic2base64</ title >
< style >
</ style >
< script >
window.onload = function(){
  var input = document.getElementById("demo_input");
  var result= document.getElementById("result");
  var img_area = document.getElementById("img_area");
  if ( typeof(FileReader) === 'undefined' ){
          result.innerHTML = "抱歉,你的浏览器不支持 FileReader,请使用现代浏览器操作!";
          input.setAttribute( 'disabled','disabled' );
  } else {
          input.addEventListener( 'change',readFile,false );}
}
function readFile(){
         var file = this.files[0];
//这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件
         if(!/image\/\w+/.test(file.type)){
                 alert("请确保文件为图像类型");
                 return false;
         }
         var reader = new FileReader();
         reader.readAsDataURL(file);
         reader.onload = function(e){
                 result.innerHTML = '< img src = "'+this.result+'" alt = "" />';
                 img_area.innerHTML = '< div class = "sitetip" >图片img标签展示:</ div >< img src = "'+this.result+'" alt = "" />';
         }
}
</ script >
</ head >
< body >
< input type = "file" value = "sdgsdg" id = "demo_input" />
< textarea id = "result" rows = 30 cols = 300 ></ textarea >
< p id = "img_area" ></ p >
</ body >
</ html >
下面是Java实现Base64图片互转的代码: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Base64; public class ImageBase64Util { /** * 将图片转换Base64编码字符串 * * @param imagePath 图片路径 * @return Base64编码字符串 */ public static String imageToBase64(String imagePath) { String base64 = null; InputStream in = null; try { in = new FileInputStream(imagePath); byte[] bytes = new byte[in.available()]; in.read(bytes); base64 = Base64.getEncoder().encodeToString(bytes); } catch (Exception e) { e.printStackTrace(); } finally { try { in.close(); } catch (Exception e) { e.printStackTrace(); } } return base64; } /** * 将Base64编码字符串转换图片 * * @param base64 Base64编码字符串 * @param imagePath 图片保存路径 * @return 是否保存成功 */ public static boolean base64ToImage(String base64, String imagePath) { boolean result = false; OutputStream out = null; try { out = new FileOutputStream(imagePath); byte[] bytes = Base64.getDecoder().decode(base64); out.write(bytes); result = true; } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (Exception e) { e.printStackTrace(); } } return result; } } ``` 使用示例: ```java public class ImageBase64UtilTest { public static void main(String[] args) { // 图片路径 String imagePath = "D:/test.png"; // 将图片转换Base64编码字符串 String base64 = ImageBase64Util.imageToBase64(imagePath); System.out.println(base64); // 将Base64编码字符串转换图片 String newImagePath = "D:/newtest.png"; ImageBase64Util.base64ToImage(base64, newImagePath); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值