国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

基于Java如何實(shí)現文件和base64字符串轉換

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù) 歡迎投稿:712375056

這篇文章主要為大家展示了“基于Java如何實(shí)現文件和base64字符串轉換”,內容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習一下“基于Java如何實(shí)現文件和base64字符串轉換”這篇文章吧。

這篇文章主要介紹了基于Java實(shí)現文件和base64字符串轉換,文中通過(guò)示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價(jià)值,需要的朋友可以參考下

項目中遇到需要將圖片轉成base64編碼的字符串的需求,但是,考慮到擴展性,寫(xiě)了一個(gè)可以轉換任務(wù)類(lèi)型文件的方法。需要引入的包:

<dependency>      <groupId>commons-codec</groupId>      <artifactId>commons-codec</artifactId>      <version>1.13</version>    </dependency>

源碼如下:

import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;  import java.io.*;  public class Base64FileUtil {    private static String targetFilePath = "E:\\base2Img\\target\\test.txt";    public static void main(String[] args) throws Exception {    String fileStr = getFileStr("E:\\base2Img\\big test.txt");    System.out.println("fileStr ===" + fileStr);    System.out.println(generateFile(fileStr, targetFilePath));    System.out.println("end");  }    /**   * 文件轉化成base64字符串   * 將文件轉化為字節數組字符串,并對其進(jìn)行Base64編碼處理   */  public static String getFileStr(String filePath) {    InputStream in = null;    byte[] data = null;    // 讀取文件字節數組    try {      in = new FileInputStream(filePath);      data = new byte[in.available()];      in.read(data);      in.close();    } catch (IOException e) {      e.printStackTrace();    } finally {      try {        in.close();      } catch (IOException e) {        e.printStackTrace();      }    }    // 對字節數組Base64編碼    BASE64Encoder encoder = new BASE64Encoder();    // 返回 Base64 編碼過(guò)的字節數組字符串    return encoder.encode(data);  }    /**   * base64字符串轉化成文件,可以是JPEG、PNG、TXT和AVI等等   *   * @param base64FileStr   * @param filePath   * @return   * @throws Exception   */  public static boolean generateFile(String base64FileStr, String filePath) throws Exception {    // 數據為空    if (base64FileStr == null) {      System.out.println(" 不行,oops! ");      return false;    }    BASE64Decoder decoder = new BASE64Decoder();      // Base64解碼,對字節數組字符串進(jìn)行Base64解碼并生成文件    byte[] byt = decoder.decodeBuffer(base64FileStr);    for (int i = 0, len = byt.length; i < len; ++i) {      // 調整異常數據      if (byt[i] < 0) {        byt[i] += 256;      }    }    OutputStream out = null;    InputStream input = new ByteArrayInputStream(byt);    try {      // 生成指定格式的文件      out = new FileOutputStream(filePath);      byte[] buff = new byte[1024];      int len = 0;      while ((len = input.read(buff)) != -1) {        out.write(buff, 0, len);      }    } catch (IOException e) {      e.printStackTrace();    } finally {      out.flush();      out.close();    }    return true;  } }

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

久久久久久成人毛片免费看| 亚洲欧美一区二区成人片| 久久婷婷丁香七月色综合| 琪琪女色窝窝777777| 出租屋嫖妓大龄熟妇露脸在线播放| 国产午夜精品理论片 |