Java学习

java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已经搬家了,新的地址是 http://www.javaly.cn 如果有对文章有任何疑问或者有任何不懂的地方,欢迎到www.javaly.cn (Java乐园)指出,我会尽力帮助解决。一起进步

 

JSF实现文件上传(2)

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().put("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>

posted on 2008-10-28 15:20 找个美女做老婆 阅读(396) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

本blog已经搬到新家了, 新家:www.javaly.cn
 http://www.javaly.cn

常用链接

留言簿(6)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