首先在http://commons.apache.org/proper/commons-codec/下载 jar包
import org.apache.commons.codec.binary.Base64;
实现代码如下:
public String getFileByteString(File file) throws Exception{
Base64 b64 = new Base64();
FileInputStream fis = new FileInputStream(file);
System.out.print(file.length());
byte[] buffer = new byte[(int)file.length()];
System.out.print(buffer.length);
fis.read(buffer);
fis.close();
return b64.encodeToString(buffer);
}
public void getFileByString(String string, String target) throws Exception{
Base64 b64 = new Base64();
byte[] buffer = b64.decode(string);
FileOutputStream fos = new FileOutputStream(target);
fos.write(buffer);
fos.close();
}