title: How to download bytea column as file using Java
body: <p>I want to download files stored in bytea format using java. I don't have superuser privileges. Using the code below I download the hex encoded file and convert it to pdf but the converted pdf is damaged whereas if I copy using \copy function(cannot use in java) via terminal, downloading process works smoothly.</p>
  
  <pre><code>        String sql = "(SELECT encode(f,'hex') FROM test_pdf where id='2' LIMIT 1)";
          System.out.println(sql);
  
          CopyManager copyManager = new CopyManager((BaseConnection) conn);
  
          FileWriter filew = new FileWriter("/home/sourabh/image.hex");
          copyManager.copyOut("COPY "+sql+"  TO STDOUT ", filew );`
  </code></pre>
  
  <p>And then : </p>
  
  <pre><code>xxd -p -r image.hex &gt; image.pdf
  </code></pre>