java.lang.NullPointerException:未设置 FileItemFactory

发布于 2025-01-07 08:12:45 字数 2443 浏览 0 评论 0原文

我尝试实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2025-01-14 08:12:45

猜测,这是因为您使用的是 upload,它不是用 DiskFileItemFactory 初始化的,而不是 servletFileUpload代码>,这是。

At a guess, it's because you're using upload, which wasn't initialized with a DiskFileItemFactory, instead of servletFileUpload, which was.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文