gpt4 book ai didi

java - 尝试读取 WEB-INF 中的属性文件时 InputStream 为空

转载 作者:行者123 更新时间:2023-11-28 22:17:41 28 4
gpt4 key购买 nike

每当我尝试读取位于“/WEB-INF”中的属性文件时,我的 InputStream 对象都会让控制台打印出 NullPointerException。但是当我执行 InputStream.toString() 时,它会打印出 java.io.FileInputStream@1933968 而不是实际的 null 值。

这是我返回的堆栈跟踪:

SEVERE: Servlet.service() for servlet [spring] in context with path [/translator] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.webclaims.translator.TranslatorApi.readPropertiesFile(TranslatorApi.java:149)
at com.webclaims.translator.TranslatorApi.writePropertiesFile(TranslatorApi.java:128)
at com.webclaims.translator.controllers.TestController.editableWebpage(TestController.java:45)

/WEB-INF的布局;

WEB-INF-|
|-jsp
|-configDummy.properties
|-spring-servlet.xml
|-web.xml

我正在使用 Java 8 的 Tomcat v8 服务器上运行这个项目。

spring-servlet.xml;

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<mvc:annotation-driven/>
<context:component-scan base-package="com.webclaims.*" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="propertiesFile" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" value="/WEB-INF/configDummy.properties"></property>
</bean>

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>translator</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

TestController.java(包含异常入口点)

@Controller
@PropertySource("/WEB-INF/configDummy.properties")
public class TestController {

private static final Logger LOGGER = Logger.getLogger(TestController.class.getName());

private static final String PROPS_FILE = "/WEB-INF/configDummy.properties";

@RequestMapping(value = "/test")
public ModelAndView originalWebpage() {
return new ModelAndView("testpage");
}

@CrossOrigin()
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String editableWebpage(@RequestBody String data, HttpServletRequest request) {
InputStream inputStream = request.getSession().getServletContext()
.getResourceAsStream(PROPS_FILE);
System.out.println(inputStream);

JsonParser parser = new JsonParser(data);
Elements elements = parser.jsonToElements();
TranslatorApi t = new TranslatorApi();
t.writePropertiesFile(elements, inputStream);
return data;
}
}

TranslatorApi.java

@PropertySource("/WEB-INF/configDummy.properties")
public class TranslatorApi {

@Autowired
private Properties properties;

private static final Logger LOGGER = Logger.getLogger(TranslatorApi.class.getName());

public TranslatorApi() {}

public Properties getProperties() {
return properties;
}

public void setProperties(Properties properties) {
this.properties = properties;
}

public void writePropertiesFile(Elements elements, InputStream inputStream) {
System.out.println("write opertion executed");
System.out.println(elements);
try {
readPropertiesFile(inputStream);

for(Element e : elements) {
properties.setProperty(e.className(), e.text());
}

String path=Thread.currentThread().getContextClassLoader()
.getResource("src/main/webapp/WEB-INF/props/configDummy.properties").toString();
File file = new File(path);
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "DUMMY");
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void readPropertiesFile(InputStream inputStream) throws IOException {
try {
properties.load(inputStream);
inputStream.close();
} catch(FileNotFoundException e) {
LOGGER.log(Level.SEVERE, "FileNotFoundException has occured", e);
} catch(IOException e) {
LOGGER.log(Level.SEVERE, "IOException has occured", e);
}
}

这里的大多数人建议我应该将属性文件放在类路径中。如果我有它,我将来可以写信给它吗?

最佳答案

对我而言,您的 NPE 与 TranslatorApi 中的字段 propertiesnull 这一事实更为相关,因为它没有被正确注入(inject)。

尝试使用 Environment 类作为下一个:

@Autowired
private Environment properties;

这是一个很好的例子 how to inject properties in your class .

关于java - 尝试读取 WEB-INF 中的属性文件时 InputStream 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38871231/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com