2015-09-04 60 views
-1

我這是寫在春季4.1休息控制器,請參考下面的代碼如何使用休息呼叫顯示圖像前端?

@RequestMapping(value = "/image", method = RequestMethod.GET) 
    @ResponseBody 
    public ResponseEntity<InputStreamResource> downloadUserAvatarImage() { 
     GridFSDBFile gridFsFile = App.getImageResponse(); 

     return ResponseEntity.ok() 
       .contentLength(gridFsFile.getLength()) 
       .contentType(MediaType.parseMediaType(gridFsFile.getContentType())) 
       .body(new InputStreamResource(gridFsFile.getInputStream())); 
    } 

我現在的問題是,我有HTML塊當我加載 http://host.com/title 標題HTML頁面的下面貼

頁面
<html> 
    <body> 
     <h1>Host.com some title</h1> 
     <p>Some content</p> 
     <div id='loadImage'></div> 
     ... 
     some content 
     ... 
    </body> 
</html> 

我想打電話給我休息控制器加載在DIV由ID loadImage提到的圖像說http://www.host.com/getMyImage

我該怎麼做?

回答

1

爲什麼JQuery在第一位?

由於您的響應返回圖像,你可以直接把它的URL http://www.host.com/getMyImage

<html> 
     <body> 
      <h1>Host.com some title</h1> 
      <p>Some content</p> 
      <div id='loadImage'> 
       <img src="http://www.host.com/getMyImage"> 
      </div> 
      ... 
      some content 
      ... 
     </body> 
    </html>