Solved

Convert BufferedReader to File

Posted on 2016-08-26
1
118 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 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 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