关于IE中图片不显示的问题(IE兼容问题)

修改前在IE里是这样。。或者更惨都是小黑x。。我就纳闷了,在别的浏览器都好使。。

 找了好久原因 。。

 最终发现是 response.setContentType 设置的问题。

 之前我是这样写的:

response.setContentType("image/*");

后来就改成 发现 png 不显示

response.setContentType("image/jpeg");

然后在网上找了一个比较全的图片 ContentType

最终是这样的

   private static Map<String, String> imageContentType = new HashMap<>();
    static {
        imageContentType.put("jpg", "image/jpeg");
        imageContentType.put("jpeg", "image/jpeg");
        imageContentType.put("png", "image/png");
        imageContentType.put("tif", "image/tiff");
        imageContentType.put("tiff", "image/tiff");
        imageContentType.put("ico", "image/x-icon");
        imageContentType.put("bmp", "image/bmp");
        imageContentType.put("gif", "image/gif");
    }
    
    public static final String showImg(HttpServletResponse response, String path) throws IOException {
          // 获取文件名后缀
          String fileType = path.substring(path.lastIndexOf(".") + 1).toLowerCase();
          FileInputStream fileIs=null;
          try {
              fileIs = new FileInputStream(path);
          } catch (Exception e) {
           return null;
          }
          int i=fileIs.available(); //得到文件大小   
          byte data[]=new byte[i];   
          fileIs.read(data);  //读数据
          response.setContentType(imageContentType.get(fileType));
          //response.setContentType("image/jpeg"); //设置返回的文件类型   图片
          OutputStream outStream=response.getOutputStream(); //得到向客户端输出二进制数据的对象
          outStream.write(data);  //输出数据
          outStream.flush();
          outStream.close();   
          fileIs.close();
          return path;
    }

@

原文地址:https://www.cnblogs.com/DarGi2019/p/13366921.html