• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Reading File from outside the WEBROOT

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to open a text file from jsp page.
when the file is in the web root, then the file is open, but when i copy the file to some other directory like D drive, then its showing error: "The requested URL /servlets/oracle.jsp.JspServlet was not found on this server". This error shows that its not finding the file.

The Code i am using :
<%@page contentType="text/html" import="java.io.*" %>
<%
String path="E://REPORT//test.txt";
out.println(path);
response.setContentType("text");
response.setHeader("content-disposition","inline;filename="+path);
String temp=request.getRealPath(path);
//out.println(temp);
FileInputStream fin;
try
{
fin = new FileInputStream (temp);
out.println( new DataInputStream(fin).readLine() );
fin.close();
}
catch (IOException e)
{
out.println ("Unable to read from file" +e);
System.exit(-1);
}
%>

Thanks for your Help

Regards
[ May 10, 2007: Message edited by: sk mehrotra ]
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sk mehrotra:

String path="E://REPORT//test.txt";
...
catch (IOException e)
{
out.println ("Unable to read from file" +e);
System.exit(-1);
}
[ May 10, 2007: Message edited by: sk mehrotra ]



There are quite a few things that are sort of wrong in that code. Please study some resources on how to handle exceptions, servlet/jsp environment, MVC architecture etc..

Anyhow -
Is the second slash after REPORT ('//') a mistake?
Do not use System.exit() in a JSP running in a servlet container!!!
At the very least, wrap the IOException in a RuntimeException and throw it again. At least, that will show you why it is failing.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You should not use request.getRealPath() with absolute path (E://REPORT//test.txt) because it is already real full path.
You use request.getRealPath only if you have file relative to your web application folder. By the way, this method is deprecates and you should use ServletContext.getRealPath() or application.getRealPath() from your JSP.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic