我在文件上传中获取空值我的代码在下面....请检查并让我知道缺少什么
@Component("businessInformationBean")
@Scope("view")
public class BusinessInformationBean extends AbstractBaseBean {
private static final Logger LOGGER = Logger
.getLogger(BusinessInformationBean.class);
private static final long serialVersionUID = 1L;
@Autowired
private BusinessInformationService businessInformationService;
@Value("#{addressBean}")
private AddressBean address;
private String firmName;
private Date openingDate;
private Date activityDate;
private String businessType;
private Date seasonStartDate;
private Date seasonEndDate;
private Date closerDate;
private String natureOfBusiness;
private UploadedFile file;
private String bankAccholdername;
private String bankName;
private String bankBranchName;
private String accountNumber;
private String accountType;
private String bankAddress;
private String bankIFSCCode;
private String bankCity;
private String bankDistrict;
private String bankState;
private Integer bankZipcode;
public void save(ActionEvent event) {
LOGGER.info("businessInformationBean Save");
try
{
FacesContext fc = getFacesContext();
if (frmValidation())
{
MmBusinessInformation businessInformation = businessInformationService
.fetchBusinessInformationServiceByLoginUser(getCurrentUser());
mapWebBackToDomain(businessInformation);
businessInformationService.saveOrUpdate(businessInformation);
renderRecordSaveMessage();
}
else
{
fc.renderResponse();
}
}
catch (Exception e)
{
LOGGER.error(e.getMessage());
renderFatalError();
}
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
我的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ems-web</display-name>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>redmond</param-value>
</context-param>
<!-- Spring Security Facelets Tag Library -->
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/springsecurity.taglib.xml</param-value>
</context-param>
<context-param>
<param-name>primefaces.FONT_AWESOME</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/security-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/home/alekss/uploads</param-value>
</init-param>
<init-param>
<description>Maximum size of stored in memory file</description>
<param-name>thresholdSize</param-name>
<param-value>10240000</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring security filter -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</web-app>
和.xhtml文件代码在
之下 <p:fileUpload id="fileUp" fileUploadListener="#{businessInformationBean.file}" mode="commons" />
请给我一些遗漏的建议..很多让我迷惑的... 在此先感谢。
答案 0 :(得分:0)
p:fileUpload中的fileUploadListener属性需要一个带有FileUploadEvent参数的方法,如下所示:
public void handleFileUpload(FileUploadEvent event) {
//try {
UploadedFile uf = event.getFile();
//uf.getFileName();
//uf.getInputstream();
//} catch (IOException e) {
//Do something
//}
}
<p:fileUpload id="fileUp" fileUploadListener="#{businessInformationBean.handleFileUpload}" mode="commons" />
然后,您可以获取流,文件名等
同时检查您的表单是否具有enctype属性。
<h:form id="myForm" enctype="multipart/form-data">