有谁用过richfaces的fileUpload控件,怎么不能保存上传文件呀! 文件大小为0?
代码如下
<rich:fileUpload 
fileUploadListener="#{systemMaintainBean.uploadListener}"
maxFilesQuantity="100"
immediateUpload="true"
autoclear="false"
addControlLabel="图片上传"
clearAllControlLabel="清除"
clearControlLabel=""
cancelEntryControlLabel="停止"
listHeight="0"
listWidth="515"
style="height:100;"
acceptedTypes="jpg, gif, png, bmp">
<a4j:support event="onuploadcomplete" 
reRender="functions,errorArticleEdit"
oncomplete="refresh();"/>
</rich:fileUpload> public void uploadListener(UploadEvent event){
try{
//为了避免黑客上传木马,上传后的文件将随即改名
    UploadItem item = event.getUploadItem();
Date tmp=new Date();
MD5 md5=new MD5();
String fileName=item.getFileName();//文件名
String filepath=FacesUtils.getServletContext().getRealPath("/")+"UploadFile\\";
//创建File对象,将上传的文件保存到指定文件夹下
File file = new File(filepath, fileName);
//将该数据流写入到指定文件中
FileOutputStream out = new FileOutputStream(file);
out.write(item.getData());
FacesUtils.addErrorMessage("处理上传成功!"+filepath+fileName);
}
catch(Exception e){
FacesUtils.addErrorMessage(e.toString());
}
}

解决方案 »

  1.   

    有错误吗,加上out.close试试看。可能是缓冲区的原因。
      

  2.   

    我用此组件时,uploadListener(UploadEvent event) 方法不能激活,请高手指点
      

  3.   

    请哪位解决问题的高手把web.xml贴出来;还有所用的包名
      

  4.   

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     <description>Richfaces Components demo</description>
     <display-name>richfaces-demo</display-name>
     <context-param>
      <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
      <param-value>.xhtml</param-value>
     </context-param>
     <context-param>
      <param-name>facelets.REFRESH_PERIOD</param-name>
      <param-value>2</param-value>
     </context-param>
     <context-param>
      <param-name>facelets.DEVELOPMENT</param-name>
      <param-value>true</param-value>
     </context-param>
     <context-param>
      <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
      <param-value>server</param-value>
     </context-param>
     <context-param>
      <param-name>com.sun.faces.validateXml</param-name>
      <param-value>true</param-value>
     </context-param>
     <context-param>
      <param-name>com.sun.faces.verifyObjects</param-name>
      <param-value>false</param-value>
     </context-param>
     <context-param>
      <param-name>org.ajax4jsf.SKIN</param-name>
      <param-value>#{skinBean.skin}</param-value>
     </context-param>
     <context-param>
      <param-name>org.ajax4jsf.VIEW_HANDLERS</param-name>
      <param-value>com.sun.facelets.FaceletViewHandler</param-value>
     </context-param>
     <context-param>
      <param-name>org.ajax4jsf.COMPRESS_SCRIPT</param-name>
      <param-value>true</param-value>
     </context-param>
     <context-param>
      <param-name>org.ajax4jsf.COMPRESS_STYLE</param-name>
      <param-value>true</param-value>
     </context-param> 
     <context-param>
      <param-name>org.ajax4jsf.xmlparser.ORDER</param-name>
      <param-value>NONE, NEKO, TIDY</param-value>
     </context-param>
     <context-param>
      <param-name>org.richfaces.CONTROL_SKINNING</param-name>
      <param-value>enable</param-value>
     </context-param>
     
      <context-param>
    <param-name>org.richfaces.LoadStyleStrategy</param-name>
    <param-value>ALL</param-value>
    </context-param>
    <context-param>
    <param-name>org.richfaces.LoadScriptStrategy</param-name>
    <param-value>ALL</param-value>
    </context-param> <filter>
    <display-name>Ajax4jsf Filter</display-name>
    <filter-name>ajax4jsf</filter-name>
    <filter-class>org.ajax4jsf.Filter</filter-class>
    <init-param>
    <param-name>createTempFiles</param-name>
    <param-value>false</param-value>
    </init-param>
    <init-param> 
    <param-name>maxRequestSize</param-name>
    <param-value>100000</param-value>
    </init-param>
    </filter>
    <filter-mapping>
      <filter-name>ajax4jsf</filter-name>
      <servlet-name>Faces Servlet</servlet-name>
      <dispatcher>FORWARD</dispatcher>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>INCLUDE</dispatcher>
      <dispatcher>ERROR</dispatcher>
     </filter-mapping>
     <servlet>
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.jsf</url-pattern>
     </servlet-mapping>
     <login-config>
      <auth-method>BASIC</auth-method>
     </login-config>
    </web-app>