使用Tableau REST API取得缩略图1:取得缩略图

相关的API说明:https://help.tableau.com/v2020.1/api/rest_api/en-us/REST/rest_api_ref_workbooksviews.htm#query_view_with_preview

首先在RestUri类里面加一个Operation:

QUERY_WORKBOOK_PREVIEWIMAGE(getApiUriBuilder().path("sites/{siteId}/workbooks/{workbookId}/previewImage")), 

然后再在处理的类(这里示例的类名为mapReport)写一个获取图片数据流的方法:

//返回指定工作薄的缩略图数据流
	public InputStream getWorkbookPreviewImagesStream(String workbookID)
	{
		InputStream inputStream=null;
		try
		{
			credential=auth.login();			
			if(credential!=null)
			{				
				s_restApiUtils=auth.getRestApi();
				String siteId=credential.getSite().getId();				
				
				String url=RestUri.Operation.QUERY_WORKBOOK_PREVIEWIMAGE.getUrl(siteId,workbookID);						
				inputStream = s_restApiUtils.getFile(url,credential.getToken());
			}
		}
		catch(Exception e)
		{

		}
		return inputStream;
	}

在Controller(这里类名为ReportController)里面写一个输出图片流的方法

//*-----------------------------------------------------------
	//*
	//*                    返回指定工作簿的缩略图数据流
	//*
	//*-----------------------------------------------------------
	@RequestMapping(value="/report/getWorkbookPreviewImagesStream",produces="image/png")
	@ResponseBody
	public void getWorkbookPreviewImagesStream(HttpServletRequest request,HttpServletResponse response) throws Exception,IOException
	{		
		String workbookID=(String)request.getParameter("workbookID");			
		
		mapReport report=new mapReport();
		InputStream inputStream= report.getWorkbookPreviewImagesStream(workbookID);
				
		if(inputStream!=null)
		{
			OutputStream outstream = response.getOutputStream();
			
			int bytesRead = 0;
			byte[] buffer = new byte[1024];
			while ((bytesRead = inputStream.read(buffer, 0, 1024)) != -1) 
			{
				outstream.write(buffer, 0, bytesRead);
			}
			response.setContentType("image/png");	
			inputStream.close();
			outstream.flush();
			outstream.close();
		}
	}

前端JS直接将缩略图加载到图片对象

var url="/report/getWorkbookPreviewImagesStream?workbookID="+row.workbookid;	
	var xhr = new XMLHttpRequest();    
	xhr.open("get", url, true);
	xhr.responseType = "blob";
	xhr.onload = function() {
		if (this.status == 200) {
			var blob = this.response;       
			blobToDataURI(blob, function (data) {
				$("#reportPreview").attr("src",data);
			});
		} 
	}
	xhr.send();

这样中间全部是数据流,不会有临时文件什么的还要清理。

你可能感兴趣的:(编程开发,商业智能,大数据与数据可视化)