• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

download files from UNC using jsp

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
I have a web app that has view from JSPs. I need to create a JSP page to force the download of different types of files - doc, xls, pdf etc and not open it by default in the browser (IE/Firefox). The other problem is the file path is not on my webserver document directory but on a UNC drive (universal naming convention) eg. \\server\folder\file.xls

Please give me suggestions how I can download this using jsp.

Thanks,
~ML
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to stream the files (which is just about impossible from a JSP) after setting the content disposition to be an attachment.

I have a servlet that streams files at:
http://simple.souther.us

Look for SimpleStream.

It sets the Content-Disposition header too, so you'll see how to do that.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW: It should be no different from a UNC path than from a local path as long as the user that Tomcat is running under has sufficient permissions.
 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ben, I checked the SimpleStream class and looks great, just what I needed. But I did not get
(which is just about impossible from a JSP)

~Martin
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Streaming from a JSP is tough because the body itself (everthing outside the <% %> tags) is written to the out stream by the writer.

For that reason, it's difficult, at best, to start and stop a stream without generating an illegal state exception.

Make life easier, do it from a servlet..
[ February 11, 2005: Message edited by: Ben Souther ]
 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,
Have a few problems:
1. The servlet doesnot seem to get the InputStream of the file on my UNC.
But if i do this it works.

2. I see my pc cpu usage goes to 100% after a couple of requests to the servlet.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm context.getResourceAsStream is probably not the best way to get a file that's outside of the scope of the app. The example was for reading files/resources from within the app or war file.
Using FileInputStream was a good idea.

Why are you creating a printwriter variable?
Are you still using response.getOuputStream to stream the file down to the browser?

How big are the files?
[ February 11, 2005: Message edited by: Ben Souther ]
 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was using the PrintWriter and looks like that was the cause of the performance issue. I replaced it with

and it has better performance. Do you have any other alternatives better than getOutPutStream ?
My files are in the range if 1 to 10 MB
~ML
[ February 11, 2005: Message edited by: Martin Lira ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, OutputStream is perfect for that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic