2014-01-28 71 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)

相關問題