我正在使用相同的插件,它对我来说工作得很好。我在您的代码中看到的第一个问题是您尚未设置要提交的上传按钮的类型。
<button id="px-submit" type="submit">Upload</button>
希望这应该可以解决空指针异常。
另外,正如这个插件文档中提到的,您需要返回一个 json 字符串
<div id='message'>success message</div>
上传成功后。所以您需要更改 struts.xml 映射。
尝试一下,如果您遇到任何其他问题,请回复我。
编辑:
好的,这是您要求的我的代码
JSP
<form action="uploadImage" method="post" enctype="multipart/form-data">
<input type="file" name="image" class="fileUpload" multiple/>
<button id="px-submit" type="submit">Save all images</button>
<button id="px-clear" type="reset">Clear all</button>
</form>
$('.fileUpload').fileUploader({
autoUpload: false,
buttonUpload: '#px-submit',
buttonClear: '#px-clear',
});
动作类
您需要返回流结果。我正在使用一个插件(struts2 jquery 插件)它的处理方式各不相同,但是你dont仅因为这一要求而必须使用它,相反,我为您提供了一段代码来返回流结果,而不使用任何插件。(取自here)
public class UploadImageAction extends ActionSupport{
private File image;
private String imageContentType;
private String imageFileName;
//getter/setter for these
public String execute() {
String status="";
try{
//save file code here
status="<div id='message'>successfully uploaded</div>"; //on success
inputStream = new StringBufferInputStream(status);
}catch(WhateverException e){
status="<div id='status'>fail</div><div id='message'>Your fail message</div>"; //on error
inputStream = new StringBufferInputStream(status);
//other code
}
return SUCCESS;
}
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
}
struts.xml
<action name="fileUpload" class="com.xxx.action.UploadImageAction">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>