简体   繁体   English

commons fileupload(Apache)

[英]commons fileupload(Apache)

I am getting null pointer exception here: 我在这里得到空指针异常:

List fileItems = upload.parseRequest(req);

That happens if the number of line in the file is greater the 2000 approx, as I could upload the file of lines above 1000. Someone please help me out. 如果文件中的行数大于2000,则会发生这种情况,因为我可以上传1000行以上的行。有人请帮帮我。 The form is below. 表格如下。

<form name="fos_picks" id="fos_picks" action="<%=path%>/fos_upld" method="post" enctype="multipart/form-data" >

<br/><br/><br/><br/>
<p align="center">
    <input type="file" name="file" size="50" /><br/>
    <br/>
    <input type="submit" class="buttons" value="Upload File" />
    </p>
</form>

Upload controller code... 上传控制器代码......

 package file_proc;
import DBConn.DBConn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.PreparedStatement;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


@WebServlet(name="file_upld", urlPatterns = {"/file_upld"}) 

public class file_upld extends HttpServlet {

   private boolean isMultipart;
   private String filePath;
   private int maxFileSize = 50 * 1024;
   private int maxMemSize = 10 * 1024;
   private File file ;

/*   public void init( ){
      // Get the file location where it would be stored.
      filePath = 
             getServletContext().getInitParameter("file-upload"); 
   }*/
    @Override
   public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, java.io.IOException {
      // Check that we have a file upload request
       java.io.PrintWriter out = res.getWriter( );
    //   out.println("entered");
      isMultipart = ServletFileUpload.isMultipartContent(req);

      res.setContentType("text/html");

      if( !isMultipart ){
         out.println("!multipart");
         System.out.println("here");
         return;
      }
      DiskFileItemFactory factory = new DiskFileItemFactory();
      // maximum size that will be stored in memory
      factory.setSizeThreshold(maxMemSize);
      // Location to save data that is larger than maxMemSize.
      factory.setRepository(new File("c:/temp"));
      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory);
      // maximum file size to be uploaded.
      upload.setSizeMax( maxFileSize );
 ServletContext servletContext = getServletContext();
    String path = servletContext.getRealPath("/");
     BufferedReader br=null;
     String fileName="";
     DBConn db = new DBConn();

      try{ 
      // Parse the request to get file items.
          System.out.println("here1"+req);
      List fileItems = upload.parseRequest(req);

      // Process the uploaded file items
      Iterator i = fileItems.iterator();

/*      out.println("<html>");
      out.println("<head>");
      out.println("<title>Upload</title>");  
      out.println("</head>");
      out.println("<body>");*/
      while ( i.hasNext () ) 
      {
         FileItem fi = (FileItem)i.next();
         if ( !fi.isFormField () )  
         {
            // Get the uploaded file parameters
            String fieldName = fi.getFieldName();
            fileName = fi.getName();
            String contentType = fi.getContentType();
            boolean isInMemory = fi.isInMemory();
            long sizeInBytes = fi.getSize();
            // Write the file
            if( fileName.lastIndexOf("\\") >= 0 ){
               file = new File( "c:/Temp/" + 
               fileName.substring( fileName.lastIndexOf("\\"))) ;
            }else{
               file = new File( "c:/Temp/"+ 
               fileName.substring(fileName.lastIndexOf("\\")+1)) ;
            }
            if(!file.exists())
            {
                File fold=new File(file.getParent());
                fold.mkdirs();
            }
            fi.write( file ) ;
            System.out.println("Uploaded Filename: " + fileName + "<br>");
         }
      }
      }catch(Exception ce)
      {
            out.println("<font size='30' color='red'>Error Code 016</font>");
        //out.println("Exception1: "+ce);
      }
 //read uploaded file and insert into table********************************************  

