我想用这个控件实现上传功能,但是不知道该怎么写我是新手
哪位朋友有代码给我贴一下吧,最好是有注释的。
页面,后台bean,配置文件,这些都应该注意些什么呢。

解决方案 »

  1.   

    http://blog.sina.com.cn/s/blog_602feaa80100guy6.html
      

  2.   

    一:采用myfaces标签进行上传<t:inputFileUpload styleClass="fileupload" value="#{QuploadBean.upFile}" size="60">
         </t:inputFileUpload>其中upFile是myfaces.custom.FileUpload.uploadedFile的一个对象,通过这样的绑定,在jsf的back bean中就可以得到上传文件的inputStream:InputStream in = upFile.getInputStream();---获得输入流     然后在服务器中生成一个以时间命名的文件,用outputStream将获得的输入byte,write到该文件中。    Date date = new Date();// 获取当前系统的时间
      SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss");// 格式化日期
      String dateStr = s.format(date); // 转为字符串
      int ms = Calendar.getInstance().get(Calendar.MILLISECOND);// 得到当前的毫秒数
      String fileName = dateStr + String.valueOf(ms) + ".xml";// 以日期命名xml文件
      String serverPath = getServerPath() + "\\upload\\";// 文件上传的目标目录
      serverPath = checkPath(serverPath);// 判断该目录是否存在,不存在则创建
      String filePath = serverPath + fileName;OutputStream os= new FileOutputStream(filePath);
       os.write(upFile.getBytes());
       os.close(); ps:commandButton的action属性主要是用于页面转向,而actionListener主要是处理一些业务。在提交页面时会先执行actionlistener,然后再执行action对应的方法。二:如何得到该工程所在的文件夹路径此问题碰到过很多次,每次都要查!!!public String getServerPath() {  javax.Faces.context.FacesContext fc = FacesContext.getCurrentInstance();  HttpServletRequest request = (HttpServletRequest) fc
        .getExternalContext().getRequest();
      String filePath = request.getSession().getServletContext().getRealPath(
        "/");// 通过上下文取得应用程序在服务器上的真实路径
      File fi = new File(filePath);
      String fil = fi.getParent();  return fil;-----E:\platform\apache-tomcat-5.5.25-new\webapps
     }  
      

  3.   

    刚刚搞定一个文件上传功能,就是用Servlet,用common-fileupload.jar
      

  4.   


    谢谢,已经解决了,我是用的这句话,少些很多代码的,哈哈      FileUtils.copyInputStreamToFile(file[0].getInputStream(), new File(dirPath+"/"+fileNameStr));