Pentaho BIServer 调用API根据指定路径下载文件

Pentaho官网文档:https://help.pentaho.com/Documentation/8.2/Developer_Center/Embed_Pentaho_Server

api : /pentaho/api/repos/【path】/generatedContent

这里【path】的格式需要注意一下
 

public class HttpUtils {

    public static OutputStream doPostStream(String url, Map<String, String> params, int timeout){
    	
    	OutputStream os = null;
    	CloseableHttpClient httpClient =  HttpClients.createDefault();
    	try{
 
	        //超时设置
            RequestConfig requestConfig =  RequestConfig.custom().setConnectTimeout(timeout) //连接超时
	                .setConnectionRequestTimeout(timeout)//请求超时
	                .setSocketTimeout(timeout)
	                .setRedirectsEnabled(true)  //允许自动重定向
	                .build();
            HttpPost httpPost  = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            httpPost.addHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
            Map<String, String> headers = new HashMap<> ();
            headers.put("Authorization", "Basic " + Base64.encodeBase64String("Admin:password".getBytes()));
            for (Map.Entry<String, String> e : headers.entrySet()) {
                httpPost.addHeader(e.getKey(), e.getValue());
            }
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
            Iterator iterator = params.entrySet().iterator();
            while(iterator.hasNext()){
                Entry<String,String> elem = (Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
            }
            if(list.size() > 0){
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, HTTP.UTF_8);
                httpPost.setEntity(entity);
            }
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            if(httpResponse.getStatusLine().getStatusCode() == 200){
            	InputStream in = httpEntity.getContent();
            	os = new ByteArrayOutputStream();
            	int temp = 0;
            	while ((temp = in.read()) != -1) {
             		os.write(temp);
            	}
                os.flush();
                os.close();
                EntityUtils.consume(httpEntity);
                return os;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                httpClient.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

}

测试用例:将文件通过浏览器下载下来

@RequestMapping("/test")
public void test (HttpServletResponse response, HttpServletRequest request) throws IOException {
        OutputStream out = response.getOutputStream();
        Map<String, String> param = new HashMap<> ();
        // 这里格式参考官方文档,根据自己需要下载的文件格式传参
//		obj1.put("output-target", "table/html;page-mode=page");
        param.put("output-target", "pageable/pdf");
        
        OutputStream os=HttpUtils.doPostStream("http://192.168.129.138:8080/pentaho/api/repos/:home:leo:test1.prpt/generatedContent", param, 2000);
        String finalFileName = "123";
        final String userAgent = request.getHeader("USER-AGENT");

        if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent,"Trident")){//IE浏览器
            finalFileName = URLEncoder.encode(finalFileName,"UTF8");
        }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
            finalFileName = new String(finalFileName.getBytes(), "ISO8859-1");
        }else{
            finalFileName = URLEncoder.encode(finalFileName,"UTF8");//其他浏览器
        }

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "attachment;fileName=" + finalFileName + ".pdf");// 设置文件名

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos=(ByteArrayOutputStream) os;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
		
        int temp = 0;
        while ((temp = swapStream.read()) != -1) {
            out.write(temp);
        }
        os.flush();
        os.close();
}

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值