posts - 5,  comments - 7,  trackbacks - 0
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          jsf

          搜索

          •  

          積分與排名

          • 積分 - 56061
          • 排名 - 925

          最新評論

          閱讀排行榜

          評論排行榜

          后臺實現(xiàn)類:

           

          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;

          import javax.faces.context.FacesContext;

          import org.apache.myfaces.custom.fileupload.UploadedFile;


          public class Fileupload
          {
              
          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();
                  
          int start=_upFile.getName().lastIndexOf("\\")==-1?_upFile.getName().lastIndexOf("\\"):_upFile.getName().lastIndexOf("\\")+1;
                  String file
          =_upFile.getName().substring(start,_upFile.getName().length());
                  String path
          =Fileupload.class.getResource("/").getPath().substring(1,Fileupload.class.getResource("/").getPath().length());
                  path
          =path.replace("%20"" ")+file;
            
          try {
             FileOutputStream keyFOS 
          = new FileOutputStream(path);
             keyFOS.write(_upFile.getBytes());
             keyFOS.close();
            }
           catch (Exception e) {
             System.out.println(
          "lalala");
            }

            
                  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 path;
              }


          public boolean isUploaded()
              
          {
                  FacesContext facesContext 
          = FacesContext.getCurrentInstance();
                  
          return facesContext.getExternalContext().getApplicationMap().get("fileupload_bytes")!=null;
              }

          }


           

          前臺實現(xiàn):Myfaces

           

          <t:inputFileUpload id="fileupload"
                                         value
          ="#{action$fixing.myFile}"         
                                         storage
          ="file"
                                         styleClass
          ="fileUploadInput"
                                         maxlength
          ="200000"/>
                
          <h:commandButton value="#{prompt['Submit']}"
                 action
          ="#{action$fixing.sel}" />

           

           

          后臺管理Bean:

          import org.apache.myfaces.custom.fileupload.UploadedFile;

           

          public class FixingAction{

          private UploadedFile myFile;

           

          public UploadedFile getMyFile() {
            return myFile;
           }

           

           

          public void setMyFile(UploadedFile myFile) {
            this.myFile = myFile;
           }


              public String sel(){
                 Fileupload file=new Fileupload();

          String path=file.setUpFile(this.myFile);

          System.out.println("文件存放路徑:"+path);

          return null;

            }
          }
          posted on 2008-11-27 11:17 Vincent-chen 閱讀(397) 評論(0)  編輯  收藏 所屬分類: JSF
          主站蜘蛛池模板: 丹东市| 夹江县| 夏邑县| 朝阳县| 军事| 察雅县| 包头市| 潼关县| 吴桥县| 浠水县| 绥江县| 萨嘎县| 永寿县| 喀什市| 尉犁县| 尖扎县| 观塘区| 印江| 鲁甸县| 道真| 广元市| 民权县| 陵川县| 刚察县| 龙里县| 四子王旗| 西乌珠穆沁旗| 朔州市| 泸定县| 政和县| 大厂| 昌乐县| 崇左市| 南投市| 万年县| 岚皋县| 河间市| 吉林市| 杭锦后旗| 吉安市| 应城市|