0

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

nubteens
  • 5,462
  • 4
  • 20
  • 31
  • Can you put in a println statement before your if statement? Just check and verify that if it is actually hitting the while loop and not stopping in your if statement – ryekayo May 04 '15 at 17:58
  • @ryekayo It is not hitting the while loop – nubteens May 04 '15 at 17:59
  • What is the request variable? That is not shown anywhere in your code. Can you add that in your question? – ryekayo May 04 '15 at 18:00
  • Are you in servlet parsing a multipart/form-data stream? – Aninda Bhattacharyya May 04 '15 at 18:01
  • List items = upload.parseRequest(request); - Means you are trying to assign multiple files at a time to the List. I don't exactly know if this is wrong or not. Try to get 1 file at a time. For example see this: http://stackoverflow.com/questions/13048939/file-upload-with-servletfileuploads-parserequest – The Guest May 04 '15 at 18:03
  • 1
    After seeing your edit I can say that : upload.parseRequest(request); is causing you the error.Go to the link I have provided in my earlier comment and see how the FileItem is handled there – The Guest May 04 '15 at 18:05
  • @TheGuest I tried the one in the link but it is still not hitting the while loop – nubteens May 04 '15 at 18:19
  • @nubteens Check whether you are successfully getting the file into your 'List' or not. For example try printing out List.lengt() i.e. items.length() and see what's there in the List – The Guest May 04 '15 at 18:25

0 Answers0