2014-01-28 72 views
3

我试图使用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; 
} 

访问内部的WebContent文件从Java线程,但我无法使用它的相对路径使用相对路径访问它,

enter image description here

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

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

}

我还想从访问Web内容中的文件RESTful web服务 e使用相对路径的方法。提前致谢。

+0

parseDiv的做法是什么? – jny

回答

7

使用ServletContext.getRealPath()在磁盘上查找文件。请注意,这只有在部署期间web应用程序“爆炸”时才有效。

例如:

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

谢谢dnault,就像魅力一样工作! –

+0

现在我想访问REST风格的Web服务和java调度程序类中的同一组文件。我可以在这里使用这个getServletContext()吗? –

+0

不是我所知道的。您可以将这些文件移动到类路径中(在“Java Resources/src”下)并加载它们,如下所示:http://stackoverflow.com/a/3309088/611819 – dnault

3

文件的当前位置可能被视为一个安全问题。建议将这些文件放入WEB-INF

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

+1 Servlet容器不允许在WEB-INF下下载文件。 http://stackoverflow.com/a/19786283/611819 – dnault

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

通过调用ServletContext对象,我们可以从现在得到WebContext根上病房,我们可以通过我们的文件的文件静态

0

您可以在HTML页面表单操作或HTML页面使用servlet的URL模式href tag.But你的web.xml使用像这样的servlet-mapping的url模式(/ projectname/servleturl)

相关问题