0

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!

  • Maybe it is not the best place to upload a file (do you want to have this kind of content in the same disk partition as your applications?) Maybe you should consider uploading your files to a location agreed with your operations people (maybe a NAS?) and keep the path to that filesystem in some .properties or database – Jorge_B Feb 24 '14 at 09:34
  • Yah! But as of now I've picked this to be just a random location, may change it in future. –  Feb 24 '14 at 09:36

5 Answers5

3

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.

Mathias G.
  • 4,875
  • 3
  • 39
  • 60
  • Can you show me sample code? I've already tried this method but failed. –  Feb 24 '14 at 09:36
  • The path must begin with a "/" and is interpreted as relative to the current context root. For example `ServletContext context = getServletContext(); InputStream is = context.getResourceAsStream("/yourfilename.cnf");` Maybe that was the problem? – Mathias G. Feb 24 '14 at 09:41
1

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 .

0

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

LynAs
  • 6,407
  • 14
  • 48
  • 83
  • its a text file, can I use this method? BTW I need to do it on my .java side in doget method. –  Feb 24 '14 at 09:35
  • in java you can read file not show. for showing you always have to use jsp pages. you can see content of that file in the console – LynAs Feb 24 '14 at 09:36
  • Oh! Okay so you want me to give this on my jsp page? –  Feb 24 '14 at 09:37
  • its up to you. in the java file you can modify the content and for showing the jsp file – LynAs Feb 24 '14 at 09:38
  • it is better if you put your code here then it will be easy to understand what you want – LynAs Feb 24 '14 at 09:38
0

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

    }
Suzon
  • 749
  • 1
  • 8
  • 21
  • eclipse says there is nothing called as getResourceAsStream!! Checking google –  Feb 24 '14 at 10:02
-1

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);
...
Akkusativobjekt
  • 2,005
  • 1
  • 21
  • 26
  • Never ever use `getRealPath` because you never know if the server will return null or something usable. – Michael-O Feb 24 '14 at 09:42
  • I've tried this, as shown above, still doesn't work. –  Feb 24 '14 at 09:57
  • @Akkusativobjekt, http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath%28java.lang.String%29 – Michael-O Feb 24 '14 at 09:59