如何將InputStream轉化爲base64

  1. /**
  2. * @author
  3. * @date
  4. * @version 1.0
  5. */
  6. public class FileToBase64 {
  7. public static String getBase64FromInputStream(InputStream in) {
  8. // 將圖片文件轉化爲字節數組字符串,並對其進行Base64編碼處理
  9. byte[] data = null;
  10. // 讀取圖片字節數組
  11. try {
  12. ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
  13. byte[] buff = new byte[100];
  14. int rc = 0;
  15. while ((rc = in.read(buff, 0, 100)) > 0) {
  16. swapStream.write(buff, 0, rc);
  17. }
  18. data = swapStream.toByteArray();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } finally {
  22. if (in != null) {
  23. try {
  24. in.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. return new String(Base64.encodeBase64(data));
  31. }
  32. }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章