Java 从 servlet 访问 WebContent 中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21415413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:36:38  来源:igfitidea点击:

Access file inside WebContent from servlet

javaservletsjsoupservlet-3.0web-application-project

提问by Thirunavukkarasu Muthuswamy

I trying to access the html files inside the WebContent/alerts folder using its relative path from servlet. But i unable to access it using its relative path,

我尝试使用来自 servlet 的相对路径访问 WebContent/alerts 文件夹中的 html 文件。但我无法使用其相对路径访问它,

enter image description here

在此处输入图片说明

Access the file inside WebContent from Servletusing relative path:

使用相对路径从Servlet访问 WebContent 中的文件:

protected Element getSummary(String value) throws IOException
{
    Element element=null;
    Summary summary = Summary.valueOf(value);
    switch(summary) {
    case rtp_summary:
        element=parseDIV(new File("../../WebContent/alerts/rtp_bcklg_mail.html"),"rtp_summary");
        break;
    case ffu_summary:
        element=parseDIV(new File("/../../WebContent/alerts/ffu_comp_bcklg_mail.html"),"ffu_summary");
        break;    
    default:
        System.out.println("Enter a valid choice");
        break;
    }
    return element;
}

Access the file inside WebContent from Java Threadusing relative path:

使用相对路径从Java Thread访问 WebContent 中的文件:

 public class WriteJSONFile implements Runnable{

WriteJSONFile(){
}

@Override

public void run()
{
    try {
        createJSONFile();
    } catch (IOException e) {

        e.printStackTrace();
    }
}

@SuppressWarnings("unchecked")
private static void createJSONFile() throws IOException
{
    String path="C:/Users/Thiru/Documents/Website Design/Macaw/";
    JSONArray jsonArray=new JSONArray();
    JSONObject rtpStatus=new JSONObject();
    rtpStatus.put("name","RTP");
    rtpStatus.put("status",parseDIV(new File(path+"WebContent/alerts/rtp_bcklg_mail.html"),"rtp_health"));
    jsonArray.add(rtpStatus);

    JSONObject proposalattribStatus=new JSONObject();
    proposalattribStatus.put("name","PROPOSAL ATTRIBUTE");
    proposalattribStatus.put("status",parseDIV(new File(path+"WebContent/alerts/prpsl_txtsrch.html"),"prpsl_attrb_health"));
    jsonArray.add(proposalattribStatus);

    writetoFile(jsonArray);
}


private static void writetoFile(JSONArray jsonArray) {
    try {
        String path="C:/Users/Thiru/Documents/Website Design/Macaw/";
        FileWriter file = new FileWriter(path+"WebContent/properties/status.json");
        file.write(jsonArray.toJSONString());
        file.flush();
        file.close();

    } catch (IOException e) {
        e.printStackTrace();
    }   
}

protected static String parseDIV(File input, String divElement)
        throws IOException {
    Document doc = Jsoup.parse(input, "UTF-8");
    String content = doc.getElementById(divElement).val();
    System.out.println(divElement +" "+content);
    return content;
}

}

}

And i also want to access the files inside Webcontent from RESTful web service method using relative path. Thanks in advance.

而且我还想使用相对路径从RESTful Web 服务方法访问 Webcontent 中的文件。提前致谢。

采纳答案by dnault

Use ServletContext.getRealPath()to locate the file on disk. Note that this only works if the webapp is "exploded" during deployment.

使用ServletContext.getRealPath()定位磁盘上的文件。请注意,这仅在 webapp 在部署期间“爆炸”时才有效。

For example:

例如:

   File f = new File(getServletContext().getRealPath("alerts/rtp_bcklg_mail.html"));

回答by dnault

Current location of your files could be considered as a security issue. It is advisable to place such files in WEB-INF.

您文件的当前位置可能被视为安全问题。建议将此类文件放在WEB-INF.

String filePath = getServletContext().getRealPath("/WEB-INF/alerts/rtp_bcklg_mail.html");

回答by balu

Properties props=new Properties();
    props.load(this.getServletContext().getResourceAsStream("/alerts/"+your_fileName+".html"));

by Calling ServletContext object we can get WebContext root from now on wards we can pass our file file statically

通过调用 ServletContext 对象,我们可以从现在开始获取 WebContext 根,我们可以静态地传递我们的文件

回答by moett

You can use servlet url pattern in your html page form action or html page a href tag.But your web.xml use servlet-mapping's url pattern like this (/projectname/servleturl)

您可以在 html 页面表单操作中使用 servlet url 模式或在 html 页面中使用 href 标签。但是您的 web.xml 使用 servlet-mapping 的 url 模式,如下所示 (/projectname/servleturl)