Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes I/O and Streams and the fly likes Save pdf from http inputstream to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Save pdf from http inputstream to file" Watch "Save pdf from http inputstream to file" New topic

Save pdf from http inputstream to file

DN Debed

Joined: Nov 03, 2008
Posts: 10
Hi, i'd like to ask how can I save the pdf file being sent from an HttpUrlConnection to a file?

Im using an HttpURLConnection to call a report from ms sql server report server. This should return a pdf file.

Then upon checking the getInputStream(), and writing the stream as string, I can tell that the contents are the pdf file itself(although it prints out probably hex values).

Normally, if it is run in a web application, it displays the pdf automatically.

But im running the code in a desktop environment (probably command line only). So what i need is to get the inputstream of the httpurlconnection, and save it to its PDF file.

Other approach is welcomed also... thank you!
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
This should help:
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Basically you just need to write it to FileOutputStream.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

... without converting it to a String. Read into a byte[], and save the byte[] to a FileOutputStream.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Save pdf from http inputstream to file
It's not a secret anymore!