2014-01-28 88 views
1

我正在開發一個項目並擁有一個驗證xml文件的類,但它們來自webPage,並從一個webmail下載並保存在臨時文件夾中。獲取我的JSF項目中的文件夾路徑

我的問題是,如果我通過應用程序獲取文件,我可以讓我的xsd文件進行驗證。但通過電子郵件下載,他們不會加載我的文件。

但我怎樣才能得到這些文件在我的資源文件夾?

public static String xsdFile(File file) { 

    String rootElement = getRootElement(file); 
    String rootVersion = getVersion(file); 
    String realPath = ""; 

    try { 
     realPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(xsdPath); 
    } catch (Exception e) { 
     String path = XmlUtil.class.getClassLoader().getResource("").getPath().replaceAll("classes/", ""); 
     path = path.concat(xsdPath).concat(rootVersion).concat("/").concat(rootElement).concat(".xsd"); 
     File teste = new File(path); 

     if (teste.exists()) { 
      System.out.println("found"); 
     } 
     return path.concat ("/").concat(rootVersion).concat("/").concat(rootElement).concat(".xsd"); 
    } 
    return realPath.concat("\\").concat(rootVersion).concat("\\").concat(rootElement).concat(".xsd"); 
} 

回答

0
public static String xsdFile(File file) { 

    String rootElement = getRootElement(file); 
    String rootVersion = getVersion(file); 
    String realPath = ""; 

    try { 

     realPath = getHTTPpath(); 

    } catch (Exception e) { 

     return getMachinePath(rootVersion, rootElement).concat(".xsd"); 

    } 

    return realPath.concat("\\").concat(rootVersion).concat("\\").concat(rootElement).concat(".xsd"); 
} 

private static String getHTTPpath() throws Exception { 
    return FacesContext.getCurrentInstance().getExternalContext().getRealPath(xsdPath); 
} 

private static String getMachinePath(String rootVersion, String rootElement) { 

    String realPath = XmlUtil.class.getClassLoader().getResource("").getPath(); 

    try { 

     realPath = URLDecoder.decode(realPath, "UTF-8"); 

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

    realPath = realPath.substring(1).replace("/WEB-INF/classes", ""); 
    return realPath.concat("resources/xsd/" + rootVersion + "/" + rootElement); 
} 
相關問題