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;
}
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);
public class base64Config {
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
}
public static void decoderBase64File(String base64Code,String targetPath) throws Exception {
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
public static void toFile(String base64Code,String targetPath) throws Exception {
byte[] buffer = base64Code.getBytes();
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
}