用java流讀取字節文件的注意事項

在java用字節流讀取視頻或者圖片或者音頻文件的時候,務必要記住,用字節流讀出來的東西,必定要以字節的形式保存,傳遞,不能轉換成字符串的形式。若是肯定要以字符串的形式進行保存傳遞,那麼須要用BASE64進行加密,解密。



public static void main(String[] args) throws Exception{
    FileInputStream   fileForInput   =   new   FileInputStream("F:\\ppt\\ppt.jpg");  
      String   content   =   new   String();  
      byte[]   bytes   =   new   byte[fileForInput.available()];  
      fileForInput.read(bytes);  
      content   =   new   sun.misc.BASE64Encoder().encode(bytes);   //具體的編碼方法  
      fileForInput.close();  
      System.out.println(content);
       
     
     
      byte[]   result   =new   sun.misc.BASE64Decoder().decodeBuffer(content.trim());  
      RandomAccessFile   inOut   =   new   RandomAccessFile("d:\\my.jpg","rw");  
      inOut.write(result);  
      inOut.close(); 
}


在用get方法傳送二進制文件字符的時候,須要用
  System.out.println(URLEncoder.encode(GetImageStr("c:/dest.jpg"), "utf-8")); 處理一下,以防瀏覽器對url參數中的特殊字符進行一些處理。
相關文章
相關標籤/搜索