问题能解密:我想创建一个使用JSP文件上传画面。 屏幕将让用户在屏幕上选择多个文件,但只会有一个上传按钮,所有的人。 上传按钮的点击所有的文件对象应该在Action类中获得。
但重要的是页面不应该提交后得到刷新。 会有哪些不应该改变在文件上载过程中在同一个屏幕上显示的其他信息。
我尝试:我用简单的Struts2的文件上传功能,工作正常。 但是令人耳目一新的提交页面。 我用AJAX(JQuery的)来解决这个问题。 我与AJAX面临的问题是,它不设置File对象到Action类的文件属性。 因此,我不能够获得Action类的文件对象和进一步处理。
任何人都可以请帮我这。
我附上什么的我至今尝试过的代码。
JSP:
<s:form action="fileUpload" method="post" enctype="multipart/form-data" >
<s:file id="file" name="userImage" cssClass="fileUpload" cssStyle="fileUpload" />
<button id="px-submit">Upload</button>
</s:form>
<script type="text/javascript">
jQuery(function($){
$('.fileUpload').fileUploader();
});
</script>
jQuery插件 : 这是我使用的jQuery插件。
Action类 :
public class FileUploadAction extends ActionSupport{
private File userImage;
public File getUserImage() {
return userImage;
}
public void setUserImage(File userImage) {
this.userImage = userImage;
}
public String execute()
{
try
{
System.out.println("file name: " + userImage.toString());
}
catch(Exception e)
{
e.printStackTrace();
}
return SUCCESS;
}
编辑:这是我的支柱配置文件。
在struts.xml
<action name="commonDataImportAction_*" class="xxx.Action">
<result name="SUCCESS" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
我在这里得到一个空指针作为文件对象没有被设置。
请帮忙。
提前致谢。 拉夫
我使用的是相同的插件,它是对我工作的罚款。 我在你的代码中看到的第一个问题是,你没有设置你的上传按钮提交类型。
<button id="px-submit" type="submit">Upload</button>
我们希望,这应该能够解决空指针excepton。 此外,如在此插件的文档中提到,你需要返回一个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',
});
Action类
你需要返回流的结果。 我使用的插件( struts2的jQuery插件 ),它采用的是变化很好地照顾,但你不必使用它,只是因为这一要求,而不是我给你一个代码,而无需使用任何插件返回流的结果。(摘自从这里 )
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>
添加一个target
属性,您的形式和具有相同名称的iframe中。 直接提交多部分形式的动作,而无需使用任何插件和响应将在iframe加载。 这将解决您的页面刷新的问题。 如果你需要使用插件的话,我会建议使用http://valums.com/ajax-upload/这将使用XHR / Ajax的现代浏览器和优雅降级到IFRAME上传在旧的浏览器。