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;
        }
  • 相关阅读:
    java exception
    【洛谷P1627】 【CQOI2009】中位数
    切蛋糕
    【NOIP2015Day2T2】【洛谷P2679】子串
    【NOIP2017Day1T3】【洛谷P3953】逛公园
    【bzoj1082】【SCOI2005】栅栏
    搬砖
    花花的森林
    跳跳棋
    异或
  • 原文地址:https://www.cnblogs.com/xiangguoguo/p/9756992.html
Copyright © 2011-2022 走看看