- /**
- * @author
- * @date
- * @version 1.0
- */
- public class FileToBase64 {
- public static String getBase64FromInputStream(InputStream in) {
- // 將圖片文件轉化爲字節數組字符串,並對其進行Base64編碼處理
- byte[] data = null;
- // 讀取圖片字節數組
- try {
- ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
- byte[] buff = new byte[100];
- int rc = 0;
- while ((rc = in.read(buff, 0, 100)) > 0) {
- swapStream.write(buff, 0, rc);
- }
- data = swapStream.toByteArray();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return new String(Base64.encodeBase64(data));
- }
- }