Solved

Convert BufferedReader to File

Posted on 2016-08-26
1
75 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
ID: 41771596
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 79
Requested array size exceeds VM limit 3 84
Error trying to install RTMT Win7 5 41
netstat -ano | find "8000" and taskkill /f /pid 2984 3 35
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

910 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

16 Experts available now in Live!

Get 1:1 Help Now