    //      String newline = System.getProperty("line.separator");
//  File file = new File(path+"//"+fileName);
            //  file.createNewFile();
try{

    if(file.isFile())
    {
         br = new BufferedReader(new FileReader(file));
    String str="";
    String temp[]=null;


        file.canWrite();
        file.canRead();
        file.setWritable(true);

        db.conn.setAutoCommit(false);
        while((str=br.readLine())!=null)
                    {
                        temp=str.split("    ");

                //      PreparedStatement ps = db.conn.prepareStatement("INSERT INTO file_proc VALUES(?,?,?,?,?,?,?,?,?,STR_TO_DATE(?,'%m/%d/%Y'))");
                        PreparedStatement ps = db.conn.prepareStatement("INSERT INTO file_proc(run_date,zone,location,bank,file_type,num_rec,ex_sett_date,ex_stat_date) "
                                                        + "VALUES(STR_TO_DATE(?,'%m/%d/%Y'),?,?,?,?,?,STR_TO_DATE(?,'%m/%d/%Y'),STR_TO_DATE(?,'%m/%d/%Y'))");
                        ps.setString(1,temp[0]);
                        ps.setString(2,temp[1]);
                        ps.setString(3,temp[2]);
                        ps.setString(4,temp[3]);
                        ps.setString(5,temp[4]);
                        ps.setInt(6,Integer.parseInt(temp[5]));
                        ps.setString(7,temp[6]);
                        ps.setString(8,temp[7]);
                        ps.executeUpdate();
                        ps.close();     

                    }   
                    db.conn.commit();
                    db.conn.setAutoCommit(true);
                    db.conn.close();
                    br.close(); 
        file.delete();  

       //  RequestDispatcher rd = req.getRequestDispatcher("./status/status.jsp");
        //  rd.forward(req, res);
          out.print("success");
     //     out.println("</html>");
    }
    else
    {
        out.println(file+" is not a file");
    }           

   }catch(Exception ex) {
    out.println("file name= "+fileName);
      // out.println("DBEX= "+ex );
        out.println("<font size='30' color='red'>Error Code 017 - Recommended date format = m/d/yyyy.</font>");
        out.println("<font size='30' color='red'>Check the column order</font>"+ex);    
      //file.delete();  
     //  RequestDispatcher rd = req.getRequestDispatcher("./status/error.jsp");
//  rd.forward(req, res);
   }finally{
                    try{
                        db.conn.close();
                        br.close();
                    }catch(Exception e){}
                }
   }
    @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, java.io.IOException {

       doPost(request, response);  
   } 
}

** * * Here is form * ** * ** ** * *这是表格* ** * **

<form name="file_proc" id="file_proc" method = "post" action="../file_upld" enctype="multipart/form-data">
    <br/><br/><br/><br/>
    <p align="center">
<input type="file" id="file" name="file" size="50" /><br/>
<br/>
<input type="submit" class="buttons" value="Upload File" />br/><br/><br/>
     </p>
</form>

Upload controller code... 上传控制器代码......

 package file_proc;
import DBConn.DBConn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.PreparedStatement;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


@WebServlet(name="file_upld", urlPatterns = {"/file_upld"}) 

public class file_upld extends HttpServlet {

   private boolean isMultipart;
   private String filePath;
   private int maxFileSize = 50 * 1024;
   private int maxMemSize = 10 * 1024;
   private File file ;

/*   public void init( ){
      // Get the file location where it would be stored.
      filePath = 
             getServletContext().getInitParameter("file-upload"); 
   }*/
    @Override
   public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, java.io.IOException {
      // Check that we have a file upload request
       java.io.PrintWriter out = res.getWriter( );
    //   out.println("entered");
      isMultipart = ServletFileUpload.isMultipartContent(req);

      res.setContentType("text/html");

      if( !isMultipart ){
         out.println("!multipart");
         System.out.println("here");
         return;
      }
      DiskFileItemFactory factory = new DiskFileItemFactory();
      // maximum size that will be stored in memory
      factory.setSizeThreshold(maxMemSize);
      // Location to save data that is larger than maxMemSize.
      factory.setRepository(new File("c:/temp"));
      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory);
      // maximum file size to be uploaded.
      upload.setSizeMax( maxFileSize );
 ServletContext servletContext = getServletContext();
    String path = servletContext.getRealPath("/");
     BufferedReader br=null;
     String fileName="";
     DBConn db = new DBConn();

