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 閱讀(1297) 評論(0)  編輯  收藏 所屬分類: 網絡資料
          主站蜘蛛池模板: 南丹县| 怀宁县| 贡觉县| 车致| 丰县| 海原县| 广元市| 泰顺县| 青州市| 阜平县| 建水县| 昂仁县| 柘城县| 合川市| 宁城县| 石河子市| 布拖县| 崇左市| 娱乐| 博爱县| 安庆市| 赫章县| 建水县| 高尔夫| 句容市| 西平县| 漳平市| 宁津县| 广州市| 凤冈县| 洛扎县| 玉龙| 湾仔区| 常州市| 密山市| 怀仁县| 云南省| 淅川县| 张北县| 纳雍县| 高邮市|