springMVC返回图片

/**
     * 回显图片
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("show.do")
    public boolean show(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取传入参数
    	String attachId = request.getParameter("attachId");
        //查询文件详情
    	Attach attach = fileService.getAttachById(Long.parseLong(attachId));

    	File file = new File(attach.getFilePath());
    	if(!file.exists()) {
    		return false;
    	}
    	FileInputStream inputStream = new FileInputStream(file);
    	byte[] data = new byte[(int)file.length()];
    	inputStream.read(data);
    	inputStream.close();
    	response.setContentType("image/png");
        OutputStream os = response.getOutputStream();
        os.write(data);
        os.flush();
        os.close();
        return true;
    }

猜你喜欢

转载自blog.csdn.net/weixin_40562026/article/details/84588795