2014-01-28 163 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); 
} 
相关问题