Skip to Content
author's profile photo Former Member
Former Member

Forcing PDF Download/Stream Error

Hello everyone,

I am currently writing an iView that forces the download

of a PDF file instead of opening it by default in the

users browser. I set content type and content disposition

and then try and stream the pdf. It prompts me for download,

as it should, and i can save the file. Once it's downloaded though, it can't open the file in the adobe

Reader and says the file is damaged. I tried opening

it in notepad and the contents of the file were as

follows:

"Error Streaming the Data"

I have included the two pieces of code i have tried below

anyone got any clues as to why it isn't working?

	public void doOnNodeReady(IPortalComponentRequest request, IEvent event)
	{		
		IPortalComponentProfile userProfile = request.getComponentContext().getProfile();
		HttpServletResponse response = request.getServletResponse(true);
			  
		//Retrieve values from properties file
		docURL = userProfile.getProperty("docURL");
		
		file = request.getParameter("rid");

		//set the content type(can be excel/word/powerpoint etc..)
		response.setContentType ("application/pdf");
		
		//get the file name
		String name = file.substring(file.lastIndexOf("/") + 1,file.length());
		
		//set the header and also the Name by which user will be prompted to save
		response.setHeader ("Content-Disposition", "attachment; filename="" + name + """);
	
	
		String ErrorStr = null;
		try{
			  //find the right MIME type and set it as contenttype
			  PrintWriter outstr = new PrintWriter(response.getOutputStream());
			  InputStream in = null;       
			   try{
				   URL url	= new URL(URLEncoder.encode("http://mydomain.goes.here" + file));
				   URLConnection urlc= url.openConnection();
				   int length = urlc.getContentLength();
				   in = urlc.getInputStream();  
				   response.setContentLength(length);
				   int ch;
				   while ( (ch = in.read()) != -1 ) {
					outstr.print( (char)ch );
				   }
				} catch (Exception e) {
						e.printStackTrace();
						ErrorStr = "Error Streaming the Data";
						outstr.print(ErrorStr);
				} finally {
						if( in != null ) {
						  in.close();
						}
						if( outstr != null ) {
						  outstr.flush();
						  outstr.close();
						}
				}  
			  }
			  catch(Exception e){
					  e.printStackTrace();
			  }

	
		}

	public void doOnNodeReady(IPortalComponentRequest request, IEvent event)
	{		
		IPortalComponentProfile userProfile = request.getComponentContext().getProfile();
		HttpServletResponse response = request.getServletResponse(true);
			  
		//Retrieve values from properties file
		docURL = userProfile.getProperty("docURL");
		
		file = request.getParameter("rid");
		
		
		//set the content type(can be excel/word/powerpoint etc..)
		response.setContentType ("application/pdf");
		
		//get the file name
		String name = file.substring(file.lastIndexOf("/") + 1,file.length());
		
		//set the header and also the Name by which user will be prompted to save
		response.setHeader ("Content-Disposition", "attachment; filename="" + name + """);

		//OPen an input stream to the file and post the file contents thru the 
		//servlet output stream to the client m/c
		
		int bit = 256;
		int i = 0;

		try
		{
			URL u = new URL("http://tvms0019.ad.bgep.co.uk" + file);
			InputStream in = new FileInputStream(u.getFile());
			ServletOutputStream outs = response.getOutputStream();
			
			while ((bit) >= 0)
			{
				bit = in.read();
				outs.write(bit);
			}
		
			outs.flush();
			outs.close();
			in.close();	
		}
		catch (Exception ioe)
		{
			ioe.printStackTrace(System.out);
		}
	}

Note that i have tried both these pieces of code using

URL and File and neither work. I've tried printing

some debugging messages and the file path is correct.

Thanks,

Alessandro

 
 
 

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Sep 14, 2005 at 09:58 AM

    Amendment, changed the code. Now i get a 401 http error,

    which essentially is an Unauthorised access error. How

    do i fix this?

    I am currently runnign these off the EP5 server and i

    am logged in with a valid account. How come i am not

    authorised to download files off my own server? however,

    when i right click on a link and select "Save target" it

    works fine!

    Thanks,

    Alessandro

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 01, 2005 at 08:02 AM

    Hi,

    I had solving your Problem, but I have an other Problem:

    The Bug is in the the line

    PrintWriter outstr = new PrintWriter(response.getOutputStream());

    I become the equal Error with this line .

    Replace it

    PrintWriter outstr = response.getWriter();

    My other Problem.

    If I read the File (pdf) character-for-character from the FileInputStream and write the integer from the charater in the printWriter (for example 226(int) as byte E2). After than, I open the downloaded file and show it in the editor. But the charater is an other (for example 195(int) as byte C3).

    Why that???

    I become bugs by the pictures in the pdf-file.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 01, 2005 at 10:50 AM

    i'm using a code looking like your second method.

    However the part copying the input stream to the output stream can fail, since you write the -1 value to the output stream.

    I'm using the following code :

    InputStream         is = new FileInputStream(...);
    ServletOutputStream os = res.getOutputStream();
    boolean             ok = true;
    while (ok) {
       int input = is.read();
       if (input == -1) {
          ok = false;
       } else {
          os.write(input);
       }
    }
    is.close();
    os.close();
    

    There is a blog on the subject. The only modification you have to to (and you already have made them in your code) is to change the headers to set the file as en attachement.

    Regards

    Guillaume

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.