图片流输出到前台成图片预览显示

            HttpServletResponse response = this.getResponse();
            response.setCharacterEncoding("UTF-8");
            response.reset(); 

            String fileName = URLEncoder.encode(fileInfo[2].toString(), "utf-8");
            String extension = FilenameUtils.getExtension(fileName);
            boolean isJPG = StringUtils.isBlank(extension) || extension.equalsIgnoreCase("jpg");
            extension = isJPG ? "jpeg" : extension;

            //这一步很重要,如果不设置预览正常,但是前天FileReader.src操作后就有问题。
            response.setContentType("image/".concat(extension).concat(";charset=UTF-8"));
            response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");

            byte[] fileBytes = consoleService.downLoadFile(fileInfo[3].toString());
            ServletOutputStream os = response.getOutputStream();
            os.write(fileBytes);
            os.flush();
            os.close();

代码经过删除,看以下三部即可
response.setCharacterEncoding(“UTF-8”);
response.reset();
//这一步很重要,如果不设置预览正常,但是前天FileReader.src操作后就有问题。
response.setContentType(“image/jpeg;charset=UTF-8”);
response.setHeader(“Content-Disposition”, “inline; filename=\”” + fileName + “\”“);

注意 setContentType不要设置 image/* 表面上看没有问题,有时候会有问题。我就遇到这个坑

你可能感兴趣的:(java)