源码分享,直接复制粘贴使用即可,亲测可用:
package utils;
import java.io.*;
import java.util.Base64;
public class FileUtil {
public static void download(String base64Content,String filePath) throws IOException {
Base64.Decoder decoder = Base64.getMimeDecoder();
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
byte[] bytes = decoder.decode(base64Content);
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
bis = new BufferedInputStream(byteInputStream);
File file = new File(filePath);
File path = file.getParentFile();
if(!path.exists()){
path.mkdirs();
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int length = bis.read(buffer);
while(length != -1){
bos.write(buffer, 0, length);
length = bis.read(buffer);
}
bos.flush();
bos.close();
fos.close();
bis.close();
}
}