zoukankan      html  css  js  c++  java
  • Base64编码保存为图片,java工具类

    import com.weiming.so.nova.common.utils.uuid.UUID;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Base64;
    
    public class Base64FileUtil {
    
        /**
         * base64 保存为文件
         * 直接获取系统temp目录,文件名UUID
         * @param base64code base64code
         * @return {@link File}
         * @throws IOException ioexception
         */
        public static File base64ToFile(String base64code) throws IOException {
            String tmpdir = System.getProperty("java.io.tmpdir") + File.separator;
    
            Base64.Decoder decoder = Base64.getDecoder();
            // 前台在用Ajax传base64值的时候会把base64中的+换成空格,所以需要替换回来。
            String baseValue = base64code.replaceAll(" ", "+");
            // 确定第一个逗号,用于截取data:image/jpeg;base64
            int ftIdx = baseValue.indexOf(",") + 1;
            // 文件类型
            String fileType = baseValue.substring(0,ftIdx);
            // 文件内容
            String fileContent = baseValue.substring(ftIdx);
            // 文件后缀
            String fileSuffix =getBase64FileSuffix(fileType);
            // 文件名
            String fileName = UUID.randomUUID() + fileSuffix;
            // 解密
            byte[] b = decoder.decode(fileContent);
            // 创建临时文件
            File file = createFile(tmpdir, fileName);
            // 写入文件
            try(OutputStream outputStream = new FileOutputStream(file)){
                // 处理数据
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {
                        b[i] += 256;
                    }
                }
                outputStream.write(b);
                outputStream.flush();
            }
            return file;
        }
    
        /**
         * 得到Base64的文件后缀
         *
         * @param iden iden
         * @return {@link String}
         */
        public static String getBase64FileSuffix(String iden){
            if(iden == null || iden.isEmpty()){
                return null;
            }
            if (iden.contains("jpeg")) {
                return ".jpg";
            }else if(iden.contains("png")){
                return ".png";
            }else if(iden.contains("gif")){
                return ".gif";
            }else if(iden.contains("jpg")){
                return ".jpg";
            }
            return null;
        }
    
        /**
         * 创建临时文件
         *
         * @param filePaht 文件目录
         * @param fileName 文件名称
         * @return {@link File}
         * @throws IOException ioexception
         */
        public static File createFile(String filePaht, String fileName) throws IOException {
            File tempFile = new File(filePaht, fileName);
            if(!tempFile.getParentFile().exists()){
                tempFile.getParentFile().mkdirs();
            }
            if(!tempFile.exists()){
                tempFile.createNewFile();
            }
            return tempFile;
        }
    }
    
    
  • 相关阅读:
    互联网中的公钥与私钥
    Apache的order、allow、deny
    Linux进程中TIME_OUT解析
    no xxx find in java.library.path
    检测 USB 设备拨插的 C# 类库:USBClassLibrary
    C# 实现的异步 Socket 服务器
    javascript制作公式编辑器,函数编辑器和图形绘制
    浏览器内部工作原理
    10 款基于 jQuery 的切换效果插件推荐
    DIV焦点事件详解 --【focus和tabIndex】​
  • 原文地址:https://www.cnblogs.com/inkyi/p/15726199.html
Copyright © 2011-2022 走看看