【问题标题】:Access file inside WebContent from servlet从 servlet 访问 WebContent 中的文件
【发布时间】:2014-02-20 07:56:54
【问题描述】:

我尝试使用来自 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 服务e 方法使用相对路径访问 Webcontent 中的文件。提前致谢。

【问题讨论】:

  • parseDiv 方法的作用是什么?

标签: java servlets jsoup servlet-3.0 web-application-project


【解决方案1】:

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

例如:

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

【讨论】:

  • 现在我想访问 RESTful Web 服务和 java 调度程序类中的同一组文件。我可以在这里使用这个 getServletContext() 吗?
  • 我不知道。您可以将这些文件移动到类路径中(在“Java Resources/src”下)并像这样加载它们:stackoverflow.com/a/3309088/611819
【解决方案2】:

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

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

【讨论】:

【解决方案3】:
Properties props=new Properties();
    props.load(this.getServletContext().getResourceAsStream("/alerts/"+your_fileName+".html"));

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 2015-07-01
      • 2018-02-15
      • 2023-03-13
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多