title: Empty PDF on REST API POST
body: <p>I'm attempting to upload a (non-empty) PDF to Confluence using the Atlassian REST API (in Java). The issue I am having is that the PDF that ends up being uploaded has no content, but is exactly the same file size as the original PDF document!</p>
  
  <p>Here is the code that I am using to write the contents of the request:</p>
  
  <pre><code>private String generateUploadContent( final ByteArrayInputStream is,
                                        final String fileName,
                                        final String boundary )
                                            throws Exception
  {
      final String newLine = "\r\n";
      final StringWriter writer = new StringWriter();
  
      writer.write( "--" + boundary + newLine );
      writer.write(
          "Content-Disposition: form-data; name=\"file\"; filename=\"" +
              fileName + "\"" + newLine );
      if( fileName.endsWith( ".pdf" ) )
      {
          writer.write( "Content-Type: application/pdf" + newLine );
      }
      else
      {
          writer.write( "Content-Type: text/plain" + newLine );
      }
      writer.write( "Content-Transfer-Encoding: binary" + newLine + newLine );
  
      IOUtils.copy( is, writer );
  
      writer.write( newLine );
      writer.write( "--" + boundary + "--" + newLine );
  
      return writer.toString();
  }
  </code></pre>
  
  <p>The ByteArrayInputStream 'is' is the input stream of the file that I'm attempting to upload. This code works perfectly for plain text files, just not PDFs.</p>
  
  <p>If I do the following, the resulting file on my system is a PDF with all of the expected content, so I know the input stream is fine:</p>
  
  <pre><code>final File file = new File( "C:/file.pdf" );
  final FileOutputStream fos = new FileOutputStream( file );
  
  IOUtils.copy( is, fos );
  </code></pre>
  
  <p>The following is a complete list of headers that I am also sending with the content generated above as part of the request:</p>
  
  <pre><code>Authorization: Basic &lt;!AUTH STUFF!&gt;
  X-Atlassian-Token: no-check
  Content-Type: multipart/form-data; boundary=&lt;!BOUNDARY!&gt;
  </code></pre>
  
  <p>Based on my research so far (which has been quite extensive!), the common suggestions seem to be related to:</p>
  
  <ul>
  <li>Using the "Cache-Control: no-cache" header</li>
  <li>Providing/not providing the "Content-Length" header</li>
  <li>Something to do with the encoding of the PDF contents when it is converted to a String (apparently the equal file size points to this being a problem)</li>
  </ul>
  
  <p>I have only really had a good crack at the last solution above, but with no success.</p>
  
  <pre><code>IOUtils.copy( is, writer, StandardCharsets.UTF_8 );
  </code></pre>
  
  <p>The other two solutions didn't have much success in any of the forums that I read them in, so I haven't attempted them.</p>
  
  <p>If anyone has an alternative solution, or can provide greater insight into any of the provided solutions, I would be very appreciative. I have spent too long trying to figure this one out on my own now.</p>
  
  <p>If it helps, here is the Atlassian REST API reference that I have based my code off: <a href="https://docs.atlassian.com/atlassian-confluence/REST/latest-server/#content/%7Bid%7D/child/attachment-createAttachments" rel="nofollow noreferrer">https://docs.atlassian.com/atlassian-confluence/REST/latest-server/#content/{id}/child/attachment-createAttachments</a></p>
  
  <p>I have tried using the suggested cURL command to upload the same file to the same page and the file is uploaded fine and contains the expected contents, its just my Java code that seems to be going wrong somewhere.</p>
  
  <p>Thanks in advance,<br/>
  Adam</p>
  
  <p>EDIT: My unsuccessful attempt at base64 encoding:</p>
  
  <pre><code>if( fileName.endsWith( ".pdf" ) )
  {
      writer.write( "Content-Type: application/pdf" + newLine );
      writer.write( "Content-Transfer-Encoding: base64" + newLine + newLine );
  
      final byte[] base64Bytes = Base64.encodeBase64( IOUtils.toByteArray( is ) );
      final String base64String = new String( base64Bytes );
  
      writer.write( base64String );
  }
  else
  {
      writer.write( "Content-Type: text/plain" + newLine );
      writer.write( "Content-Transfer-Encoding: binary" + newLine + newLine );
  
      IOUtils.copy( is, writer );
  }
  
  writer.write( newLine );
  writer.write( "--" + boundary + "--" + newLine );
  </code></pre>