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

  • 时间:
  • 来源:互联网

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

api : /pentaho/api/repos//generatedContent

这里的格式需要注意一下

代码如下:

工具类:

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();
	}
雾饮
发布了8 篇原创文章 · 获赞 0 · 访问量 1377
私信 关注

本文链接http://element-ui.cn/news/show-1641.html