lbom

          小江西

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            18 隨筆 :: 21 文章 :: 69 評(píng)論 :: 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) 評(píng)論(0)  編輯  收藏 所屬分類: 網(wǎng)絡(luò)資料
          主站蜘蛛池模板: 玉溪市| 内乡县| 民县| 高州市| 邯郸市| 包头市| 南雄市| 册亨县| 三亚市| 远安县| 凤山县| 大埔县| 涟源市| 宝丰县| 江阴市| 长兴县| 泰来县| 迭部县| 墨江| 龙门县| 吉林省| 房山区| 郧西县| 峨山| 辽源市| 鹤山市| 当雄县| 洛南县| 都兰县| 涟水县| 荣成市| 高台县| 邯郸县| 巩留县| 若羌县| 台中市| 盐源县| 张家港市| 阿勒泰市| 洞口县| 凤山县|