这个问题已经在这里有一个答案:
- 推荐的方式来保存上传的文件在servlet应用 2个回答
我想上传图片文件使用的ServletContext到文件夹/ Web应用程序/照片照片/,但每次我得到NullPointerException异常
我该怎么办呢正确?
这里就是NPE上CONTAINER_ROOT可变上升UploadFile.class。
@Component
public class UploadFile {
@Autowired
HttpServletRequest httpServletRequest;
private String CONTAINER_ROOT = httpServletRequest.getSession().getServletContext().getRealPath("/");
private String DIR_NAME = "foto";
private String UPLOAD_DIR = CONTAINER_ROOT + File.separator + DIR_NAME;
private Logger logManager = LogManager.getLogger(this.getClass());
public String getUploadedPath(MultipartFile file) {
return upload(file);
}
private String upload(MultipartFile file) {
if (!file.isEmpty()) try {
byte[] bytes = file.getBytes();
String fileName = file.getOriginalFilename();
File dir = new File(UPLOAD_DIR);
if (!dir.exists()) dir.mkdirs();
File fileOnServer = new File(dir.getAbsolutePath() + File.separator + fileName);
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(fileOnServer));
outputStream.write(bytes);
outputStream.close();
logManager.warn("UPLOADED FILE LOCATION: " + fileOnServer.getAbsolutePath());
return fileOnServer.getPath();
}
catch (IOException e) {
logManager.error("Error trying to upload file to location, or file is empty!", e.getMessage());
}
return null;
}
}
这是我对控制器如何使用:
Foto savedFoto = this.fotoRepository.save(new Foto(this.uploadFile.getUploadedPath(file), savedImovel));
堆栈跟踪:
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [br.com.agenciadsw.morenoimoveis.util.UploadFile]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
28-Aug-2014 11:02:16.409 WARNING [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.loader.WebappClassLoader.clearReferencesJdbc The web application [] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069)
... 71 more
Caused by: java.lang.NullPointerException
at br.com.agenciadsw.morenoimoveis.util.UploadFile.<init>(UploadFile.java:26)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
... 73 more
而这里web.xml
:
<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>MorenoImoveis</display-name>
<servlet>
<servlet-name>dispacher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
br.com.agenciadsw.morenoimoveis.cfg.PersistenceConfigurer
br.com.agenciadsw.morenoimoveis.cfg.SecurityConfigurer
br.com.agenciadsw.morenoimoveis.cfg.WebMvcConfigurer
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispacher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
br.com.agenciadsw.morenoimoveis.cfg.PersistenceConfigurer
br.com.agenciadsw.morenoimoveis.cfg.SecurityConfigurer
br.com.agenciadsw.morenoimoveis.cfg.WebMvcConfigurer
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>