Solved

Convert BufferedReader to File

Posted on 2016-08-26
1
62 Views
Last Modified: 2016-08-27
I have a servlet which returns a PDF document to the requesting user. When it is called from a browser, the PDF document is automatically downloaded.
Now I need to call this servlet from Java (standalone code, not server side). Once this Java code gets the PDF document, it needs to convert it to images using Apache PDF Box API.

This is how my Java code looks like. It writes bytes to the screen.

URL url = new URL("http://localhost:8080/Stream/Document&type=pdf");
           BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
	   String line = in.readLine();
	   System.out.println( line );	
	   in.close(); 

Open in new window

                 
                  
and this is how the code to convert it to Images looks like

String pdfFileName = "myfile.pdf";
PDDocument document = PDDocument.load(new File(pdfFileName)); 

Open in new window

                 


Now I need to combine these two so that the PDF file that is downloaded (in memory, would prefer not to write it to Filesystem) is converted to JPG. So, instead of passing a hardcoded filename, I somehow need to convert the BufferedReader object to File object and pass it as an input to PDDocument.

I am not able to make out how this conversion would happen.

Thank you.
0
Comment
Question by:ank5
1 Comment
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 

Open in new window

You can't read a binary (pdf) file like that. Get it like this
InputStream in = url.openStream(); 

Open in new window

and copy it with this
http://technojeeves.com/index.php/aliasjava1/12-copying-streams
(if you need a file)
If you don't, you can just do
PDDocument document = PDDocument.load(in);

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now