List<FileItem> is null when uploading file

828 Views Asked by At

I am trying to store a file along with two other values which are inputted in the text fields (orderid2 and remarks) in the database. However, the List< FileItem > is null. I do not know what is wrong with my code.. It is not doing the while loop.

DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while(iter.hasNext()) {
    FileItem item = iter.next();
    if(item.isFormField()) {
    String name = item.getFieldName();
    String value = item.getString();
    System.out.println(name + " " + value);
        if(name.equals("orderid2"))
            order.setOrderID(Integer.parseInt(value));
        else if(name.equals("remarks"))
            order.setRemarks(value);
    }
}

P.S. I actually think that the problem is in the request variable. I have this whole code in my servlet inside this function protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, FileUploadException, SQLException {
I do not really know what should be the parameter of the upload.parseRequest(). I just copied this somewhere

0

There are 0 best solutions below