zoukankan      html  css  js  c++  java
  • img标签显示图片方法总结

    1、通过图片在项目下的可访问路径。

      例如:<img src='../assets/imags/xxx.jpg' />

    2、通过一个下载器链接,读取(文件)服务器上的图片资源。

      例如:<img src='http://ip:port/projectName/getImageServlet?imagesPath' />

      后端代码:

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String imagePath = "D://牙牙牙白.gif";
            String contentType = "";
            String lowerPath = imagePath.toLowerCase();
            if(lowerPath.endsWith("jpg") || lowerPath.endsWith("jpe") || lowerPath.endsWith("jpeg")){
                contentType = "image/jpeg;charset=utf-8";
            } else if (lowerPath.endsWith("png")) {
                contentType = "image/png;charset=utf-8";
            } else if (lowerPath.endsWith("bmp")) {
                contentType = "image/bmp;charset=utf-8";
            } else if (lowerPath.endsWith("gif")) {
                contentType = "image/gif;charset=utf-8";
            }
            response.setContentType(contentType);
            
            // 文件流的方式
            InputStream in = new FileInputStream(imagePath);// 输入流
            OutputStream out = response.getOutputStream();// 输出流
            
            // 边读边写
            byte[] bt = new byte[1024];
            int length = 0;
            while ((length = in.read(bt)) != -1) {
                out.write(bt, 0, length);
            }
            // 关闭流
            in.close();
            out.flush();
            out.close();
        }  

    3、base64加密数据填充法。

      例如:<img src="data:image/gif;base64,R0lGODlhHAAmAKIHAKqqqsvLy0hISObm5vf394uLiwAAAP///yH5B…  EoqQqJKAIBaQOVKHAXr3t7txgBjboSvB8EpLoFZywOAo3LFE5lYs/QW9LT1TRk1V7S2xYJADs=">

      后端代码:

      public static String getImageBase64() throws Exception{
            String imagePath = "D://牙牙牙白.gif";
            String contentType = "";
            String lowerPath = imagePath.toLowerCase();
            if(lowerPath.endsWith("jpg") || lowerPath.endsWith("jpe") || lowerPath.endsWith("jpeg")){
                contentType = "image/jpeg";
            } else if (lowerPath.endsWith("png")) {
                contentType = "image/png";
            } else if (lowerPath.endsWith("bmp")) {
                contentType = "image/bmp";
            } else if (lowerPath.endsWith("gif")) {
                contentType = "image/gif";
            }
            
            InputStream in = new FileInputStream(imagePath);// 输入流
            ByteArrayOutputStream out = new ByteArrayOutputStream(); // 输出流
            // 边读边写
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
            // 关闭流
            in.close();
            out.flush();
            out.close();
            
            // import sun.misc.BASE64Encoder
            BASE64Encoder encoder = new BASE64Encoder();
            String imageBase64 = new String(encoder.encode(out.toByteArray()));
            String imageSrc = "data:" + contentType + ";base64," + imageBase64;
            
            return imageSrc;
        }

      

      

  • 相关阅读:
    恢复计算机崩溃数据的五款最佳Linux发行版
    不敢想象!Vim使用者的“大脑”竟是这样
    开发者和系统管理者最喜爱的开源工具Vim 起步学习的五个技巧
    .NET程序与CA对接一直提示重定向
    覆盖原有div或者Input的鼠标移上去描述
    IOS9以上如何导入铃声并设置
    c# 动态调用webserver
    没有终结点在侦听可以接受消息的*这通常是由于不正确的地址或者 SOAP操作导致的
    url 参数的加号变成空格处理
    在git 服务器挂载、创建新的项目、克隆新的项目
  • 原文地址:https://www.cnblogs.com/will-666/p/11379052.html
Copyright © 2011-2022 走看看