      try{ 
      // Parse the request to get file items.
          System.out.println("here1"+req);
      List fileItems = upload.parseRequest(req);

      // Process the uploaded file items
      Iterator i = fileItems.iterator();

/*      out.println("<html>");
      out.println("<head>");
      out.println("<title>Upload</title>");  
      out.println("</head>");
      out.println("<body>");*/
      while ( i.hasNext () ) 
      {
         FileItem fi = (FileItem)i.next();
         if ( !fi.isFormField () )  
         {
            // Get the uploaded file parameters
            String fieldName = fi.getFieldName();
            fileName = fi.getName();
            String contentType = fi.getContentType();
            boolean isInMemory = fi.isInMemory();
            long sizeInBytes = fi.getSize();
            // Write the file
            if( fileName.lastIndexOf("\\") >= 0 ){
               file = new File( "c:/Temp/" + 
               fileName.substring( fileName.lastIndexOf("\\"))) ;
            }else{
               file = new File( "c:/Temp/"+ 
               fileName.substring(fileName.lastIndexOf("\\")+1)) ;
            }
            if(!file.exists())
            {
                File fold=new File(file.getParent());
                fold.mkdirs();
            }
            fi.write( file ) ;
            System.out.println("Uploaded Filename: " + fileName + "<br>");
         }
      }
      }catch(Exception ce)
      {
            out.println("<font size='30' color='red'>Error Code 016</font>");
        //out.println("Exception1: "+ce);
      }
 //read uploaded file and insert into table********************************************  

    //      String newline = System.getProperty("line.separator");
//  File file = new File(path+"//"+fileName);
            //  file.createNewFile();
try{

    if(file.isFile())
    {
         br = new BufferedReader(new FileReader(file));
    String str="";
    String temp[]=null;


        file.canWrite();
        file.canRead();
        file.setWritable(true);

        db.conn.setAutoCommit(false);
        while((str=br.readLine())!=null)
                    {
                        temp=str.split("    ");

                //      PreparedStatement ps = db.conn.prepareStatement("INSERT INTO file_proc VALUES(?,?,?,?,?,?,?,?,?,STR_TO_DATE(?,'%m/%d/%Y'))");
                        PreparedStatement ps = db.conn.prepareStatement("INSERT INTO file_proc(run_date,zone,location,bank,file_type,num_rec,ex_sett_date,ex_stat_date) "
                                                        + "VALUES(STR_TO_DATE(?,'%m/%d/%Y'),?,?,?,?,?,STR_TO_DATE(?,'%m/%d/%Y'),STR_TO_DATE(?,'%m/%d/%Y'))");
                        ps.setString(1,temp[0]);
                        ps.setString(2,temp[1]);
                        ps.setString(3,temp[2]);
                        ps.setString(4,temp[3]);
                        ps.setString(5,temp[4]);
                        ps.setInt(6,Integer.parseInt(temp[5]));
                        ps.setString(7,temp[6]);
                        ps.setString(8,temp[7]);
                        ps.executeUpdate();
                        ps.close();     

                    }   
                    db.conn.commit();
                    db.conn.setAutoCommit(true);
                    db.conn.close();
                    br.close(); 
        file.delete();  

       //  RequestDispatcher rd = req.getRequestDispatcher("./status/status.jsp");
        //  rd.forward(req, res);
          out.print("success");
     //     out.println("</html>");
    }
    else
    {
        out.println(file+" is not a file");
    }           

   }catch(Exception ex) {
    out.println("file name= "+fileName);
      // out.println("DBEX= "+ex );
        out.println("<font size='30' color='red'>Error Code 017 - Recommended date format = m/d/yyyy.</font>");
        out.println("<font size='30' color='red'>Check the column order</font>"+ex);    
      //file.delete();  
     //  RequestDispatcher rd = req.getRequestDispatcher("./status/error.jsp");
//  rd.forward(req, res);
   }finally{
                    try{
                        db.conn.close();
                        br.close();
                    }catch(Exception e){}
                }
   }
    @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, java.io.IOException {

       doPost(request, response);  
   } 
}

** * * Here is form * ** * ** ** * *这是表格* ** * **

<form name="file_proc" id="file_proc" method = "post" action="../file_upld" enctype="multipart/form-data">
    <br/><br/><br/><br/>
    <p align="center">
<input type="file" id="file" name="file" size="50" /><br/>
<br/>
<input type="submit" class="buttons" value="Upload File" />br/><br/><br/>
     </p>
</form>

here is the log4j stuff, it shows the error at filter.java, which i am not using in the current servlet. 这是log4j的东西,它显示了filter.java中的错误,我没有在当前的servlet中使用它。

