0

我正在尝试首先从具有基本身份验证的服务器 A 下载 PDF,然后将 PDF 下载到服务器后,在浏览器插件可以读取的新窗口/选项卡中将其显示给用户。我正在尝试这样做,以便我不只是以纯文本形式发送用户名和密码。我想如果我先下载到服务器,我可以隐藏用户名和密码,然后将文件显示给用户。到目前为止,这是我的代码,它不起作用。什么都没发生。

//Get URL For The PDF File
    StringBuilder parameters = new StringBuilder();
    parameters.append("api=getDocument");
    parameters.append("&documentType=report");
    parameters.append("&documentID=").append(selectedReport);

    URL url;
    HttpURLConnection connection = null;
    try {
        //Create connection
        url = new URL(apiURL);

        //Create connection
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        BASE64Encoder enc = new sun.misc.BASE64Encoder();
        String userpassword = userName + ":" + password;
        String encodedAuthorization = enc.encode(userpassword.getBytes());
        connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(URLEncoder.encode(parameters.toString(), "UTF-8").getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(parameters.toString());
        wr.flush();
        wr.close();

        //Get Response  
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        //Convert to Byte Array
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
        while ((nRead = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();

        // Prepare.
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
        BufferedOutputStream output = null;
        int DEFAULT_BUFFER_SIZE = 10240;

        // Init servlet response.            
        response.reset();
        response.setHeader("Content-Type", "application/pdf");
        response.setHeader("Content-Disposition", "inline; filename=\"" + "report.pdf" + "\"");
        output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

        // Write file contents to response.
        byte[] buff = buffer.toByteArray();
        int length;
        while ((length = is.read(buff)) > 0) {
            output.write(buff, 0, length);
        }

        // Finalize task.
        output.flush();

        //Keep The Below         
        rd.close();

        facesContext.responseComplete();

    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
4

0 回答 0