zoukankan      html  css  js  c++  java
  • 关于流的转换

      /**
         * file文件转byte数组
         * */
    public byte[] fileToByte(String filePath){ byte[] data = null; FileInputStream fis = null; ByteArrayOutputStream baos = null; try { fis = new FileInputStream(filePath); baos = new ByteArrayOutputStream(); int len; byte[] buffer = new byte[1024]; while ((len = fis.read(buffer)) != -1) { baos.write(buffer, 0, len); } data = baos.toByteArray(); fis.close(); baos.close(); } catch (Exception e) { log.error("FileUpload_fileToByte_error:"+e.toString()); } return data; }
       /**
         * file文件压缩后转成base64
         * */
        public static String zipByteArrayOutputStream(String filePath) {
            File zip = ZipUtil.zip(filePath);
            if (null == zip){
                throw new BizException(ErrCode.UserEnum.FILE_NOT_EXIST);
            }
            String result = null;
            FileInputStream inputFile = null;
            try {
                inputFile = new FileInputStream(zip);
                byte[] bytes = new byte[(int)zip.length()];
                inputFile.read(bytes);
                result= Base64.getEncoder().encodeToString(bytes);
            }catch (Exception e){
                log.error("YLVirtualAccountRouteServiceImpl_FileUpload 文件压缩转Base64失败:"+e.toString());
                throw new BizException(ErrCode.UserEnum.ZIP_TO_FILE_ERROR);
            }finally {
                try {
                    inputFile.close();
                }catch (Exception e){
                    log.error("YLVirtualAccountRouteServiceImpl_FileUpload 文件压缩转Base64失败:"+e.toString());
                    throw new BizException(ErrCode.UserEnum.ZIP_TO_FILE_ERROR);
                }
            }
            return result;
        }
    /** * base64 转成file * */
    public
    static void base64ToFile(String base64,String targetPath,String fileName)throws Exception { byte[] decode = Base64.getDecoder().decode(base64); FileOutputStream out = new FileOutputStream(targetPath+"/"+fileName); out.write(decode); out.close(); }
  • 相关阅读:
    由于客观原因,暂时学习php两天,然后继续学习.net
    【任务】html编辑器在vs2003下实现
    one bug og webMatrix when create a new file
    数据统一接口?
    安全3S
    一个订单管理页面
    【总结】浪费3个月向.net继续前进
    关于在asp.net中类的继承问题
    【心得】create a data table in webMatrix is very easy!
    Java与.NET谁是未来
  • 原文地址:https://www.cnblogs.com/bokai/p/11579303.html
Copyright © 2011-2022 走看看