user1733583
user1733583

Reputation:

How do I open a file located in WEB-INF?

My servlet lets user upload a file, I've created a button to view the uploaded file.

now, on click of that button I want the uploaded file to open. How do I do this on the JSP side or servlet.java side?

it is located in WEB-INF/Uploads/my.txt folder.

=====================================EDIT=========================================

Based on answers below, I've modified my code and I'm pasting the same here for more answers,

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    ServletContext context = getServletContext();
    String path = context.getRealPath("/u/poolla/workspace/FirstServlet/WebContent/WEB-INF/Uploads/Config.txt");
    FileReader reader = new FileReader(path);
    BufferedReader br = new BufferedReader(reader);
    String firstline = br.readLine();
    System.out.println(firstline);

}

PS: This is not working, still looking for answers. Thank You!

Upvotes: 0

Views: 6335

Answers (5)

user957654
user957654

Reputation:

try to do the following :

ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/WEB-INF/Uploads/my.txt");

then read the URL content like the following :

  BufferedReader br = new BufferedReader(new InputStreamReader(
               is));

int value=0;

         // reads to the end of the stream 
         while((value = br.read()) != -1)
         {
            // converts int to character
            char c = (char)value;

            // prints character
            System.out.println(c);
         }

and please give me some feedback

Hope That Helps .

Upvotes: 1

Suzon
Suzon

Reputation: 759

In Java class: If you need to direct access then you have to extends HttpServlet like

public class FileReader extends HttpServlet {
....
....
....
  public void readAFile(){
    ServletContext servletContext=super.getServletContext();
    InputStream initFileStream = servletContext.getResourceAsStream("/WEB-INF/<path>");
    //TODO : according to your need
  }
{

Tested Servlet

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
    /**
     * Servlet implementation class STest
     */
    @WebServlet("/STest")
    public class STest extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public STest() {
        super();
        // TODO Auto-generated constructor stub
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        process();
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        process();
        }

        private void process() throws IOException {
        ServletContext servletContext=super.getServletContext();
        InputStream initFileStream = servletContext.getResourceAsStream("/WEB-INF/test.txt");
        BufferedReader reader=new BufferedReader(new InputStreamReader(initFileStream));
        StringBuffer stringAll=new StringBuffer();
        while(reader.ready()){
            stringAll.append(reader.readLine());
        }
        System.out.println(stringAll);
        }

    }

Upvotes: 0

LynAs
LynAs

Reputation: 6567

if it is a image file then u can do following using jstl tag

<img src="<c:url value='Uploads/yourImg.png' />">

Assuming you web-inf file in the src folder you can try folloing

File f = new File("src/web-inf/Uploads/YourFile.txt");

If the file name is not fixed then use <form> in jsp to get the file name from the jsp page

Upvotes: 0

Akkusativobjekt
Akkusativobjekt

Reputation: 2023

In your Servlet class, you can use the following code :

ServletContext context = getServletContext();
String path = context.getRealPath("/WEB-INF/Uploads/my.txt");

Then the path should be correct. And then you can use a normal FileReader :

FileReader reader = new FileReader(path);
BufferedReader br = new BufferedReader(reader);
String firstline = br.readLine();
System.out.println(firstline);
...

Upvotes: -1

Mathias G.
Mathias G.

Reputation: 5105

You can do this by using the ServletContext:

ServletContext#getResourceAsStream()

As far as I know the classLoader can only access WEB-INF/classes and WEB-INF/lib but not WEB-INF/Uploads. Try to put the file in the classes sub-folder.

Upvotes: 3

Related Questions