zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    固定表头/锁定前几列的代码参考[JS篇]
    盘点mysql中容易被我们误会的地方
    cookie&session的Q&A故事[原理篇]
    网络第一道防线:验证码的故事[安全篇]
    2016,把一年的牛皮先吹了吧[生涯规划篇]
    微软职位内部推荐-Software Engineer II
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-SW Engineer II for Cloud Servi
    微软职位内部推荐-SW Engineer II for Cloud Servi
  • 原文地址:https://www.cnblogs.com/ly-gaoshuaige/p/14242649.html
Copyright © 2011-2022 走看看