我需要从类中访问我的Web项目中的资源文件.问题是开发时的路径与部署路径不同.
例如,如果我想在开发时访问某些css文件,我可以这样做:
File file = new File("src/main/webapp/resources/styles/some.css/");
Run Code Online (Sandbox Code Playgroud)
但是一旦部署它可能不起作用,因为目标文件夹中没有src或main目录.那么如何一致地访问文件呢?
我有一个XML文档,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xs:msgdata xmlns:xs="http://www.myCompany.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.myCompany.com msgdata.xsd">
<xs:msgid>MsgID001</xs:msgid>
<xs:msgHash>hlkJKLHljkhkjlHKJLHkjl6y987HJKH</xs:msgHash>
</xs:msgdata>
Run Code Online (Sandbox Code Playgroud)
还向我发送了一个模式文档(称为msgdata.xsd).我正在使用JAXB将上述xml文档解组为Java对象.解组代码如下所示:
final JAXBContext context = JAXBContext.newInstance(clazz);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("C:\\temp\\msgdata.xsd"));
final Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(schema);
return (T) unmarshaller.unmarshal(new StringReader(xml));
Run Code Online (Sandbox Code Playgroud)
XML的目标对象如下所示(然后将对象转换为休眠实体.
@XmlRootElement(name = "msgdata")
public class Message {
private String msgid;
private String msgHash;
@XmlElement(name = "msgid")
public String getMsgid() {
return msgid;
}
public void setMsgid(String msgid) {
this.msgid = msgid;
}
@XmlElement(name = "msgHash")
public String getMsgHash() {
return msgHash;
} …Run Code Online (Sandbox Code Playgroud) 我使用上述方法感到困惑,因为在加载某些属性文件时,人们正在遵循不同的方法......
Properties prop
= new Properties(new FileInputStream(new File("<path of the properties file>"));
Run Code Online (Sandbox Code Playgroud)
很少有人在使用..
Properties prop
= new Properties(getClass().getResourceAsStream("<path of the properties file>"));
Run Code Online (Sandbox Code Playgroud)
哪一个使用的时候?
我正在使用Eclipse并开发一个使用本地文件的java应用程序.该文件位于项目文件夹内,在子文件夹中,说"mydata/myfile.txt"当我从eclipse运行应用程序时一切正常,我打开一个FileInputStream,传递"mydata/myfile.txt"作为名称,文件是读得很好.但是当我将我的应用程序导出为.jar并尝试运行它时,我得到的只是控制台中的FileNotFoundException.
如何导出程序以便它可以在任何计算机上运行并能够访问该文件?先感谢您.
要properties使用Glassfishv3 webserver(位于root directory我的Web应用程序中)读取JSF2.0中的文件,我使用下面的代码 -
ServletContext ctx = (ServletContext) FacesContext
.getCurrentInstance().getExternalContext().getContext();
String deploymentDirectoryPath = ctx.getRealPath("/");
Properties prop = new Properties();
prop.load(new FileInputStream(deploymentDirectoryPath
+ File.separator + "portal-config.properties"));
Run Code Online (Sandbox Code Playgroud)
以下是门户网站的截图 -

运行门户网站时,我收到FileNotFound错误,因为该文件不存在于glassfish域中.
有没有办法读取属性文件,它既适用于情境,也适用于开发阶段和战争文件?
我正在尝试获取InputStream托管豆中的PNG文件,如下所示:
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
InputStream input = externalContext.getResourceAsStream("/myFile.png");
// input is null.
Run Code Online (Sandbox Code Playgroud)
但是,InputStream始终为null。这是怎么引起的,我该如何解决?
我在java中有一个Web应用程序,在servlet中我需要从xml文件加载属性.
代码是
XMLReader reader = XMLReaderFactory.createXMLReader();
...
FileInputStream fis = new FileInputStream("myconf.xml");
reader.parse(new InputSource(fis));
Run Code Online (Sandbox Code Playgroud)
我的问题是:myconf.xml文件应放在war文件中的哪个位置,以便servlet可以找到它?
谢谢
我已经在eclipse中将项目导出到可运行的JAR中.我的代码中有一些地方我已经完成了以下操作.
String file = "src/Files/...";
loadFile(file); // purely for example
Run Code Online (Sandbox Code Playgroud)
既然项目是JAR的形式,那些目录似乎不存在,加载这些文件失败.我很确定我引用的文件是打包在JAR中的.导出到JAR时,目录是否以任何特定方式更改?关于如何使这项工作的任何其他想法?
public class ConvertXMLtoJSON {
public static void main(String[] args) throws Exception {
InputStream in = ConvertXMLtoJSON.class.getResourceAsStream("D:\\sample.xml");
String xml = IOUtils.toString(in);
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read(xml);
System.out.println(json.toString(2));
}
}
Run Code Online (Sandbox Code Playgroud)
但我遇到错误
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1020)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:358)
at com.apache.poi.ConvertXMLtoJSON.main(ConvertXMLtoJSON.java:13
Run Code Online (Sandbox Code Playgroud)
您能帮我解决吗,这是我的xml格式ac3 AC3电话ACME电话200.0 1.0是
我已经从我的excel文件中生成了这个xml,并且已经将此xml文件转换为json文件