lbom

          小江西

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            18 隨筆 :: 21 文章 :: 69 評論 :: 0 Trackbacks
          public class MainCtl {
              UploadedFile upFile;
          ...

              public String upload() throws IOException {
                 // This is using Hibernate to figure out what folder path
          should be used to store the file.
                  Sysctl sys = Sysctl.getRecord();

                  FacesContext fc = FacesContext.getCurrentInstance();
                  fc.getExternalContext().getApplicationMap().put("fileupload_bytes",
          upFile.getBytes());
                  fc.getExternalContext().getApplicationMap().put("fileupload_type",
          upFile.getContentType());
                  fc.getExternalContext().getApplicationMap().put("fileupload_name",
          upFile.getName());

                  String guid = (new VMID()).toString().replaceAll(":", "");
                  writeFile(upFile, sys.getUploadfolder().trim() + guid);
           
                  return null;
              }

              private void writeFile(UploadedFile uf, String file) throws IOException {
                  InputStream is = uf.getInputStream();
                  FileOutputStream fos = new FileOutputStream(file);
                  int c;
                  while ((c = is.read()) != -1) {
                      fos.write(c);
                  }
              }

              public UploadedFile getUpFile() {
                  return upFile;
              }

              public void setUpFile(UploadedFile x) {
                  upFile = x;
              }
          }

          -------jsp---------
            <x:inputFileUpload id="fileupload"
                               accept="image/*"
                               value="#{MainCtl.upFile}"
                               storage="file"
                               styleClass="input"
                               required="false"/>
          <h:commandButton value="load it up" action="#{MainCtl.upload}"
          styleClass="button"/>

          posted on 2006-02-24 12:10 lbom 閱讀(1301) 評論(0)  編輯  收藏 所屬分類: 網絡資料
          主站蜘蛛池模板: 马山县| 蒙山县| 罗定市| 双城市| 卓资县| 府谷县| 长泰县| 澄迈县| 珲春市| 庆城县| 吴桥县| 上饶市| 宜良县| 潞西市| 福州市| 普陀区| 花垣县| 平度市| 永春县| 常州市| 抚顺市| 樟树市| 娱乐| 锦州市| 旬阳县| 大渡口区| 侯马市| 大邑县| 巢湖市| 乌拉特中旗| 嘉定区| 沙雅县| 福鼎市| 博湖县| 浙江省| 安陆市| 太谷县| 海安县| 明溪县| 蓬莱市| 高要市|