Java將圖片的路徑轉為Base64,VUE前端顯示


項目里前端圖片因為某種原因,不能解密顯示出來,所以就想了個辦法,通過后台處理,然后在前端顯示,非常感謝各位的代碼分享,我忘記是那個博主分享的了,本來想附上路徑的,找不到了,再次感謝

后台:

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字符串
         * 將文件轉化為字節數組字符串,並對其進行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 編碼過的字節數組字符串
            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解碼,對字節數組字符串進行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;
        }
}

前端調用(src一定要加上data部分的,要不不會識別為圖片,imgSrc是base64碼):

 <el-image class="view-img" :src="'data:image/jpeg;base64,'+imgSrc"></el-image>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM