?
Solved

Convert BufferedReader to File

Posted on 2016-08-26
1
Medium Priority
?
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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