myfaces,它是jsf的扩展,和jsf是完全支持的,组件用法也是一样的,这是服务器端获取你上传文件的类 ,你应该把它定义成一个managed-bean,在我的项目里面,
我把它定义为fileupload,我想managed bean的定义你应该清楚吧,后面的是文件上传组件的的应用,那个代码是页面中的应用,
import java.io.IOException;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import javax.faces.context.FacesContext;
public class FileUploadForm
{
private UploadedFile _upFile;
private String _name = "";
public UploadedFile getUpFile()
{
return _upFile;
}
public void setUpFile(UploadedFile upFile)
{
_upFile = upFile;
}
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
public String upload() throws IOException
{
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().getApplicationMap().p
ut("fileupload_bytes", _upFile.getBytes());
facesContext.getExternalContext().getApplicationMap().put("fileupload_type", _upFile.getContentType());
facesContext.getExternalContext().getApplicationMap().put("fileupload_name", _upFile.getName());
return "ok";
}
public boolean isUploaded()
{
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getExternalContext().getApplicationMap().get("fileupload_bytes")!=null;
}
}
//放在form里面
<h:form id="form1" name="form1" enctype="multipart/form-data" >
<x:inputFileUpload id="fileupload" accept="application/vnd.ms-excel"
value="#{fileupload.upFile}" storage="file"
styleClass="fileUploadInput" required="true" />
此处是按钮,点击执行managed bean:fileupload的文件上传方法:upload
<h:commandButton type="submit" action="#{fileupload.upload}" value="上传文件" styleClass="button-main-4"/>
</h:form>