title: (spring boot or java) I have a problem opening URL PDF
body: <p>spring boot or java <strong>read/open</strong> pdf url and ResponseEntity attachment file .pdf</p>
  <ol>
  <li>Call the URL <a href="https://xxxxx.xxx/file.pdf" rel="nofollow noreferrer">https://xxxxx.xxx/file.pdf</a></li>
  <li>Read the file from step 1 and display it. By setting the response value as follows:</li>
  </ol>
  <pre><code>Content-Type : application/pdf
  Content-Transfer-Encoding : binary
  Content-disposition : attachment; filename=filename.pdf
  Content-Length : xxxx
  </code></pre>
  <pre><code>URL url = new URL(apiReportDomain
                  + &quot;/rest_v2/reports/reports/cms/loan_emergency/v1_0/RTP0003_02.pdf?i_ref_code=&quot; + documentId);
          System.out.println(url);
          String encoding = Base64.getEncoder().encodeToString(
                  (apiReportUsername + &quot;:&quot; + apiReportPassword).getBytes(StandardCharsets.UTF_8));
  
          HttpURLConnection connectionApi = (HttpURLConnection) url.openConnection();
          connectionApi.setRequestMethod(&quot;GET&quot;);
          connectionApi.setDoOutput(true);
          connectionApi.setRequestProperty(&quot;Authorization&quot;, &quot;Basic &quot; + encoding);
          connectionApi.setRequestProperty(&quot;Content-Type&quot;, &quot;application/pdf&quot;);
          InputStream content = connectionApi.getInputStream();
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(content));
                  
          StringBuilder sb = new StringBuilder();
          int cp;
          while ((cp = in.read()) != -1) {
              sb.append((char) cp);
          }
  
          byte[] output = sb.toString().getBytes();
  
          HttpHeaders responseHeaders = new HttpHeaders();
          responseHeaders.set(&quot;charset&quot;, &quot;utf-8&quot;);
          responseHeaders.setContentType(MediaType.valueOf(&quot;application/pdf&quot;));
          responseHeaders.setContentLength(output.length);
          responseHeaders.set(&quot;Content-disposition&quot;, &quot;attachment; filename=filename.pdf&quot;);
  
          return new ResponseEntity&lt;byte[]&gt;(output, responseHeaders, HttpStatus.OK);
  </code></pre>
  <p><a href="https://i.stack.imgur.com/ajS1A.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  <p>which the result i got is a blank page But in fact, this PDF contains a full sheet of text.</p>