我正在尝试获取文件的相对路径,但没有成功。绝对路径是:“ / Users / dev / Documents / projects / testService / src / main / resources / response.xml”。部署代码后,我得到了java.io.FileNotFoundException :(无此类文件或目录)。
能帮我吗? 谢谢
0
代码如下:
try {
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("src/main/resources/response.xml"));
while(xsr.hasNext()) {
xsr.next();
if (xsr.getEventType() == XMLStreamConstants.START_DOCUMENT || xsr.getEventType() == XMLStreamConstants.END_DOCUMENT ||xsr.isCharacters() == true || xsr.hasText() == true || xsr.getEventType() == XMLEvent.END_ELEMENT)
continue;
QName name = xsr.getName();
int index = name.toString().indexOf('}');
String aa = name.toString().substring(index+1);
if (xsr.isStartElement() && aa.contains("Bonitae"))
break;
}
jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
response = unmarshaller.unmarshal(xsr, Customer.class);
}
在我的本地PC上运行正常。我用罐子部署了项目。
答案 0 :(得分:0)
您可以使用以下方法获取InputStream:
XMLInputFactory xif = XMLInputFactory.newFactory();
InputStream in = Main.class.getResourceAsStream("resources/response.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(in);
用您的班级名称替换主要