提问者:小点点

IE图像未加载x-content-type-options:nosniff


介绍

@RequestMapping(value = "/getUserImage" , produces = org.springframework.http.MediaType.IMAGE_PNG_VALUE)
public @ResponseBody
void getUserImage(
        @RequestParam(value = "userId", required = false) int userId,
        HttpServletRequest request, HttpServletResponse response) {

    try {
        //Get file and add it to response
        IOUtils.copy(inputStream, response.getOutputStream());
        response.getOutputStream().flush();
        response.setContentType(org.springframework.http.MediaType.IMAGE_PNG_VALUE);
        response.setHeader("Content-Type","image/png");
        response.flushBuffer();
        inputStream.close();
    } catch (Exception e){
    }
}

我尝试在方法拦截器中以同样的方式添加响应头,但仍然没有成功。

但在Chrome和Firefox中也是如此。


共1个答案

匿名用户

试试这个:

 @RequestMapping(value = "/image/{personId}")
    @ResponseBody
    public HttpEntity<byte[]> getPhoto(@PathVariable int personId) {
        Person person = this.personService.getPersonById(personId);
        if (person != null && person.getProfileThumbnail() != null) {
            try {
                byte[] image;
                try {
                    image = org.apache.commons.io.FileUtils.readFileToByteArray(new File(msg + "/" + person.getUsername() + "/" + personId + ".png"));
                } catch (FileNotFoundException e) {
                    image = org.apache.commons.io.FileUtils.readFileToByteArray(new File(defaultProfilePath));
                }
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.IMAGE_PNG);
                headers.setContentLength(image.length);
                return new HttpEntity<>(image, headers);
            } catch (IOException ignored) {
            }

        } 
}

我所做的基本上是检查是否有一个图像在文件系统上为用户,如果没有,然后我加载一个默认的图像。现在它可以在所有浏览器上工作,所以即使personid是0,我也会得到默认图像,还有其他原因,我没有在这里发布。