页面从后台接口获取数据流显示照片

作者:Fasure_Smile

JSP页面

页面上接收数据流可以用<a>标签的“href”属性接收,也可以用<img>标签的“src”属性接收。

<a href="${ctx}/identify/img?imgId=${identify.img}" target="_blank">查看详情</a>   (target="_blank",在新的标签页中打开)  

接口:

@RequestMapping("img")  
public void getImg(HttpServletRequest request, HttpServletResponse response) throws Exception{  
      
    //照片存储在网盘中,获取页面传来的照片ID  
    String id = request.getParameter("imgId");  
    //配置文件取url和身份验证token  
    String url = ConfigReader.getProperty("CLOUDDISK_DOWNLOAD_URL");  
    String token = ConfigReader.getProperty("CLOUDDISK_AUTHCODE");  
    String param = "authCode=" + token + "&id=" + id;  
    //从网盘下载照片的流  
    InputStream in = getImgStream(url,param,"GET");  
      
    //声明输出流,但是要从HttpServletResponse中获取才管用  
    BufferedOutputStream bout = new BufferedOutputStream(response.getOutputStream());  
    //从输入流到输出流  
    try {    
        byte b[] = new byte[1024];    
        int len = in.read(b);    
        while (len > 0) {    
            bout.write(b, 0, len);    
            len = in.read(b);    
        }    
    } catch (Exception e) {    
        throw e;     
    } finally {    
        bout.close();    
        in.close();    
    }  
      
}  
posted @ 2017-08-30 09:38  李慕白520  阅读(703)  评论(0编辑  收藏  举报