when multipart is used in jsp it passing null values to controller why?

i want to upload image file to database as blob when i use multipart in html it not passing any values.. Please someone help me

My html code

<c:url var="addTicketUrl" value='/ticket/saveTicket'/>
                <form:form method="POST" modelAttribute="ticketScreenObject" id="addTicketForm" enctype="multipart/form-data" action="${addTicketUrl}">
                <div id="page-heading-container">
                    <nav>
                     <input type="submit" value="Save" class="save">
                    </nav>
                    <div class="page-heading settings-icon">Add Ticket</div>
                </div>
<tr>
        <td class="table-content-alt"><form:label for="file" path="file">itemImage:</form:label></td>
        <td class="table-content-alt"><form:input path="file" type="file" /></td>
    </tr>
    </table>

my controller

@SuppressWarnings("unused")
    @RequestMapping(value = "/saveTicket", method = RequestMethod.POST)
    public @ResponseBody String addTicket(Model model,
            @ModelAttribute("ticketScreenObject") @Valid TicketScreenObject ticketScreenObject,
            BindingResult result, RedirectAttributes redirectAttributes) throws SerialException, SQLException, IOException {
        LOGGER.debug("Implementation for addTicket method begins.");
        if (result.hasErrors()) {
            getDefaultData(ticketScreenObject);
            model.addAttribute("ticketScreenObject", ticketScreenObject);
            return "ticket/addTicketForm";
        }
        List<TicketScreenObject> list = new ArrayList<TicketScreenObject>();
        if (ticketScreenObject.getCustomerId() != null) {
            Customer customer = customerService.getCustomerById(ticketScreenObject.getCustomerId() );
            Ticket ticket = new Ticket();
            ticket.setNsFwCustomer(customer);
            ticket.setTitle(ticketScreenObject.getTicketTitle());
            if(ticketScreenObject.getDescription() != null){
                ticket.setDescription(ticketScreenObject.getDescription());
            }else {
                ticket.setDescription(Constants.EMPTY_STRING);
            }
            ticket.setPriority(ticketScreenObject.getPriority());
            ticket.setSeverity(ticketScreenObject.getSeverity());
            ticket.setStatus(ticketScreenObject.getStatus());

            TicketType ticketType = ticketService.getTicketTypeById(Integer.parseInt(ticketScreenObject.getTicketTypeId()));
            ticket.setNsFwTicketType(ticketType);

            ticket.setIsDeleted(Constants.IS_DELETED_NO);
            ticket.setCreatedStamp(new Date());
            ticket.setLastModifiedStamp(new Date());
            ticket.setCreatedUser((Integer)httpServletRequest.getSession().getAttribute(Constants.EMP_DB_ID_IN_SESSION));
            ticket.setLastModifiedUser((Integer)httpServletRequest.getSession().getAttribute(Constants.EMP_DB_ID_IN_SESSION));

            ticketScreenObject.setItemImage(ticketScreenObject.getFile().getInputStream());
            ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            Blob blob = new javax.sql.rowset.serial.SerialBlob(IOUtils.toByteArray(ticketScreenObject.getItemImage()));
            //byte[] fileData = dataStream.toByteArray();
            //ticket.setFilename(file.getOriginalFilename());
            int blobLength = (int) blob.length();  
            byte[] blobAsBytes = blob.getBytes(1, blobLength);
            ticket.setTicketFileData(blobAsBytes);
            blob.free();
            //ticket.setTicketFileData(file.getContentType());
            try {
                ticketService.addTicket(ticket);
            } catch (Exception e) {
                LOGGER.error("An exception occured while adding ticket.",e);
            }
        }
        model.addAttribute("ticketScreenObject", list);
        redirectAttributes.addFlashAttribute("SUCCESS_MESSAGE_TICKET_ADDED",
                ticketScreenObject.getTicketId());
        LOGGER.debug("Implementation for addImplementation method ends.");
        return "redirect:/ticket/ticketList";
    }

i'm using separate pojo class for ticketScreenObject and ticket is object of Ticket.java is model class.

In TicketScreenObject contains

private Multipart file;

private inputStream itemImage;

Answers


Visit https://docs.oracle.com/javaee/6/tutorial/doc/glrbb.html and you find out that you missed @MultipartConfig annotation .


Need Your Help

Dropdowmenu: Align submenus to their parents dynamically with JQuery

jquery html5 css3 drop-down-menu

I have a dropdown menu where what i want to do is to change the padding left of the submenu dynamically so the parent and the sibling (the submenu) are aligned vertically.

Finding the shortest paths of a directed graph C++

c++ graph graph-algorithm dijkstra shortest-path

Over the last week, I have implemented a Digraph by parsing an input file. The graph is guaranteed to have no cycles. I have successfully created the graph, used methods to return the number of ver...