求助!JSF中的x:inputFileUpload
空指针问题
求助!x:inputFileUpload抛出
空指针异常 这是JSP页面部分代码
<x:inputFileUpload id= "myFileId " value= "#{addCrsRoom.upfile} "
storage= "file " required= "true " />
<hanelGrid columns= "2 " border= "1 " width= "60% ">
<h:commandButton type= "submit " value= "提交 "
action= "#{addCrsRoom.insert} "> </h:commandButton>
<h:commandButton type= "reset " value= "重置 "> </h:commandButton>
</hanelGrid>
配置文件
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN " "http://java.sun.com/dtd/web-facesconfig_1_1.dtd ">
<faces-config>
<managed-bean>
<managed-bean-name> addCrsRoom </managed-bean-name>
<managed-bean-class>
neusoft.action.manage.AddNewCr
</managed-bean-class>
<managed-bean-scope> session </managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id> /pages/manage/addcrs.jsp </from-view-id>
<navigation-case>
<from-outcome> successadd </from-outcome>
<to-view-id> /pages/common/success.jsp </to-view-id>
</navigation-case>
<navigation-case>
<from-outcome> fail </from-outcome>
<to-view-id> /pages/common/fail.jsp </to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
BEAN文件部分代码
private UploadedFile upfile;
public UploadedFile getUpfile() {
return upfile;
}
public void setUpfile(UploadedFile upfile) {
this.upfile = upfile;
}
System.out.println( "1 ");
System.out.println(upfile.getName());
System.out.println(getFileName(upfile.getName()));
InputStream is = upfile.getInputStream();
System.out.println( "2 ");
String filename = getFileName(upfile.getName());
System.out.println(filename);
FileOutputStream fos = new FileOutputStream( "D:/ " + filename);
System.out.println( "over ");
int isleft;
while ((isleft = is.read()) != -1)
fos.write(isleft);
System.out.println( "3 ");
is.close();
fos.close();
System.out.println( "4 ");
在console里面 只能输出一个1 还有就是空指针异常了
我只是想用JSF在前台读入一张图然后存到指定位置
高手指教错在哪
或者还有没有其他用JSF解决上传存储图片的方法
谢谢各位!
------解决方案--------------------richfaces
------解决方案--------------------System.out.println( "1 ");
System.out.println(upfile.getName());
输出一个1后执行System.out.println(upfile.getName());
这里解释下空指针出现的可能性:
如果有一个空的值或者对象,是不能点出他的属性,因为空是没有属性的,因此到这句出现空指针异常很有可能是本类的upfile为空(或者upfile对象没有初始化)