zoukankan      html  css  js  c++  java
  • File与Base64之间的转换

    import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

    1、File转Base64

    public static String file2Base64(File file) {
            if(file==null) {
                return null;
            }
            String base64 = null;
            FileInputStream fin = null;
            try {
                fin = new FileInputStream(file);
                byte[] buff = new byte[fin.available()];
                fin.read(buff);
                base64 = Base64.encode(buff);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fin != null) {
                    try {
                        fin.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return base64;
        }

    2、Base64转File

    public static File base64ToFile(String base64) {
            if(base64==null||"".equals(base64)) {
                return null;
            }
            byte[] buff=Base64.decode(base64);
            File file=null;
            FileOutputStream fout=null;
            try {
                file = File.createTempFile("tmp", null);
                fout=new FileOutputStream(file);
                fout.write(buff);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(fout!=null) {
                    try {
                        fout.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return file;
        }
  • 相关阅读:
    设置WebSphere字符集参数
    防SQL注入
    改变radio/checkbox默认样式
    数据完整性约束错误
    Java项目多数据源配置
    No row with the given identifier exists:错误另解
    ICTCLAS20160405分词系统调试过程
    centos7 忘记root密码
    java之Junit
    javaweb之登录
  • 原文地址:https://www.cnblogs.com/xiangguoguo/p/9756992.html
Copyright © 2011-2022 走看看