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 閱讀(1302) 評論(0)  編輯  收藏 所屬分類: 網絡資料
          主站蜘蛛池模板: 永寿县| 绥阳县| 安塞县| 巴东县| 马公市| 周至县| 会理县| 建宁县| 德令哈市| 鸡东县| 丰顺县| 清镇市| 石泉县| 若尔盖县| 屏东县| 班戈县| 广宁县| 乐山市| 偃师市| 宜兴市| 深水埗区| 布尔津县| 四川省| 临清市| 中卫市| 镇平县| 顺义区| 岳池县| 额尔古纳市| 永济市| 武定县| 漳州市| 吉林省| 拜城县| 华安县| 甘洛县| 阿瓦提县| 巴林左旗| 建平县| 漳平市| 文化|