Save the audio file (mp3) to the local absolute path through the base64 string

/**
  * 获得 base64 字符串的音频文件
  * @return
  */
public String base64() {
    
    
         String url = "D:" + File.separator + "date" + File.separator ;
         String base64Code = null;
        try {
    
    
            System.out.println(url + "13066.mp3");
            base64Code = base64Config.encodeBase64File(url+ "13066.mp3");
         } catch (Exception e) {
    
    
            e.printStackTrace();
            System.out.println("转base64字符串失败");
        }
        return base64Code;
    }
    /**
     * @param base64 字符串
     * @param fileName 文件名
     * @param savePath 保存路径
     */
    public static void base64ToFile(String base64, String fileName, String savePath) {
    
    
        File file = null;
        //创建文件目录
        String filePath = savePath;
        File dir = new File(filePath);
        //判断是否存在文件夹
        if (!dir.exists() && !dir.isDirectory()) {
    
    
            dir.mkdirs();
        }
        BufferedOutputStream bos = null;
        java.io.FileOutputStream fos = null;
        try {
    
    
            byte[] bytes = Base64.getMimeDecoder().decode(base64);
            file=new File(filePath + fileName);
            fos = new java.io.FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
        // 关闭流
            if (bos != null) {
    
    
                try {
    
    
                    bos.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
            if (fos != null) {
    
    
                try {
    
    
                    fos.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
 	   //按年月格式把数据分开保存
       Calendar cal = Calendar.getInstance();
       int year = cal.get(Calendar.YEAR);
       int month = cal.get(Calendar.MONTH )+1;
       String months = month <10 ? "0" + month : month + "";
       String url = "D:" + File.separator + "date" +  File.separator  + year + "_" + months +  File.separator;
       String base64 = this.base64();
       base64ToFile(base64,"xxx.mp3",url);

Guess you like

Origin blog.csdn.net/zhongzih/article/details/111638024