You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Steve Vanspall <st...@crmsoftware.com.au> on 2005/05/04 15:29:59 UTC
Serving files using tomcat
Hi,
I have been looking around and haven't found a solution that works
basically I have a PDF that gets created dynamically. Now to save memory I have the PDF written to a file rather than a ByteArray. The only way I can be sure that I wont encounter errors creating the file is to use File.createTempFile. The creation goes of ok. And I have checked the file itself and the PDF looks great.
How do i now serve this to the user who has requested it. If I try to write it to the response (using the same method I use to creare dynamic image, this works), it just shows up a blank screen.
The problem also is, even if it did show the PDF, acrobat, to my understand will read only chunks of the stream and will go pack to get more. Thisis a problem because there is nothing to go back for.
So the point,
If I can just redirect the browser to a file in the tomcat temp directory (can I do that, will the use have access to that directory), then how do I translate the location of the temp directory to a url that is accesible outside.
If not then what other suggestions can people give me.
Thanks in advance
Steve
Re: Serving files using tomcat
Posted by Dakota Jack <da...@gmail.com>.
If you are using struts, you should be forwarding a null. That is
probably your problem.
On 5/4/05, Steve Vanspall <st...@crmsoftware.com.au> wrote:
> Unfortunately that is what I do
>
> OutputStream dos = null;
> FileInputStream fis = null;
> try
> {
> fis = new FileInputStream(rf.getPdf());
> response.setContentType("application/pdf");
> response.setContentLength((int) rf.getPdf().length());
> //response.setHeader(response.)
> dos = response.getOutputStream();
>
> int read = -1;
> byte[] bytes = new byte[100000];
> while((read = fis.read(bytes)) != -1)
> dos.write(bytes, 0, read);
> dos.flush();
> return mapping.findForward("PDF");
> } catch (Exception e)
> {
> // TODO Auto-generated catch block
> if(e instanceof SocketException)
> return mapping.findForward("reload");
> throw new IOException(e.toString());
> }
> finally
> {
>
> if(dos != null)
> dos.close();
> if(fis != null)
> fis.close();
>
> }
>
> Acrobat now loads but the PDF doesn't appear.
>
> Probably worth mentioning that I use struts, so I forward to a blank page
> with the content type set to application/pdf, maybe that is the problem, but
> not sure what else to do with the return.
>
> When I do the same thing with a dynamic image and forward to a page with a
> jpg content type, the image appears without a problem.
>
> Steve
> ----- Original Message -----
> From: "Anhony" <an...@comcast.net>
> To: "Tomcat Users List" <to...@jakarta.apache.org>
> Sent: Thursday, May 05, 2005 1:02 AM
> Subject: Re: Serving files using tomcat
>
> > Greetings,
> >
> > Take a look at the code fragment below. It should serve as a good starting
> > point.
> > I hope this helps.
> >
> > AS-
> >
> > private void processPDFRequest(HttpServletRequest request,
> > HttpServletResponse response) throws ServletException, IOException,
> > Exception
> > {
> > int bytesCopied = 0;
> >
> > FileInputStream fin = null;
> > OutputStream out = null;
> >
> > String fileAddress = "The fully qualified path to your PDF file";
> > if( fileAddress == null )
> > return;
> >
> > int ext = fileAddress.lastIndexOf( '.' );
> > if( ext != -1 )
> > {
> > ext = fileAddress.substring( ext+1,
> > fileAddress.length() ).toLowerCase();
> >
> > if( ext == "pdf" )
> > response.setContentType("application/pdf");
> > else
> > "Do whatever you think best to do"
> > }
> > else
> > "Do whatever you think best to do"
> >
> > try
> > {
> > out = response.getOutputStream();
> > fin = new FileInputStream( fileAddress );
> > bytesCopied = StreamCopier.copy( fin, out );
> > }
> > finally
> > {
> > if( fin != null )
> > fin.close();
> > if( out != null )
> > {
> > out.flush();
> > out.close();
> > }
> > }
> > }
> >
> >
> > ----- Original Message -----
> > From: "Steve Vanspall" <st...@crmsoftware.com.au>
> > To: "Tomcat User List" <to...@jakarta.apache.org>
> > Sent: Wednesday, May 04, 2005 9:29 AM
> > Subject: Serving files using tomcat
> >
> >
> > Hi,
> >
> > I have been looking around and haven't found a solution that works
> >
> > basically I have a PDF that gets created dynamically. Now to save memory I
> > have the PDF written to a file rather than a ByteArray. The only way I can
> > be sure that I wont encounter errors creating the file is to use
> > File.createTempFile. The creation goes of ok. And I have checked the file
> > itself and the PDF looks great.
> >
> > How do i now serve this to the user who has requested it. If I try to
> write
> > it to the response (using the same method I use to creare dynamic image,
> > this works), it just shows up a blank screen.
> >
> > The problem also is, even if it did show the PDF, acrobat, to my
> understand
> > will read only chunks of the stream and will go pack to get more. Thisis a
> > problem because there is nothing to go back for.
> >
> > So the point,
> >
> > If I can just redirect the browser to a file in the tomcat temp directory
> > (can I do that, will the use have access to that directory), then how do I
> > translate the location of the temp directory to a url that is accesible
> > outside.
> >
> > If not then what other suggestions can people give me.
> >
> > Thanks in advance
> >
> > Steve
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
--
"You can lead a horse to water but you cannot make it float on its back."
~Dakota Jack~
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: Serving files using tomcat
Posted by Steve Vanspall <st...@crmsoftware.com.au>.
Yes i see no difference,
I assume StreamCopier.copy() just does what my code does. I cannot find it
in any of the standard jars, so I assume this is one of your own.
Other than that everything else seems to be fine.
Oh well I am sure I will owrk it out
Steve
----- Original Message -----
From: "Anhony" <an...@comcast.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Thursday, May 05, 2005 2:19 AM
Subject: Re: Serving files using tomcat
> I use this code and it works in my app. Their are small differences
between
> how we copy the data to the response output. I don't know for sure, but
this
> may account for why the fragment I posted works.
>
> The difference is small, I think it would be worth giving it a try.
>
> AS-
>
>
> ----- Original Message -----
> From: "Steve Vanspall" <st...@crmsoftware.com.au>
> To: "Tomcat Users List" <to...@jakarta.apache.org>
> Sent: Wednesday, May 04, 2005 11:47 AM
> Subject: Re: Serving files using tomcat
>
>
> > Unfortunately that is what I do
> >
> > OutputStream dos = null;
> > FileInputStream fis = null;
> > try
> > {
> > fis = new FileInputStream(rf.getPdf());
> > response.setContentType("application/pdf");
> > response.setContentLength((int) rf.getPdf().length());
> > //response.setHeader(response.)
> > dos = response.getOutputStream();
> >
> > int read = -1;
> > byte[] bytes = new byte[100000];
> > while((read = fis.read(bytes)) != -1)
> > dos.write(bytes, 0, read);
> > dos.flush();
> > return mapping.findForward("PDF");
> > } catch (Exception e)
> > {
> > // TODO Auto-generated catch block
> > if(e instanceof SocketException)
> > return mapping.findForward("reload");
> > throw new IOException(e.toString());
> > }
> > finally
> > {
> >
> > if(dos != null)
> > dos.close();
> > if(fis != null)
> > fis.close();
> >
> >
> > }
> >
> > Acrobat now loads but the PDF doesn't appear.
> >
> > Probably worth mentioning that I use struts, so I forward to a blank
page
> > with the content type set to application/pdf, maybe that is the problem,
> > but
> > not sure what else to do with the return.
> >
> > When I do the same thing with a dynamic image and forward to a page with
a
> > jpg content type, the image appears without a problem.
> >
> > Steve
> > ----- Original Message -----
> > From: "Anhony" <an...@comcast.net>
> > To: "Tomcat Users List" <to...@jakarta.apache.org>
> > Sent: Thursday, May 05, 2005 1:02 AM
> > Subject: Re: Serving files using tomcat
> >
> >
> >> Greetings,
> >>
> >> Take a look at the code fragment below. It should serve as a good
> >> starting
> >> point.
> >> I hope this helps.
> >>
> >> AS-
> >>
> >> private void processPDFRequest(HttpServletRequest request,
> >> HttpServletResponse response) throws ServletException, IOException,
> >> Exception
> >> {
> >> int bytesCopied = 0;
> >>
> >> FileInputStream fin = null;
> >> OutputStream out = null;
> >>
> >> String fileAddress = "The fully qualified path to your PDF
file";
> >> if( fileAddress == null )
> >> return;
> >>
> >> int ext = fileAddress.lastIndexOf( '.' );
> >> if( ext != -1 )
> >> {
> >> ext = fileAddress.substring( ext+1,
> >> fileAddress.length() ).toLowerCase();
> >>
> >> if( ext == "pdf" )
> >> response.setContentType("application/pdf");
> >> else
> >> "Do whatever you think best to do"
> >> }
> >> else
> >> "Do whatever you think best to do"
> >>
> >> try
> >> {
> >> out = response.getOutputStream();
> >> fin = new FileInputStream( fileAddress );
> >> bytesCopied = StreamCopier.copy( fin, out );
> >> }
> >> finally
> >> {
> >> if( fin != null )
> >> fin.close();
> >> if( out != null )
> >> {
> >> out.flush();
> >> out.close();
> >> }
> >> }
> >> }
> >>
> >>
> >> ----- Original Message -----
> >> From: "Steve Vanspall" <st...@crmsoftware.com.au>
> >> To: "Tomcat User List" <to...@jakarta.apache.org>
> >> Sent: Wednesday, May 04, 2005 9:29 AM
> >> Subject: Serving files using tomcat
> >>
> >>
> >> Hi,
> >>
> >> I have been looking around and haven't found a solution that works
> >>
> >> basically I have a PDF that gets created dynamically. Now to save
memory
> >> I
> >> have the PDF written to a file rather than a ByteArray. The only way I
> >> can
> >> be sure that I wont encounter errors creating the file is to use
> >> File.createTempFile. The creation goes of ok. And I have checked the
file
> >> itself and the PDF looks great.
> >>
> >> How do i now serve this to the user who has requested it. If I try to
> > write
> >> it to the response (using the same method I use to creare dynamic
image,
> >> this works), it just shows up a blank screen.
> >>
> >> The problem also is, even if it did show the PDF, acrobat, to my
> > understand
> >> will read only chunks of the stream and will go pack to get more.
Thisis
> >> a
> >> problem because there is nothing to go back for.
> >>
> >> So the point,
> >>
> >> If I can just redirect the browser to a file in the tomcat temp
directory
> >> (can I do that, will the use have access to that directory), then how
do
> >> I
> >> translate the location of the temp directory to a url that is accesible
> >> outside.
> >>
> >> If not then what other suggestions can people give me.
> >>
> >> Thanks in advance
> >>
> >> Steve
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> >> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >>
> >>
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: Serving files using tomcat
Posted by Steve Vanspall <st...@crmsoftware.com.au>.
Ok thanks,
Well I have worked it out,
it turns out I needed the header
response.setHeader("Content-disposition","attachment;
filename=\"report.pdf\"");
added, this made it work.
So now the problem id that Internet Explorer flashes up the Open, Save,
Cance dialog box twice. Once the second one flashes up the first dissapears
so I am not too concerned. But wondering if this is a quirk, or have I done
something wrong.
My code is now
fis = new FileInputStream(rf.getPdf());
response.setContentType("application/pdf");
response.setContentLength(FileUtils.countBytes(rf.getPdf())); // may
be overkill thought it may be misreporting the file length.
response.setHeader("Content-disposition","attachment;
filename=\"report.pdf\"");
dos = response.getOutputStream();
int read = -1;
byte[] bytes = new byte[100000];
while((read = fis.read(bytes)) != -1)
dos.write(bytes, 0, read);
dos.flush();
return null;
----- Original Message -----
From: "David B. Saul" <db...@comcast.net>
To: "'Tomcat Users List'" <to...@jakarta.apache.org>
Sent: Thursday, May 05, 2005 6:43 AM
Subject: RE: Serving files using tomcat
May not be critical but try using the ServletOutputStream instead of
OutputStream.
DOC URL:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletResponse.html
//Clear content of the underlying buffer in the response
//without clearing headers or status code.
response.resetBuffer();
response.setContentLength(output.length);
//Returns a ServletOutputStream suitable for writing binary data in the
response.
//The servlet container does not encode the binary data.
ServletOutputStream os = response.getOutputStream();
os.write(output);
os.close();
Additionally, append &pdf=.pdf\ to the URL.
-----Original Message-----
From: Anhony [mailto:anthonys123@comcast.net]
Sent: Wednesday, May 04, 2005 9:20 AM
To: Tomcat Users List
Subject: Re: Serving files using tomcat
I use this code and it works in my app. Their are small differences between
how we copy the data to the response output. I don't know for sure, but this
may account for why the fragment I posted works.
The difference is small, I think it would be worth giving it a try.
AS-
----- Original Message -----
From: "Steve Vanspall" <st...@crmsoftware.com.au>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Wednesday, May 04, 2005 11:47 AM
Subject: Re: Serving files using tomcat
> Unfortunately that is what I do
>
> OutputStream dos = null;
> FileInputStream fis = null;
> try
> {
> fis = new FileInputStream(rf.getPdf());
> response.setContentType("application/pdf");
> response.setContentLength((int) rf.getPdf().length());
> //response.setHeader(response.)
> dos = response.getOutputStream();
>
> int read = -1;
> byte[] bytes = new byte[100000];
> while((read = fis.read(bytes)) != -1)
> dos.write(bytes, 0, read);
> dos.flush();
> return mapping.findForward("PDF");
> } catch (Exception e)
> {
> // TODO Auto-generated catch block
> if(e instanceof SocketException)
> return mapping.findForward("reload");
> throw new IOException(e.toString());
> }
> finally
> {
>
> if(dos != null)
> dos.close();
> if(fis != null)
> fis.close();
>
>
> }
>
> Acrobat now loads but the PDF doesn't appear.
>
> Probably worth mentioning that I use struts, so I forward to a blank
> page with the content type set to application/pdf, maybe that is the
> problem, but not sure what else to do with the return.
>
> When I do the same thing with a dynamic image and forward to a page
> with a jpg content type, the image appears without a problem.
>
> Steve
> ----- Original Message -----
> From: "Anhony" <an...@comcast.net>
> To: "Tomcat Users List" <to...@jakarta.apache.org>
> Sent: Thursday, May 05, 2005 1:02 AM
> Subject: Re: Serving files using tomcat
>
>
>> Greetings,
>>
>> Take a look at the code fragment below. It should serve as a good
>> starting
>> point.
>> I hope this helps.
>>
>> AS-
>>
>> private void processPDFRequest(HttpServletRequest request,
>> HttpServletResponse response) throws ServletException, IOException,
>> Exception
>> {
>> int bytesCopied = 0;
>>
>> FileInputStream fin = null;
>> OutputStream out = null;
>>
>> String fileAddress = "The fully qualified path to your PDF file";
>> if( fileAddress == null )
>> return;
>>
>> int ext = fileAddress.lastIndexOf( '.' );
>> if( ext != -1 )
>> {
>> ext = fileAddress.substring( ext+1,
>> fileAddress.length() ).toLowerCase();
>>
>> if( ext == "pdf" )
>> response.setContentType("application/pdf");
>> else
>> "Do whatever you think best to do"
>> }
>> else
>> "Do whatever you think best to do"
>>
>> try
>> {
>> out = response.getOutputStream();
>> fin = new FileInputStream( fileAddress );
>> bytesCopied = StreamCopier.copy( fin, out );
>> }
>> finally
>> {
>> if( fin != null )
>> fin.close();
>> if( out != null )
>> {
>> out.flush();
>> out.close();
>> }
>> }
>> }
>>
>>
>> ----- Original Message -----
>> From: "Steve Vanspall" <st...@crmsoftware.com.au>
>> To: "Tomcat User List" <to...@jakarta.apache.org>
>> Sent: Wednesday, May 04, 2005 9:29 AM
>> Subject: Serving files using tomcat
>>
>>
>> Hi,
>>
>> I have been looking around and haven't found a solution that works
>>
>> basically I have a PDF that gets created dynamically. Now to save
>> memory
>> I
>> have the PDF written to a file rather than a ByteArray. The only way I
>> can
>> be sure that I wont encounter errors creating the file is to use
>> File.createTempFile. The creation goes of ok. And I have checked the file
>> itself and the PDF looks great.
>>
>> How do i now serve this to the user who has requested it. If I try to
> write
>> it to the response (using the same method I use to creare dynamic
>> image, this works), it just shows up a blank screen.
>>
>> The problem also is, even if it did show the PDF, acrobat, to my
> understand
>> will read only chunks of the stream and will go pack to get more.
>> Thisis
>> a
>> problem because there is nothing to go back for.
>>
>> So the point,
>>
>> If I can just redirect the browser to a file in the tomcat temp
>> directory (can I do that, will the use have access to that
>> directory), then how do I translate the location of the temp
>> directory to a url that is accesible outside.
>>
>> If not then what other suggestions can people give me.
>>
>> Thanks in advance
>>
>> Steve
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
RE: Serving files using tomcat
Posted by "David B. Saul" <db...@comcast.net>.
May not be critical but try using the ServletOutputStream instead of
OutputStream.
DOC URL:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletResponse.html
//Clear content of the underlying buffer in the response
//without clearing headers or status code.
response.resetBuffer();
response.setContentLength(output.length);
//Returns a ServletOutputStream suitable for writing binary data in the
response.
//The servlet container does not encode the binary data.
ServletOutputStream os = response.getOutputStream();
os.write(output);
os.close();
Additionally, append &pdf=.pdf\ to the URL.
-----Original Message-----
From: Anhony [mailto:anthonys123@comcast.net]
Sent: Wednesday, May 04, 2005 9:20 AM
To: Tomcat Users List
Subject: Re: Serving files using tomcat
I use this code and it works in my app. Their are small differences between
how we copy the data to the response output. I don't know for sure, but this
may account for why the fragment I posted works.
The difference is small, I think it would be worth giving it a try.
AS-
----- Original Message -----
From: "Steve Vanspall" <st...@crmsoftware.com.au>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Wednesday, May 04, 2005 11:47 AM
Subject: Re: Serving files using tomcat
> Unfortunately that is what I do
>
> OutputStream dos = null;
> FileInputStream fis = null;
> try
> {
> fis = new FileInputStream(rf.getPdf());
> response.setContentType("application/pdf");
> response.setContentLength((int) rf.getPdf().length());
> //response.setHeader(response.)
> dos = response.getOutputStream();
>
> int read = -1;
> byte[] bytes = new byte[100000];
> while((read = fis.read(bytes)) != -1)
> dos.write(bytes, 0, read);
> dos.flush();
> return mapping.findForward("PDF");
> } catch (Exception e)
> {
> // TODO Auto-generated catch block
> if(e instanceof SocketException)
> return mapping.findForward("reload");
> throw new IOException(e.toString());
> }
> finally
> {
>
> if(dos != null)
> dos.close();
> if(fis != null)
> fis.close();
>
>
> }
>
> Acrobat now loads but the PDF doesn't appear.
>
> Probably worth mentioning that I use struts, so I forward to a blank
> page with the content type set to application/pdf, maybe that is the
> problem, but not sure what else to do with the return.
>
> When I do the same thing with a dynamic image and forward to a page
> with a jpg content type, the image appears without a problem.
>
> Steve
> ----- Original Message -----
> From: "Anhony" <an...@comcast.net>
> To: "Tomcat Users List" <to...@jakarta.apache.org>
> Sent: Thursday, May 05, 2005 1:02 AM
> Subject: Re: Serving files using tomcat
>
>
>> Greetings,
>>
>> Take a look at the code fragment below. It should serve as a good
>> starting
>> point.
>> I hope this helps.
>>
>> AS-
>>
>> private void processPDFRequest(HttpServletRequest request,
>> HttpServletResponse response) throws ServletException, IOException,
>> Exception
>> {
>> int bytesCopied = 0;
>>
>> FileInputStream fin = null;
>> OutputStream out = null;
>>
>> String fileAddress = "The fully qualified path to your PDF file";
>> if( fileAddress == null )
>> return;
>>
>> int ext = fileAddress.lastIndexOf( '.' );
>> if( ext != -1 )
>> {
>> ext = fileAddress.substring( ext+1,
>> fileAddress.length() ).toLowerCase();
>>
>> if( ext == "pdf" )
>> response.setContentType("application/pdf");
>> else
>> "Do whatever you think best to do"
>> }
>> else
>> "Do whatever you think best to do"
>>
>> try
>> {
>> out = response.getOutputStream();
>> fin = new FileInputStream( fileAddress );
>> bytesCopied = StreamCopier.copy( fin, out );
>> }
>> finally
>> {
>> if( fin != null )
>> fin.close();
>> if( out != null )
>> {
>> out.flush();
>> out.close();
>> }
>> }
>> }
>>
>>
>> ----- Original Message -----
>> From: "Steve Vanspall" <st...@crmsoftware.com.au>
>> To: "Tomcat User List" <to...@jakarta.apache.org>
>> Sent: Wednesday, May 04, 2005 9:29 AM
>> Subject: Serving files using tomcat
>>
>>
>> Hi,
>>
>> I have been looking around and haven't found a solution that works
>>
>> basically I have a PDF that gets created dynamically. Now to save
>> memory
>> I
>> have the PDF written to a file rather than a ByteArray. The only way I
>> can
>> be sure that I wont encounter errors creating the file is to use
>> File.createTempFile. The creation goes of ok. And I have checked the file
>> itself and the PDF looks great.
>>
>> How do i now serve this to the user who has requested it. If I try to
> write
>> it to the response (using the same method I use to creare dynamic
>> image, this works), it just shows up a blank screen.
>>
>> The problem also is, even if it did show the PDF, acrobat, to my
> understand
>> will read only chunks of the stream and will go pack to get more.
>> Thisis
>> a
>> problem because there is nothing to go back for.
>>
>> So the point,
>>
>> If I can just redirect the browser to a file in the tomcat temp
>> directory (can I do that, will the use have access to that
>> directory), then how do I translate the location of the temp
>> directory to a url that is accesible outside.
>>
>> If not then what other suggestions can people give me.
>>
>> Thanks in advance
>>
>> Steve
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: Serving files using tomcat
Posted by Anhony <an...@comcast.net>.
I use this code and it works in my app. Their are small differences between
how we copy the data to the response output. I don't know for sure, but this
may account for why the fragment I posted works.
The difference is small, I think it would be worth giving it a try.
AS-
----- Original Message -----
From: "Steve Vanspall" <st...@crmsoftware.com.au>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Wednesday, May 04, 2005 11:47 AM
Subject: Re: Serving files using tomcat
> Unfortunately that is what I do
>
> OutputStream dos = null;
> FileInputStream fis = null;
> try
> {
> fis = new FileInputStream(rf.getPdf());
> response.setContentType("application/pdf");
> response.setContentLength((int) rf.getPdf().length());
> //response.setHeader(response.)
> dos = response.getOutputStream();
>
> int read = -1;
> byte[] bytes = new byte[100000];
> while((read = fis.read(bytes)) != -1)
> dos.write(bytes, 0, read);
> dos.flush();
> return mapping.findForward("PDF");
> } catch (Exception e)
> {
> // TODO Auto-generated catch block
> if(e instanceof SocketException)
> return mapping.findForward("reload");
> throw new IOException(e.toString());
> }
> finally
> {
>
> if(dos != null)
> dos.close();
> if(fis != null)
> fis.close();
>
>
> }
>
> Acrobat now loads but the PDF doesn't appear.
>
> Probably worth mentioning that I use struts, so I forward to a blank page
> with the content type set to application/pdf, maybe that is the problem,
> but
> not sure what else to do with the return.
>
> When I do the same thing with a dynamic image and forward to a page with a
> jpg content type, the image appears without a problem.
>
> Steve
> ----- Original Message -----
> From: "Anhony" <an...@comcast.net>
> To: "Tomcat Users List" <to...@jakarta.apache.org>
> Sent: Thursday, May 05, 2005 1:02 AM
> Subject: Re: Serving files using tomcat
>
>
>> Greetings,
>>
>> Take a look at the code fragment below. It should serve as a good
>> starting
>> point.
>> I hope this helps.
>>
>> AS-
>>
>> private void processPDFRequest(HttpServletRequest request,
>> HttpServletResponse response) throws ServletException, IOException,
>> Exception
>> {
>> int bytesCopied = 0;
>>
>> FileInputStream fin = null;
>> OutputStream out = null;
>>
>> String fileAddress = "The fully qualified path to your PDF file";
>> if( fileAddress == null )
>> return;
>>
>> int ext = fileAddress.lastIndexOf( '.' );
>> if( ext != -1 )
>> {
>> ext = fileAddress.substring( ext+1,
>> fileAddress.length() ).toLowerCase();
>>
>> if( ext == "pdf" )
>> response.setContentType("application/pdf");
>> else
>> "Do whatever you think best to do"
>> }
>> else
>> "Do whatever you think best to do"
>>
>> try
>> {
>> out = response.getOutputStream();
>> fin = new FileInputStream( fileAddress );
>> bytesCopied = StreamCopier.copy( fin, out );
>> }
>> finally
>> {
>> if( fin != null )
>> fin.close();
>> if( out != null )
>> {
>> out.flush();
>> out.close();
>> }
>> }
>> }
>>
>>
>> ----- Original Message -----
>> From: "Steve Vanspall" <st...@crmsoftware.com.au>
>> To: "Tomcat User List" <to...@jakarta.apache.org>
>> Sent: Wednesday, May 04, 2005 9:29 AM
>> Subject: Serving files using tomcat
>>
>>
>> Hi,
>>
>> I have been looking around and haven't found a solution that works
>>
>> basically I have a PDF that gets created dynamically. Now to save memory
>> I
>> have the PDF written to a file rather than a ByteArray. The only way I
>> can
>> be sure that I wont encounter errors creating the file is to use
>> File.createTempFile. The creation goes of ok. And I have checked the file
>> itself and the PDF looks great.
>>
>> How do i now serve this to the user who has requested it. If I try to
> write
>> it to the response (using the same method I use to creare dynamic image,
>> this works), it just shows up a blank screen.
>>
>> The problem also is, even if it did show the PDF, acrobat, to my
> understand
>> will read only chunks of the stream and will go pack to get more. Thisis
>> a
>> problem because there is nothing to go back for.
>>
>> So the point,
>>
>> If I can just redirect the browser to a file in the tomcat temp directory
>> (can I do that, will the use have access to that directory), then how do
>> I
>> translate the location of the temp directory to a url that is accesible
>> outside.
>>
>> If not then what other suggestions can people give me.
>>
>> Thanks in advance
>>
>> Steve
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: Serving files using tomcat
Posted by Steve Vanspall <st...@crmsoftware.com.au>.
Unfortunately that is what I do
OutputStream dos = null;
FileInputStream fis = null;
try
{
fis = new FileInputStream(rf.getPdf());
response.setContentType("application/pdf");
response.setContentLength((int) rf.getPdf().length());
//response.setHeader(response.)
dos = response.getOutputStream();
int read = -1;
byte[] bytes = new byte[100000];
while((read = fis.read(bytes)) != -1)
dos.write(bytes, 0, read);
dos.flush();
return mapping.findForward("PDF");
} catch (Exception e)
{
// TODO Auto-generated catch block
if(e instanceof SocketException)
return mapping.findForward("reload");
throw new IOException(e.toString());
}
finally
{
if(dos != null)
dos.close();
if(fis != null)
fis.close();
}
Acrobat now loads but the PDF doesn't appear.
Probably worth mentioning that I use struts, so I forward to a blank page
with the content type set to application/pdf, maybe that is the problem, but
not sure what else to do with the return.
When I do the same thing with a dynamic image and forward to a page with a
jpg content type, the image appears without a problem.
Steve
----- Original Message -----
From: "Anhony" <an...@comcast.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Thursday, May 05, 2005 1:02 AM
Subject: Re: Serving files using tomcat
> Greetings,
>
> Take a look at the code fragment below. It should serve as a good starting
> point.
> I hope this helps.
>
> AS-
>
> private void processPDFRequest(HttpServletRequest request,
> HttpServletResponse response) throws ServletException, IOException,
> Exception
> {
> int bytesCopied = 0;
>
> FileInputStream fin = null;
> OutputStream out = null;
>
> String fileAddress = "The fully qualified path to your PDF file";
> if( fileAddress == null )
> return;
>
> int ext = fileAddress.lastIndexOf( '.' );
> if( ext != -1 )
> {
> ext = fileAddress.substring( ext+1,
> fileAddress.length() ).toLowerCase();
>
> if( ext == "pdf" )
> response.setContentType("application/pdf");
> else
> "Do whatever you think best to do"
> }
> else
> "Do whatever you think best to do"
>
> try
> {
> out = response.getOutputStream();
> fin = new FileInputStream( fileAddress );
> bytesCopied = StreamCopier.copy( fin, out );
> }
> finally
> {
> if( fin != null )
> fin.close();
> if( out != null )
> {
> out.flush();
> out.close();
> }
> }
> }
>
>
> ----- Original Message -----
> From: "Steve Vanspall" <st...@crmsoftware.com.au>
> To: "Tomcat User List" <to...@jakarta.apache.org>
> Sent: Wednesday, May 04, 2005 9:29 AM
> Subject: Serving files using tomcat
>
>
> Hi,
>
> I have been looking around and haven't found a solution that works
>
> basically I have a PDF that gets created dynamically. Now to save memory I
> have the PDF written to a file rather than a ByteArray. The only way I can
> be sure that I wont encounter errors creating the file is to use
> File.createTempFile. The creation goes of ok. And I have checked the file
> itself and the PDF looks great.
>
> How do i now serve this to the user who has requested it. If I try to
write
> it to the response (using the same method I use to creare dynamic image,
> this works), it just shows up a blank screen.
>
> The problem also is, even if it did show the PDF, acrobat, to my
understand
> will read only chunks of the stream and will go pack to get more. Thisis a
> problem because there is nothing to go back for.
>
> So the point,
>
> If I can just redirect the browser to a file in the tomcat temp directory
> (can I do that, will the use have access to that directory), then how do I
> translate the location of the temp directory to a url that is accesible
> outside.
>
> If not then what other suggestions can people give me.
>
> Thanks in advance
>
> Steve
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: Serving files using tomcat
Posted by Anhony <an...@comcast.net>.
Greetings,
Take a look at the code fragment below. It should serve as a good starting
point.
I hope this helps.
AS-
private void processPDFRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException,
Exception
{
int bytesCopied = 0;
FileInputStream fin = null;
OutputStream out = null;
String fileAddress = "The fully qualified path to your PDF file";
if( fileAddress == null )
return;
int ext = fileAddress.lastIndexOf( '.' );
if( ext != -1 )
{
ext = fileAddress.substring( ext+1,
fileAddress.length() ).toLowerCase();
if( ext == "pdf" )
response.setContentType("application/pdf");
else
"Do whatever you think best to do"
}
else
"Do whatever you think best to do"
try
{
out = response.getOutputStream();
fin = new FileInputStream( fileAddress );
bytesCopied = StreamCopier.copy( fin, out );
}
finally
{
if( fin != null )
fin.close();
if( out != null )
{
out.flush();
out.close();
}
}
}
----- Original Message -----
From: "Steve Vanspall" <st...@crmsoftware.com.au>
To: "Tomcat User List" <to...@jakarta.apache.org>
Sent: Wednesday, May 04, 2005 9:29 AM
Subject: Serving files using tomcat
Hi,
I have been looking around and haven't found a solution that works
basically I have a PDF that gets created dynamically. Now to save memory I
have the PDF written to a file rather than a ByteArray. The only way I can
be sure that I wont encounter errors creating the file is to use
File.createTempFile. The creation goes of ok. And I have checked the file
itself and the PDF looks great.
How do i now serve this to the user who has requested it. If I try to write
it to the response (using the same method I use to creare dynamic image,
this works), it just shows up a blank screen.
The problem also is, even if it did show the PDF, acrobat, to my understand
will read only chunks of the stream and will go pack to get more. Thisis a
problem because there is nothing to go back for.
So the point,
If I can just redirect the browser to a file in the tomcat temp directory
(can I do that, will the use have access to that directory), then how do I
translate the location of the temp directory to a url that is accesible
outside.
If not then what other suggestions can people give me.
Thanks in advance
Steve
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org