i have issue file uploading in jsf. have done following code ih jsf .
index.xhtml(view)
<h:panelgrid columns="2"> <h:outputlabel for="userid">user id</h:outputlabel> <h:inputtext id="userid" value="#{controller.user.username}" required="true"> </h:inputtext> <h:outputlabel for="username">username</h:outputlabel> <h:inputtext id="username" value="#{controller.user.userid}" required="true"> </h:inputtext> <h:outputlabel for="photo">profile picture</h:outputlabel> <t:inputfileupload value="#{controller.user.photo}" required="true"> </t:inputfileupload> </h:panelgrid>
user.java(model)
package com.profileapp.models; import java.io.serializable; import org.apache.myfaces.custom.fileupload.uploadedfile; public class user implements serializable { private string userid; private string username; private uploadedfile photo; public user() { } public string getuserid() { return userid; } public void setuserid(string userid) { this.userid = userid; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public uploadedfile getphoto() { return photo; } public void setphoto(uploadedfile photo) { this.photo = photo; } }
usercontroller.java(controller)
package com.profileapp.controllers; import com.profileapp.models.user; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.serializable; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.sqlexception; import javax.faces.bean.managedbean; import javax.faces.bean.requestscoped; import org.apache.commons.io.filenameutils; @managedbean(name="controller") @requestscoped public class usercontroller implements serializable { private user user=new user(); public user getuser() { return user; } public void setuser(user user) { this.user = user; } public string submit() throws ioexception ,sqlexception ,classnotfoundexception, instantiationexception, illegalaccessexception { string filename = filenameutils.getname(user.getphoto().getname()); byte[] bytes = user.getphoto().getbytes(); int index=filename.indexof('.'); string extension=filename.substring(index); file file; string path; path = "c:/users/manohar/documents/netbeansprojects/profileapplication/build/web/uploads/"+user.getusername()+(float)math.random()+"/"; if(extension.equalsignorecase(".jpg")||extension.equalsignorecase(".jpeg")||extension.equalsignorecase(".png")||extension.equalsignorecase(".gif")||extension.equalsignorecase(".tif")) { file=new file(path); if(!file.exists()) { file.mkdir(); } path=file+"/"+filename; fileoutputstream outfile=new fileoutputstream(path); outfile.write(bytes); outfile.close(); preparedstatement stmt; connection connection; string url="jdbc:mysql://localhost:3306/userprofile"; class.forname("com.mysql.jdbc.driver").newinstance(); connection = drivermanager.getconnection(url, "root", "mysql"); stmt = connection.preparestatement("insert table_profile values('"+user.getusername()+"','"+user.getuserid()+"','"+path+"')"); stmt.executeupdate(); connection.close(); return "success"; } else { return "fail"; } } }
when run code gets nullpointer execption field inputfileupolad element i.e. when debug observed setter method of user.photo field not gets called
i got following error (component tree)
<uiviewroot id="j_id1" inview="true" locale="en_us" renderkitid="html_basic" rendered="true" transient="false" viewid="/index.xhtml"> <?xml version='1.0' encoding='utf-8' ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <uioutput id="j_idt4" inview="true" rendered="true" transient="false"> <title>facelet title</title> </uioutput> <uioutput id="j_idt6" inview="true" rendered="true" transient="false"> <htmlform enctype="application/x-www-form-urlencoded" id="j_idt7" inview="true" prependid="true" rendered="true" submitted="true" transient="false"> <htmlpanelgrid border="-2147483648" columns="2" id="j_idt8" inview="true" rendered="true" transient="false"> <htmloutputlabel escape="true" for="userid" id="j_idt9" inview="true" rendered="true" transient="false"> user id </htmloutputlabel> <htmlinputtext disabled="false" id="userid" immediate="false" inview="true" localvalueset="false" maxlength="-2147483648" readonly="false" rendered="true" required="true" size="-2147483648" transient="false" valid="true" value="manu"/> <htmloutputlabel escape="true" for="username" id="j_idt11" inview="true" rendered="true" transient="false"> username </htmloutputlabel> <htmlinputtext disabled="false" id="username" immediate="false" inview="true" localvalueset="false" maxlength="-2147483648" readonly="false" rendered="true" required="true" size="-2147483648" transient="false" valid="true" value="manu"/> <htmloutputlabel escape="true" for="photo" id="j_idt13" inview="true" rendered="true" transient="false"> profile picture </htmloutputlabel> <htmlinputfileupload disabled="false" id="j_idt15" immediate="false" inview="true" localvalueset="false" maxlength="-2147483648" readonly="false" rendered="true" required="true" size="-2147483648" transient="false" valid="true"/> </htmlpanelgrid> <htmlcommandbutton action="#{controller.submit()}" actionexpression="#{controller.submit()}" disabled="false" id="j_idt16" immediate="false" inview="true" readonly="false" rendered="true" transient="false" type="submit" value="register"/> </htmlform> </uioutput> </html> </uiviewroot>
you forgot set form encoding multipart/form-data
. jsf component tree shows it's using default of application/x-www-form-urlencoded
.
fix accordingly:
<h:form enctype="multipart/form-data">
the default form enctype namely not support file uploads. don't forget register extensions filter in web.xml
per the manual. otherwise jsf won't able execute apply request values phase.
Comments
Post a Comment