Java后端响应出来的图片流在HTML中显示

 var logoName = $("#materPicLogo").val();
    if (logoName == "") {
        layer.msg("当前没有选择设置主图LOGO");
        return false;
    }
 var img = ''<div style="margin: 0 auto;width: 80%"><img width="100%" src="http://localhost/users/viewLogo?logoName='' + logoName + ''"/></div>'';
 /**
     * 返回一个图片流到前端显示
     *
     * @param logoName
     * @param map
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "viewLogo", method = RequestMethod.GET)
    public void viewLogo(@RequestParam("logoName") String logoName, Map<String, Object> map, HttpServletResponse response) throws IOException {
        AdminUser adminUser = (AdminUser) map.get("adminUser");
        //图片的绝对路径
        String picPath = new StringBuffer(logosAdminPath).append("/")
                .append(adminUser.getAdminUserNick()).append("/")
                .append(logoName).toString();
        String picType = logoName.substring(logoName.lastIndexOf(".") + 1, logoName.length());
        File file = new File(picPath);
        ByteArrayOutputStream os = null;
        try {
            BufferedImage read = ImageIO.read(file);
            os = new ByteArrayOutputStream();
            ServletOutputStream fout = response.getOutputStream();
            response.setHeader("Content-Type", "image/jped");
            ImageIO.write(read, picType, fout);
            fout.close();
        } catch (IOException e) {
            logger.error("viewLogo IOException:{}", e);
        } finally {
            if (os != null) os.close();
        }
    }

你可能感兴趣的:(Java,javaScript,java,js)