2009-10-01 38 views
5

我遇到了使用spring webflow 1.0和struts 1.3上傳文件的問題。Struts - 上傳文件

的JSP是這樣的:

<html:form action="/flowAction" method="post" enctype="multipart/form-data"> 
    <!-- snip --> 
    <html:file property="file" name="attachDocumentsForm" size="50"/> 
    <!-- snip --> 
</html:form> 

的形式是這樣的:

public class AttachDocumentsForm extends SpringBindingActionForm { 
    // note, SpringBindingActionForm extends struts' ActionForm 
    private FormFile file; 
    //snip 
} 

現在,我的問題是,當我提交表單,該file場總是null 。表單上的其他字段被正確填寫,如果我深入瞭解RequestContext,我可以發現文件被深埋在那裏的一些數據結構中。

這裏是可怕醜陋的方式,我可以在附件獲得:

// 'context' is the RequestContext 
ServletExternalContext servletExternalContext = (ServletExternalContext) context.getExternalContext(); 
ActionForm form = (ActionForm) servletExternalContext.getRequest().getAttribute("actionForm"); 
FormFile file = (FormFile) form.getMultipartRequestHandler().getFileElements().get("file"); 

我已經注意到,我的窗體上的MultipartRequestHandlernull,我懷疑這可能是部分問題,但我已經嘗試用CommonsMultipartRequestHandler實例填充它無濟於事。

我需要做些什麼才能讓file字段正確填充?

+0

你不應該能夠得到從ActionForm中訪問該文件,使用(FormFile)form.getFile()? – 2009-10-01 02:37:42

回答

0

在您的ActionFormattachDocumentsForm提及的一個)中添加一個名爲FormFile的文件的字段以及訪問器方法。

上傳的文件可以通過調用form.getFile()進行訪問。

我希望這有助於!

+0

謝謝,但那只是問題所在。我已經有了ActionForm中的其中一個字段,但它始終爲空。 – 2009-11-06 20:28:01

+0

我不知道自2006年以來是否有人經歷過這個問題,但是我正在處理的遺留項目中存在完全相同的問題......如果有人有解決方案,那就太好了:-) – Philippe 2010-03-23 20:02:56

+0

同樣的問題這裏。 – 2015-03-20 19:18:52