从servlet访问WebContent内的文件

4

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

图片描述

使用相对路径从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;
}

如何使用相对路径从Java线程中访问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;
}

我也想通过相对路径从RESTful web service方法中访问Webcontent中的文件。提前致谢。


方法parseDiv是做什么的? - jny
4个回答

6

使用 ServletContext.getRealPath() 方法来定位磁盘上的文件。请注意,这仅在部署期间 Web 应用程序被 "解压" 时才有效。

例如:

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

现在我想在RESTful Web服务和Java调度程序类中访问相同的文件集。我能在这里使用getServletContext()吗? - Thirunavukkarasu Muthuswamy
据我所知没有。你可以将这些文件移动到类路径下(在“Java Resources/src”下),并像这样加载它们:http://stackoverflow.com/a/3309088/611819 - dnault

3

您的文件的当前位置可能会被视为安全问题。建议将这些文件放置在WEB-INF中。

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

+1 Servlet容器不允许在WEB-INF目录下下载文件。https://dev59.com/M2Ij5IYBdhLWcg3w4Y6S#19786283 - dnault

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

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

0

您可以在HTML页面的表单操作或HTML页面的超链接标签中使用Servlet URL模式。但是,您的web.xml文件应该使用servlet-mapping的URL模式,如此(/projectname/servleturl)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接