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 阅读(1292) 评论(0)  编辑  收藏 所属分类: 网络资料

只有注册用户登录后才能发表评论。


网站导航: