无法从Servlet 3.0中的HttpServletRequest读取多部分数据

时间:2018-06-11 05:38:55

标签: servlets java-ee httprequest struts multipartform-data

我将表单数据从ajax post方法发送到后端(java + struts)。

$( "#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 :(得分:1)

我提出了另一种方法来做到这一点。我使用了apache commons 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);
                }
            }