Servlets 无法从Servlet 3.0中的HttpServletRequest读取多部分数据
我将表单数据从ajax post方法发送到后端(java+struts)Servlets 无法从Servlet 3.0中的HttpServletRequest读取多部分数据,servlets,jakarta-ee,httprequest,struts,multipartform-data,Servlets,Jakarta 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.
$( "#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);
}
第一个记录器显示一个空行,第二个记录器给出和null指针异常。我做错了什么?
以下是我的应用程序的依赖项
<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>
org.apache.struts
支柱核心
1.3.10
antlr
antlr
org.apache.struts
struts extras
1.3.10
org.apache.struts
struts标记库
1.3.10
爪哇
javaeewebapi
6
假如
log4j
log4j
1.2.17
通用编解码器
通用编解码器
1.10
org.apache.commons
commons-lang3
3.4
net.sf.flexjson
flexjson
3.3
泽西岛
球衣束
1.19
泽西岛
泽西服务器
1.19
泽西岛
泽西servlet
1.19.4
org.codehaus.jettison
抛弃
1.3.5
我想出了一种不同的方法。我使用了ApacheCommonsFileUpload
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);
}
}
您是否已将MultipartConfig注释添加到WebServlet?是的,我已将其添加到类的顶部。您为什么要这样做而不仅仅使用Struts?如何仅使用Struts?