【问题标题】:Image without extension in src not loading in IE alone, and works perfect in all other browers在 src 中没有扩展名的图像不能单独在 IE 中加载,并且在所有其他浏览器中都可以完美运行
【发布时间】:2017-01-12 14:59:40
【问题描述】:

我有以下 HTML 代码:

<img title="hotelThumbImage" id="hotelThumbImage01" width="140px" height="129px" 
src="/b2c/images/?url=FixedPkgB2c/FF-252-325"/>

它在IE中呈现如下:

它在所有其他浏览器(如 FireFox 和 Chrome)中呈现为:

相关问题:How to make a Servlet call form UI which returns the Content itself and place an img tag using Script in the output?

【问题讨论】:

  • 问题出在/image URL 后面的代码中。它没有设置内容类型标题。无法给出答案,因为代码在目前提供的信息中不可见。
  • 其实我是一名UI开发者,抱歉打错标签了。
  • 告诉网络开发者图片内容类型丢失或错误。优秀的 Web 开发人员会理解它。
  • 或者可能是 X-Content-Type-Options:nosniff 带有通用 Content-Type:application/octet-stream

标签: image spring-mvc internet-explorer


【解决方案1】:

我的项目也遇到了这个问题,这是因为 IE 阻止下载/显示编码与其扩展名不同的文件。这与恶意代码只需更改文件扩展名即可隐藏为图像文件有关。

Firefox 和 Chrome 足够聪明,只要编码是图像的编码,就可以将其显示为图像,但 IE 似乎没有机会。

您必须添加与图像编码相匹配的扩展名才能在 IE 中显示。

编辑:您的服务器也可能发送带有表示纯文本的标头的文件。同样,Firefox 和 Chrome 足够聪明地处理它,但 IE 不是。见:https://stackoverflow.com/a/32988576/4793951

【讨论】:

  • 即使我对相同的扩展进行热编码并检查它对我没有帮助,并且通过更改开发人员工具也没有帮助。
【解决方案2】:

欢迎来到 IE 世界... :(


为了更好的控制情况,我会做的是修改getter方法,所以在Holiday.getPkgCode()

public String getPkgCode() throws IOException {
    if (!this.pkgCode.contains(".")) {
        String ext = ImgUtil.determineFormat(this.pkgCode);
        return this.pkgCode + ImgUtil.toExtension(ext);
    } else {
        return this.pkgCode;
    }
}

要使用它,您需要捕获异常,这个 ImgUtil 类适应 from here

class ImgUtil {

    public static String determineFormat(String name) throws IOException {

        // get image format in a file
        File file = new File(name);

        // create an image input stream from the specified file
        ImageInputStream iis = ImageIO.createImageInputStream(file);

        // get all currently registered readers that recognize the image format
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            throw new RuntimeException("No readers found!");
        }

        // get the first reader
        ImageReader reader = iter.next();
        String toReturn = reader.getFormatName();

        // close stream
        iis.close();

        return toReturn;

    }

    public static String toExtension(String ext) {
        switch (ext) {
        case "JPEG": return ".jpg";
        case "PNG": return ".png";
        }
        return null;
    }

}

测试一下:

注意: 我在C:\tmp folder中放置了一个没有扩展名的图像(jpg

public class Q37052184 {

    String pkgCode = "C:\\tmp\\yorch";

    public static void main(String[] args) throws IOException {
        Q37052184 q = new Q37052184();
        System.out.println(q.getPkgCode());
    }

    // the given getter!!!
}

输出:

C:\tmp\yorch.jpg

【讨论】:

    【解决方案3】:

    您必须在 servlet 中设置响应标头的 Content Type 属性。

    例如在spring 4 mvc中,

    @GetMapping(value = "/b2c/images/?url=FixedPkgB2c/FF-252-325")
    public ResponseEntity<byte []> getImageThumbnail() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(media type));
        byte [] content= ...;
        return ResponseEntity.ok().headers(headers).body(content);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 2012-09-22
      • 2012-07-28
      • 2014-08-13
      • 1970-01-01
      相关资源
      最近更新 更多