java.lang.NullPointerException:未设置 FileItemFactory
我尝试实现 fileUpload ,但出现如下错误。
我检查了此文档,但我不明白我的代码中的问题; 问题出在我在代码中标记的第 46 行。
有人请帮助我解决方案。
private static final String UPLOAD_DIRECTORY = "d:\\";
public void doPost(HttpServletRequest request,HttpServletResponse response){
ServletFileUpload upload = new ServletFileUpload();
if(ServletFileUpload.isMultipartContent(request)){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
try {
Line 46---> error List<FileItem> items = upload.parseRequest(request);
for(FileItem item : items){
if( item.isFormField())continue;
String fileName = item.getName();
if (fileName != null) {
fileName = FilenameUtils.getName(fileName);
}
File uploadedFile = new File(UPLOAD_DIRECTORY, fileName);
if (uploadedFile.createNewFile()) {
item.write(uploadedFile);
response.setStatus(HttpServletResponse.SC_CREATED);
response.getWriter().print("The file was created successfully.");
response.flushBuffer();
}else{
throw new IOException("The file already exists in repository.");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,"Request contents type is not supported by the servlet" );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
生成错误:
java.lang.NullPointerException: No FileItemFactory has been set.
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:354)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at com.server.FileUpload.doPost(FileUpload.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I trying to implement fileUpload , But I get an error described below.
i Checked this document , BUT I DIDNT UNDERSTOOD THE PROBLEM IN MY CODE;
the problem is in Line 46 which i have marked in code.
Someone Plz help me with solution.
private static final String UPLOAD_DIRECTORY = "d:\\";
public void doPost(HttpServletRequest request,HttpServletResponse response){
ServletFileUpload upload = new ServletFileUpload();
if(ServletFileUpload.isMultipartContent(request)){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
try {
Line 46---> error List<FileItem> items = upload.parseRequest(request);
for(FileItem item : items){
if( item.isFormField())continue;
String fileName = item.getName();
if (fileName != null) {
fileName = FilenameUtils.getName(fileName);
}
File uploadedFile = new File(UPLOAD_DIRECTORY, fileName);
if (uploadedFile.createNewFile()) {
item.write(uploadedFile);
response.setStatus(HttpServletResponse.SC_CREATED);
response.getWriter().print("The file was created successfully.");
response.flushBuffer();
}else{
throw new IOException("The file already exists in repository.");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,"Request contents type is not supported by the servlet" );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Error generated:
java.lang.NullPointerException: No FileItemFactory has been set.
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:354)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at com.server.FileUpload.doPost(FileUpload.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
猜测,这是因为您使用的是
upload
,它不是用DiskFileItemFactory
初始化的,而不是servletFileUpload
代码>,这是。At a guess, it's because you're using
upload
, which wasn't initialized with aDiskFileItemFactory
, instead ofservletFileUpload
, which was.