[ERROR] 29:05(file_upld.java:doPost:172) failed! [错误] 29:05(file_upld.java:doPost:172)失败了!

java.lang.NullPointerException at file_proc.file_upld.doPost(file_upld.java:120) at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at filter.filter.doFilter(filter.java:16) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) at org.apache.ca java.lang.NullPointerException在file_proc.file_upld.doPost(file_upld.java:120)的javax.servlet.http.HttpServlet.service(HttpServlet.java:641)javax.servlet.http.HttpServlet.service(HttpServlet.java: 722)atg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)atg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)at filter.filter.doFilter(filter.java :16)org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)org.apache.catalina.core。 StandardWrapperValve.invoke(StandardWrapperValve.java:225)位于org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)org org.apache.ca上的.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) talina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) 位于org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)的talina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java: 118)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler .process(AbstractProtocol.java:565)atg.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:307)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)at at Java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)at java.lang.Thread.run(Thread.java:722)

Here goes the filter.javapackage filter; 这里是filter.javapackage过滤器;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;

public class filter implements Filter{

    private FilterConfig config=null;
    @Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException 
{
    HttpServletResponse hsr = (HttpServletResponse) res;
    hsr.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    hsr.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    hsr.setDateHeader("Expires", 0); // Proxies.
    chain.doFilter(req, res);
}
    @Override
    public void destroy() { }
    @Override
  public void init(FilterConfig config) {
  this.config = config;
  }
}

Your concrete problem is caused by bad exception handling and tight-coupling view with the controller. 您的具体问题是由错误的异常处理和与控制器的紧耦合视图引起的。

After filtering noise from your code, it goes roughly like: 过滤代码中的噪音后,大致如下:

File file = null;

try {
    // ... 
    file = new File(...);
    // ... 
} catch (Exception e) {
    out.println("...");
}

try {
    if (file.isFile())
        // ...
    } else {
        // ...
    }
} catch (Exception e) {
    out.println("...");
}

The NullPointerException is been thrown at the line with the if (file.isFile()) block. if (file.isFile())块的行中抛出了NullPointerException This thus means that the first try block threw an exception, hereby leaving the file as null . 这意味着第一个try块引发了异常,因此将file保留为null

The cause of the problem is two-fold: 问题的原因有两方面:

  1. The first try block is not returning from the servlet method on exception, but incorrectly continuing the code flow. 第一个try块在异常时没有从servlet方法返回,而是错误地继续代码流。
  2. The second try block is not checking beforehand if the file is not null . 如果file不为null则第二个try块不会事先检查。

Your concrete problem is however much bigger. 然而,你的具体问题要大得多。 You're completely swallowing exceptions and printing irrelevant HTML code instead of throwing them through and/or logging the exceptions. 您完全吞下异常并打印不相关的HTML代码,而不是抛弃它们和/或记录异常。

Replace your catch blocks as follows: 替换您的catch块如下:

} catch (Exception e) {
    throw new ServletException(e);
}

By default, this way the exception will be logged and be displayed in its full glory, complete with the stacktrace, in a HTTP 500 error page. 默认情况下,这种异常将被记录并在HTTP 500错误页面中以堆栈跟踪的形式显示其完整的荣耀。 The stacktrace gives you a wealth of information to understand the problem and fix it. 堆栈跟踪为您提供了丰富的信息,以便了解问题并进行修复。


Unrelated to the concrete problem, there are many other conceptual and design mistakes in the code, but they are so far not directly related to the concrete problem. 具体问题无关 ,代码中存在许多其他概念和设计错误,但到目前为止它们与具体问题没有直接关系。 I would however recommend to take a pause and go through some sane Servlet books/tutorials/resources. 但是我会建议暂停并仔细阅读一些理智的Servlet书籍/教程/资源。 This code seems to be just cobbled together based on snippets found in Google instead of being well thought out written. 这段代码似乎只是根据Google中的代码段拼凑而成,而不是经过深思熟虑。 The first step would be understanding how servlets actually work: How do servlets work? 第一步是了解servlet的实际工作方式: servlet如何工作? Instantiation, sessions, shared variables and multithreading 实例化,会话,共享变量和多线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM