如何打开位于WEB-INF中的文件?

时间:2014-02-24 09:28:38

标签: java jsp servlets inputstream

我的servlet允许用户上传文件,我创建了一个按钮来查看上传的文件。

现在,点击该按钮我想要打开上传的文件。我如何在JSP端或servlet.java端执行此操作?

它位于WEB-INF / Uploads / my.txt文件夹中。

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

根据下面的答案,我修改了我的代码,我在这里粘贴了相同的答案,

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:这不起作用,仍在寻找答案。 谢谢!

5 个答案:

答案 0 :(得分:3)

您可以使用ServletContext执行此操作:

ServletContext#getResourceAsStream()

据我所知,classLoader只能访问WEB-INF / classes和WEB-INF / lib,但不能访问WEB-INF / Uploads。尝试将该文件放在classes子文件夹中。

答案 1 :(得分:1)

尝试执行以下操作:

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

然后阅读以下网址内容:

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

请给我一些反馈

希望有助于此。

答案 2 :(得分:0)

如果是图像文件,则可以使用jstl标记

进行以下操作
<img src="<c:url value='Uploads/yourImg.png' />">

假设你在src文件夹中的web-inf文件,你可以尝试下面的

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

如果文件名没有修复,那么在jsp中使用<form>从jsp页面获取文件名

答案 3 :(得分:0)

在Java课程中:如果您需要直接访问,那么您必须extends HttpServlet喜欢

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

经过测试的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);
        }

    }

答案 4 :(得分:-1)

在Servlet类中,您可以使用以下代码:

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

然后路径应该是正确的。然后你可以使用普通的FileReader:

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