0

我想在 backing bean 方法中读取一个 PDF 报告文件作为流,这个 PDF 文件由 servlet 生成。整个框架用 JSF 1 和 spring security 2 实现。

我尝试使用 HttpURLConnection 调用此报告,但 Spring Security 将其视为未经身份验证的新请求,因此跳转到登录页面而不是生成 PDF 报告。

例子

String urlParameters = "reportsMenu="
            + URLEncoder.encode("146", "UTF-8") + "&fromDate=18/07/2013"
            + "&toDate=18/07/2013" + "&report_format=PDF"
            + "&cardAcct=55";
    System.out.println("1");
    URL url;
    HttpURLConnection connection = null;
    try {
        // Create connection/jasperReport/email_report
        System.out.println("2");
        url = new URL(
                "http://110.110.110.14:8080/web/jasperReport/reports");
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        String userpass = "username" + ":" + "username";
        String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());

        connection.setRequestProperty ("Authorization", basicAuth);

        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        System.out.println("3");
        connection.setRequestProperty("Content-Length", ""
                + Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");
        System.out.println("4");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        System.out.println("5");
        // Send request
        DataOutputStream wr = new DataOutputStream(connection
                .getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();
        System.out.println("6");
        // Get Response
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
            System.out.println(":::::: " + line);
        }

有什么建议吗?

4

0 回答 0