Java – publishes formdata objects using the input type and the file NullPointerException

I'm trying jQuery Ajax publishing using input type files and normal input types with text, and using request Getparameter ("element_name") retrieves them from my servlet, but when using the chrome checker, I see the object of formdata. I send my file and my text value, and the servlet reads the parameter as null for some reason

This is my form: (ticket_id successfully returned from another JSP)

<form id="upload-form" action="upload" enctype="multipart/form-data" method="post">
    <input id="attach-btn" type="file" name="uploadedFile" style="display:none"/>
    <input id="tick-id-upload" type="hidden" name="ID" value="<%=ticket_id%>" />            
    <input id="submit-form" type="button" style="display:none"/>
</form>

This is a post from jQuery Ajax:

// use to refresh section on submit of a form
$(document).on('click','#submit-form',function()
{
    var form_data = null;

    if (drop === false)
    {
        // form data object with ticket id and file
        form_data = new FormData($('#upload-form')[0]);
    }
    else
    {
        // append dropped file and value of id seperately
        form_data = new FormData();
            form_data.append('ID',$('#tick-id-upload').val());
        form_data.append('uploadedFile',dropped_files);
    }

    $.ajax(
    {
       url: $('#upload-form').attr('action'),type: 'POST',async: true,xhr: function() // custom XMLHttpRequest
       {
            myXhr = $.ajaxSettings.xhr();

            if (myXhr.upload)
            { // check if upload property exists
                myXhr.upload.addEventListener('progress',show_progress,false); // for handling the progress of the upload
            }
            return myXhr;
       },dataType: 'html',// the data type to be returned
       success: function(response,status,xhr)
       { 
            $('#progressbar').hide();

            if (xhr.getResponseHeader('duplicate') === 'true')
            {
                // file is duplicate.. display dialog @R_460_2419@
            setTimeout(function()
            {
                   $('#trigger-dialog').trigger('click');
            },10);     
            }
            else
            {
                // replace attachments section by section in response
                    $('#attachments').html($(response).find('#attachments').html());  
                    execute_attach_datatable();
                    switch_to_view();

                    init_progress_bar();
                    override_section_height();
                }
            },error: function(xhr,error) 
            {
                alert(xhr.responseText);
            },data: form_data,// what data to pass
            cache: false,contentType: false,// type of data to be sent
            processData: false
        }); 
    });

This is what I did in the dopost method in the servlet:

int ticket_id = Integer.parseInt(request.getParameter("ID"));

This line returns NullPointerException, although the data seen from the "network" section of chrome is being sent

Please note that there is no problem uploading the file without sending the input type text That is, when I have the same form with or without the tick ID upload element and use the same jQuery Ajax call, the file upload is successful

Any idea of what happened? Thank you.

Solution

You cannot use request Getparameter () directly reads multipart / form request parameters Instead, you need to use the getpart method to read different parts of multiple partial requests The following is how to iterate over the multipart request part:

for (Part part : request.getParts()) {
}

Please visit this blog for detailed examples: http://balusc.blogspot.in/2009/12/uploading-files-in-servlet-30.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>