package com.whyc.util;
|
|
import org.apache.tomcat.util.codec.binary.Base64;
|
|
import java.io.*;
|
|
public class FileCopyUtil {
|
/**
|
* base64转File
|
* @param base64
|
* @param dest
|
*/
|
public static boolean base64ToFile(String base64, File dest) {
|
BufferedInputStream bis = null;
|
BufferedOutputStream bos = null;
|
try {
|
//由于base64在传输过程中,+和/和=会被替换所以在解码前需要将base64还原成可用的base64
|
base64 = base64.replaceAll(" ","+");
|
base64 = base64.replaceAll("%2F","/");
|
base64 = base64.replaceAll("%3D","=");
|
//当使用springMVC时无需使用以上方法进行还原
|
|
byte[] bytes = Base64.decodeBase64(base64.replace("\r\n", ""));
|
bis = new BufferedInputStream(new ByteArrayInputStream(bytes));
|
bos = new BufferedOutputStream(new FileOutputStream(dest));
|
byte[] bts = new byte[1024];
|
int len = -1;
|
while((len = bis.read(bts)) != -1) {
|
bos.write(bts,0,len);
|
}
|
return true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return false;
|
} finally {
|
if (bos != null) {
|
try {
|
bos.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (bis != null) {
|
try {
|
bis.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
}
|