溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Java將文件和base64字符串進行轉(zhuǎn)換

發(fā)布時間:2021-02-24 16:22:45 來源:億速云 閱讀:272 作者:戴恩恩 欄目:編程語言

本文章向大家介紹怎么在Java將文件和base64字符串進行轉(zhuǎn)換的基本知識點總結(jié)和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。

Java的特點有哪些

Java的特點有哪些 1.Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,實現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進行復雜的編程。 2.Java具有簡單性、面向?qū)ο?、分布式、安全性、平臺獨立與可移植性、動態(tài)性等特點。 3.使用Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。

需要引入的包:

<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");
  }
 
 
  /**
   * 文件轉(zhuǎn)化成base64字符串
   * 將文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進行Base64編碼處理
   */
  public static String getFileStr(String filePath) {
    InputStream in = null;
    byte[] data = null;
    // 讀取文件字節(jié)數(shù)組
    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();
      }
    }
    // 對字節(jié)數(shù)組Base64編碼
    BASE64Encoder encoder = new BASE64Encoder();
    // 返回 Base64 編碼過的字節(jié)數(shù)組字符串
    return encoder.encode(data);
  }
 
 
  /**
   * base64字符串轉(zhuǎn)化成文件,可以是JPEG、PNG、TXT和AVI等等
   *
   * @param base64FileStr
   * @param filePath
   * @return
   * @throws Exception
   */
  public static boolean generateFile(String base64FileStr, String filePath) throws Exception {
    // 數(shù)據(jù)為空
    if (base64FileStr == null) {
      System.out.println(" 不行,oops! ");
      return false;
    }
    BASE64Decoder decoder = new BASE64Decoder();
 
 
    // Base64解碼,對字節(jié)數(shù)組字符串進行Base64解碼并生成文件
    byte[] byt = decoder.decodeBuffer(base64FileStr);
    for (int i = 0, len = byt.length; i < len; ++i) {
      // 調(diào)整異常數(shù)據(jù)
      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;
  }
 
}

以上就是小編為大家?guī)淼脑趺丛贘ava將文件和base64字符串進行轉(zhuǎn)換的全部內(nèi)容了,希望大家多多支持億速云!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI