Opening files from a web server using Java servlets

J

jonesy

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.
 
M

Manish Pandit

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
 
S

Simon Brooke

jonesy said:
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

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( );
}
 
J

jonesy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top