File converted to Base64

File converted to Base64, mainly for picture transmission. 

. 1
Import java.io.File; 2 Import a java.io.FileInputStream; . 3 Import java.io.IOException; . 4 Import a java.io.InputStream; . 5 Import sun.misc.BASE64Encoder; . 6 . 7 public class Base64Util { . 8 . 9 / * * 10 * Base64 string into the file . 11 * @param file 12 is * @return 13 is * / 14 public static string getFileBase64 (file file) { 15 InputStream in = null; 16 byte[] data = null; 17 try{ 18 in = new FileInputStream(file); 19 data = new byte[in.available()]; 20 in.read(data); 21 }catch (IOException e){ 22 e.printStackTrace(); 23 }finally { 24 if (in != null) { 25 try { in.close(); } catch (IOException e) { e.printStackTrace(); } 26 } 28 } 29 //Base64编码 30 BASE64Encoder encoder = new BASE64Encoder(); 31 return encoder.encode(data); 32 } 3340 }

 

 

Guess you like

Origin www.cnblogs.com/yan-zm/p/11984361.html