Не могу прочитать составные данные из HttpServletRequest в Servlet 3.0 - PullRequest
0 голосов
/ 11 июня 2018

Я отправляю данные формы в бэкэнд (java + struts) из метода записи ajax.

$( "#profileModalForm" ).submit(function( event ) {
    var formData = new FormData(this);
    formData.append('image', $('input[type=file]')[0].files[0]); 
    formData.append('section', 'general');
    formData.append('action', 'previewImg');

    $.ajax({
        cache: false,
        url: 'SaveProfilePopupData.ws',
        type: "POST",
        data: formData,        
        contentType: false,
        processData: false,
        success: function (html) {
            $('#notificationArea').html(html);
        }
    });
    event.preventDefault();
});

Консоль браузера отображает их как отправленные параметры.

-----------------------------181712305727018
Content-Disposition: form-data; name="hiddenIdTxt"

1000
-----------------------------181712305727018
Content-Disposition: form-data; name="passwordTxt"

abcd
-----------------------------181712305727018
Content-Disposition: form-data; name="repeatPasswordTxt"

abcd
-----------------------------181712305727018
Content-Disposition: form-data; name="fileInput"; filename="myImage.jpg"
Content-Type: image/jpeg

Øÿà.........(Some unreadable data here)

Но я не могучитать параметры и части из моего HttpServletRequest.

public ActionForward saveProfilePopupData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    TransactionResult result = TransactionResult.getInstance();
    if (WSUtil.getCurrentUser(request) != null) {
        try {

            for (Part part : request.getParts()) {
                //First Logger
                WSLogger.info(part.getSize());
            }
            //Second Logger
            WSLogger.info(request.getParameter("hiddenIdTxt").toString());

            result.setResultMessage("Profile Updated!");
            result.setResultType(TransactionResultType.SUCCESS);
        } catch (Exception ex) {
            WSLogger.error("UserController - saveProfilePopupData Exception", ex);
        }
    } else {
        return mapping.findForward(SESEXPIRE);
    }
    request.setAttribute("RESULT", result);
    return mapping.findForward(SUCCESS);
}

Первый регистратор показывает пустую строку, а второй регистратор дает и исключение nullpointer.Что я здесь не так делаю?Вот зависимости для моего приложения.

    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-core</artifactId>
            <version>1.3.10</version>
            <exclusions>
                <exclusion>
                    <artifactId>antlr</artifactId>
                    <groupId>antlr</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-extras</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-taglib</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>net.sf.flexjson</groupId>
            <artifactId>flexjson</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19</version>
        </dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.19.4</version>
</dependency>
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.3.5</version>
        </dependency>
    </dependencies>

1 Ответ

0 голосов
/ 11 июня 2018

Я придумал другой способ сделать это.Я использовал apache fileupload.

            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iterator = upload.getItemIterator(request);
            while(iterator.hasNext()){
                FileItemStream item = iterator.next();
                InputStream stream = item.openStream();
                if(item.isFormField()){
                    if(item.getFieldName().equals("hiddenIdTxt")){
                        byte[] str = new byte[stream.available()];
                        stream.read(str);
                        id = Integer.parseInt(new String(str,"UTF8"));
                    }
                    if(item.getFieldName().equals("passwordTxt")){
                        byte[] str = new byte[stream.available()];
                        stream.read(str);
                        password1 = new String(str,"UTF8");
                    }
                    if(item.getFieldName().equals("repeatPasswordTxt")){
                        byte[] str = new byte[stream.available()];
                        stream.read(str);
                        password2 = new String(str,"UTF8");
                    }
                }else{
                    byte[] str = new byte[stream.available()];
                    stream.read(str);
                    imageBase64String = Base64.encodeBase64String(str);
                }
            }
...