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;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章