java利用Base64编码和解码图片文件

1、编码与解码代码如下所示:

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author zxn
 * @version 创建时间:2014-7-2 上午11:40:40
 *
 */
public class ImageUtils {
    /**
     * 将网络图片进行Base64位编码
     *
     * @param imgUrl
     *            图片的url路径,如http://.....xx.jpg
     * @return
     */
    public static String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        ByteArrayOutputStream outputStream = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imageUrl);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
    }

    /**
     * 将本地图片进行Base64位编码
     *
     * @param imgUrl
     *            图片的url路径,如http://.....xx.jpg
     * @return
     */
    public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        ByteArrayOutputStream outputStream = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imageFile);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
    }

    /**
     * 将Base64位编码的图片进行解码,并保存到指定目录
     *
     * @param base64
     *            base64编码的图片信息
     * @return
     */
    public static void decodeBase64ToImage(String base64, String path,
            String imgName) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            FileOutputStream write = new FileOutputStream(new File(path
                    + imgName));
            byte[] decoderBytes = decoder.decodeBuffer(base64);
            write.write(decoderBytes);
            write.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2、如何直接在页面上显示base64编码的图片?

样例如下:

<html>
<body>
<img src='data:image/jpg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCACBAQ4DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiig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+o2VkkGo6dHfyIoUTCYxM3u3BBP5V7XNDFcRNFNGkkbDDI6ggj3BqlBoOjWpzb6TYRH1jtkX+Qr6R5tSrUlDFUuZrrex4nsmneLPH/EPjvV/GGn2kem+HLuGS2u0uo54Waf5lDYGAg9c/hWq/wAZb+xRI9R8MNHPj5sztEG9wrISB+Jr1sAAAAYAqKe3guojFcQxzRnqkihgfwNYfXsK0oSoe6v7zvr5j5Jb3Pnfxz45/wCEz+wf8S77H9k8z/lv5m/ft/2RjG39a6z4O+GbqO7m1+6haOAxGK23jG/JGWHsAMZ75PpXpsfhnQIpBJHoemo453LaRg/nitQAAAAYArXEZrB4b6th4csfW/mKNJ83NJnnnxl/5E22/wCv5P8A0B65Hw38Vv8AhH/D9ppX9i/aPs6keb9q2bssT02HHX1r3GivnJ0ZOfPGVvke9h8yoQwqw1ejzpO/xNfkjyP/AIXf/wBS9/5O/wD2uuL8S+JJPGfiezvI7BoHCJAsKv5hbDE56D+90xX0hRUTw9SatKenob4fNsJhp+0o4a0v8bf5o8i+N/39D+k//tOm+IviTplwt3ol/wCGxeJBI0IL3GMlSV3DC5U8djmvX6KqVCTlKUZWv5GNHNKMaNOlVpc3Jez5mt3foj5wsPDvi6XQ724sLa/h048vArsvmj2T+PHfitfwv480rwpa+UnhgC+A2zXHn/O579VJX6DiveKKiOEcGnGWvpc6qufxrxlCvRvFvpJx++25BZ3H2uxt7nbs86JZNuc4yM4zU9FFdh867N6BRRRQIKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/2Q=='/>

</body>
</html>

java利用Base64编码和解码图片文件,布布扣,bubuko.com

时间: 2024-10-16 22:52:12

java利用Base64编码和解码图片文件的相关文章

android Java BASE64编码和解码二:图片的编码和解码

1.准备工作 (1)在项目中集成 Base64 代码,集成方法见第一篇博文:android Java BASE64编码和解码一:基础 (2)添加 ImgHelper 工具类 package com.app21; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import android.graphics.Bitmap; import android

java处理图片base64编码的相互转换

哈喽大家好我是刘德华. 今天项目优化了一下上传头像的功能.采用imagecropper插件完成裁剪图片的效果. 这个插件裁剪完的图片都是base64加密的字符串,上传头像也就涉及到了如何把加密的字符串转换成图片的问题. 以下是代码: /** * @Description: 将base64编码字符串转换为图片 * @Author: * @CreateTime: * @param imgStr base64编码字符串 * @param path 图片路径-具体到文件 * @return */ pub

win api 实现 base64编码、解码

最近在写小玩意,用到了base64编码,但是考虑到体积大小,网上的多种实现都是利用开源的代码,这就增加了其大小.我想win api能不能实现base64编码.解码.通过一通乱搜,还有收获.就有了以下代码 #pragma comment(lib,"crypt32.lib") LPSTR Base64Encode(LPBYTE lpBuffer,DWORD dwLen) { DWORD dwNeed; LPSTR lpBase64Str ; DWORD dwIndex ; DWORD dw

base64编码、解码的C语言实现

转自:http://www.cnblogs.com/yejianfei/archive/2013/04/06/3002838.html base64是一种基于64个可打印字符来表示二进制数据的表示方法.由于26=64,所以每6位为一个单位,对应某个可打印字符.三个字节共24位,对应于4个base64单位,即3个字节需要用4个可打印字符来表示.它常用来作为电子邮件的传输编码.在base64中的可打印字符包括大写英文字母A-Z,小写英文字母a-z.阿拉伯数字0-9,这样共有62个字符,此外两个可打印

C# 字节流通过Base64编码转换成图片代码

C# 字节流通过Base64编码转换成图片代码 // 需载入以下的命名空间 // using System.IO; // using System.Drawing; // using System.Runtime.Serialization.Formatters.Binary; protected void Page_Load(object sender, EventArgs e) { byte[] buffer = ReadFile(Server.MapPath(@"\sex.txt"

python中base64编码与解码

引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应报文进行解码: Base64编码是一种"防君子不防小人"的编码方式.广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有"=",生成的编码都是ascii字符.优点:速度快,ascii字符,肉眼不可理解缺点:编码比较长,非常容易被破解,仅适用于加密非关键信息的场合python2中进行Base64编码和解码&g

PHP安全的URL字符串base64编码和解码

这篇文章主要介绍了PHP安全的URL字符串base64编码和解码,在base64的基础上替换了不安全的一些字符,需要的朋友可以参考下 如果直接使用base64_encode和base64_decode方法的话,生成的字符串可能不适用URL地址.下面的方法可以解决该问题: URL安全的字符串编码: function urlsafe_b64encode($string) {    $data = base64_encode($string);    $data = str_replace(array

Python中进行Base64编码和解码

Base64编码是一种“防君子不防小人”的编码方式.广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符.优点:速度快,ascii字符,肉眼不可理解缺点:编码比较长,非常容易被破解,仅适用于加密非关键信息的场合Python中进行Base64编码和解码>>> import base64>>> s = '我是字符串'>>> a = base64.b64encode(s)>>>

转: Javascript Base64编码与解码

Javascript Base64编码与解码 分类:            JavaScript            2015-02-04 14:25    212人阅读    评论(0)    收藏    举报 转自:http://blog.csdn.net/yanzhibo/article/details/20079943 [javascript] view plaincopy <html> <head> <META HTTP-EQUIV="MSThemeCo