reading a file from a servlet

Discussions

Web tier: servlets, JSP, Web frameworks: reading a file from a servlet

  1. reading a file from a servlet (5 messages)

    hi,
    i try to read a file from a servlet (on the server side) and i get the error:

    Error: 500
    Internal Servlet Error:
    java.io.FileNotFoundException: http:localhost:8080\chat\servlet\fichierALire.txt (The system cannot find the path specified)

    i guess that's a problem of path but i don't understand: i put the file on the same directory as the servlet that i call.

    Thanks a million for your time .
    Here is the code:


    public void doGet(HttpServletRequest req,
     HttpServletResponse res)
     throws ServletException, IOException{

     PrintWriter servout = res.getWriter();

     String nomFic = "http:localhost:8080/chat/servlet/fichierALire.txt";

     File fichier = new File(nomFic);
     FileReader fin = new FileReader(fichier);

     char c[] = new char[(char)fichier.length()];
     int i;
     i = fin.read(c);
     String s = new String(c);

     res.setContentType("text/html");
     servout.println( "<html>" );
     servout.println( "<body bgcolor=white text=red>");
     servout.println( "Results:<p>" + s +"</P>");
     servout.println( "</html>");

     fin.close();
     }


    Threaded Messages (5)

  2. reading a file from a servlet[ Go to top ]

    Try using the absolute path of the file that you want to read.
  3. reading a file from a servlet[ Go to top ]

    Hi,

    My guess is that your URL is incorrect--

    http://localhost:8080/ should do the trick.

    --John
  4. reading a file from a servlet[ Go to top ]

    hi,
     try to use actually system path like c:\test.txt ithink this should help u.
    bye
    -kiran
  5. reading a file from a servlet[ Go to top ]

    hi
    if you want to keep your path , is better to use :

    URLConnection con = new URLConnection("http:localhost:8080/chat/servlet/fichierALire.txt");
    InputStream in = con.getInputStream();

    ... continue like with file
  6. reading a file from a servlet[ Go to top ]

    ouch
    and i forgot :
    chage path to
    http://localhost:8080/chat/servlet/fichierALire.txt

    sorry