Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Opening files from a web server using Java servlets

Thread view: 
jonesy <3 - 23 Oct 2006 06:55 GMT
Hi,

I have used Java servlets for a website.  I would like to open files
stored on the webserver from the Java servlet.  I was previously using:

       res.sendRedirect("/filename.xls");

which redirected users to the file, but this forfeits the website
permissions as the users can simply type in the whole URL of the file
at a later date and access the file directly.

I found info on GetFile() here:
http://www.slamd.com/slamd-1.8.2-javadoc/com/sun/slamd/misc/GetFile.html

But I am unsure how to implement it.  Do I need to create a GetFile
class?  Do I need to import any extra packages to use this method?  Is
there a better way of opening files through a java servlet?

Thanks for any help.
Manish Pandit - 23 Oct 2006 08:32 GMT
Hi,

You need to open the file in the servlet (using java.io.File API), and
then stream/write the file on the response *after setting appropriate
headers* (using Servlet API). In particular you need to take a look at
HttpServletResponse methods.

-cheers,
Manish
Simon Brooke - 23 Oct 2006 14:22 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> I found info on GetFile() here:
> http://www.slamd.com/slamd-1.8.2-javadoc/com/sun/slamd/misc/GetFile.html

You don't need this. See java.net.URL.openStream().

Simply open the stream, schlurp the contents either into memory or a local
file, or simply print it directly to the output stream. For example:

       /**
        * read the value at this URL and return it as a string
        *
        * @param source where to read from
        *
        * @return a string representation of the data fetched from the URL
        *
        * @since Jacquard 1.9
        */
       protected String readStringFromURL( URL source ) throws IOException
       {
               BufferedReader in =
                       new BufferedReader( new InputStreamReader( source.openStream(  ) ) );

               StringBuffer buf = new StringBuffer(  );

               for ( String line = in.readLine(  ); line != null;
                                       line = in.readLine(  ) )
               {
                       buf.append( line );
                       buf.append( '\n' );
               }

               return buf.toString(  );
       }

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

       ;; I'll have a proper rant later, when I get the time.

jonesy <3 - 24 Oct 2006 02:32 GMT
Thanks for your help. I tried the ideas that you both gave me, but I
don't think I properly explained what I was looking for before.. sorry

We want the servlet to open a file that is not publicly accessible via
a URL. The file is stored in a separate directory on the UNIX box, at
the same level as the public_html directory.

When I tried both your ideas they seemed to access the file from a URL
location (i.e. somewhere in public_html). It is important that users
cannot simply type the full file URL and access the spreadsheet,
without using the servlet (so that their level of access permission is
checked by the servlet).

Thanks for any further help or ideas


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.