Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:00:32+00:00 2026-06-06T15:00:32+00:00

I read that the servlets map the current location based on the url. Clicking

  • 0

I read that the servlets map the current location based on the url. Clicking a button from my Home.jsp page directs me to my servlet, ExcelUploader. The URL when said button is clicked is

http://localhost:8080/ServletExample/ExcelUploader

I’m trying to open an excel file located in the same folder as my JSP. so that means I have to move one folder up relative to the url above. I have this in my servlet:

 InputStream inp = new FileInputStream("../OpenMe.xls");

However I’m still getting a

java.io.FileNotFoundException: ..\OpenMe.xls (The system cannot find the file specified)

This is how my project is setup:

Project Setup

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:00:33+00:00Added an answer on June 6, 2026 at 3:00 pm

    The FileInputStream operates on the local disk file system relative to the working directory and knows absolutely nothing about the fact that it’s invoked from a Java EE web application. Any relative path you pass to it is relative to the folder which was been opened at the moment the command to start the server is executed. This is often the server’s own installation folder, but in case of an IDE this can also be project’s own root folder. This variable is not controllable from inside your Java code. You should not be relying on that.

    You’ve stored the file as a resource of the public webcontent. So it’s available as a webcontent resource by ServletContext#getResourceAsStream() which returns an InputStream. If you have absolutely a legitimate reason to invoke the servlet by its URL instead of just using the file’s own URL http://localhost:8080/ServletExample/OpenMe.xls, then you should be getting at as follows:

    InputStream input = getServletContext().getResourceAsStream("/OpenMe.xls");
    // ...
    

    If your intent is indeed to restrict the file’s access to by the servlet only, you might want to consider to move the file into the /WEB-INF folder, so that the enduser can never open it directly by entering the file’s own URL. You only need to change the resource path accordingly.

    InputStream input = getServletContext().getResourceAsStream("/WEB-INF/OpenMe.xls");
    // ...
    

    You should not be using getRealPath() as suggested by the other answer. This won’t work when the servletcontainer is configured to expand the WAR file into memory instead of into local disk file system, which is often the case in 3rd party hosts. It would return null then.

    See also:

    • getResourceAsStream() vs FileInputStream
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read that some webmail services prefetch url links in emails. The GET request
From the examples that I have read and from the documentation on MysqlConnectionPoolDataSource at
I am studying servlets I read that servlets are java programs but there are
I want to read a source code (HTML tags) of a given URL from
I want to create a JSP page or servlet that will work in 2
Is it possible to pass a request from java servlet and read that request
I read that .NET uses connection pooling. For example, if I instantiate a bunch
I read that when you don't have access to the web server's headers you
I read that early C++ compilers actually translated the C++ code to C and
I read that System.Drawing.Point is a value type. I do not understand. Why?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.