使用richfaces上传文件

时间:2013-05-27 10:08:40

标签: jsf file-upload richfaces

在RichFaces中,为了上传我使用rich:fileUpload的文件。使用rich:fileUpload添加,上传和全部清除按钮会在richfaces livedemo示例中自动生成,但对我来说,添加,上传和全部清除在我的浏览器中显示为文本,我应该在哪里更改,以便添加,上传和清除所有可以可点击。我是否必须更改web.xml / pom.xml文件中的任何设置。

我正在使用RichFaces 4和JSF 2。

这是我的web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Sample RichFaces 4 Application</display-name>
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>org.richfaces.skin</param-name>
    <param-value>#{skinBean.skin}</param-value>
</context-param>
<context-param>
    <param-name>org.richfaces.fileUpload.maxRequestSize</param-name>
    <param-value>1000000000</param-value>
</context-param>
<context-param>
    <param-name>org.richfaces.fileUpload.createTempFiles</param-name>
    <param-value>true</param-value>
</context-param>
<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<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>
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<mime-mapping>
    <extension>ecss</extension>
    <mime-type>text/css</mime-type>
</mime-mapping>
<welcome-file-list>
    <welcome-file>index.jsf</welcome-file> 
</welcome-file-list>
<login-config>
    <auth-method>BASIC</auth-method>
</login-config>

嗨,我现在通过简单地提供h:head和h:body标签正确地获取这些添加和更新按钮但我得到此异常javax.faces.event.AbortProcessingException并且文件未被上传,如示例中所示在“FileUpload example”链接中提供。任何人都可以帮助我解决这个问题

这是我的xhtml页面

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich">



<ui:composition>
 <h:head>
 <h:outputStylesheet>
    .top {
    vertical-align: top;
    }
.info {
    height: 202px;
    overflow: auto;
    }
</h:outputStylesheet>
</h:head>
<h:body>
<h:form>
    <h:panelGrid columns="2" columnClasses="top,top">
        <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}" id="upload" acceptedTypes="jpg, gif, png, bmp"
            ontyperejected="alert('Only JPG, GIF, PNG and BMP files are accepted');" maxFilesQuantity="5">
            <a4j:ajax event="uploadcomplete" execute="@none" render="info" />
        </rich:fileUpload>
        <h:panelGrid id="info" layout="block">
            <rich:panel bodyClass="info">
                <f:facet name="header">
                    <h:outputText value="Uploaded Files Info" />
                </f:facet>
                <h:outputText value="No files currently uploaded" rendered="#{fileUploadBean.size==0}" />
                <rich:dataGrid columns="1" value="#{fileUploadBean.files}" var="file" rowKeyVar="row">
                    <rich:panel bodyClass="rich-laguna-panel-no-header">
                        <h:panelGrid columns="2">
                            <a4j:mediaOutput element="img" mimeType="image/jpeg" createContent="#{fileUploadBean.paint}"
                                value="#{row}" style="width:100px; height:100px;" cacheable="false">
                                <f:param value="#{fileUploadBean.timeStamp}" name="time" />
                            </a4j:mediaOutput>
                            <h:panelGrid columns="2">
                                <h:outputText value="File Name:" />
                                <h:outputText value="#{file.name}" />
                                <h:outputText value="File Length(bytes):" />
                                <h:outputText value="#{file.length}" />
                            </h:panelGrid>
                        </h:panelGrid>
                    </rich:panel>
                </rich:dataGrid>
            </rich:panel>
            <br/>
            <a4j:commandButton action="#{fileUploadBean.clearUploadData}" render="info, upload" value="Clear Uploaded Data"
                rendered="#{fileUploadBean.size>0}" />
        </h:panelGrid>
    </h:panelGrid>
</h:form>
</h:body>
</ui:composition>
</html> 

这是我的FileUploadBean类

package com.acc.upload;

import java.io.IOException;

import java.io.OutputStream;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.acc.fileupload.utils.FileUploadEvent;
import com.acc.fileupload.utils.UploadedFile;


@ManagedBean(name = "fileUploadBean")
@SessionScoped
public class FileUploadBean{

private ArrayList<File> files = new ArrayList<File>();
private int uploadsAvailable = 5;
private boolean autoUpload = false;
private boolean useFlash = true;


public int getSize() {
    if (getFiles().size()>0){
        return getFiles().size();
    }else 
    {
        return 0;
    }
}

public FileUploadBean() {
}

public void paint(OutputStream stream, Object object) throws IOException {
    stream.write(getFiles().get((Integer)object).getData());
}
public void listener(FileUploadEvent event) throws Exception{
    UploadedFile item = event.getUploadedFile();
    File file = new File();
    file.setLength(item.getData().length);
    file.setName(item.getName());
    file.setData(item.getData());
    files.add(file);
    uploadsAvailable--;
}  

public String clearUploadData() {
    files.clear();
    setUploadsAvailable(5);
    return null;
}

public long getTimeStamp(){
    return System.currentTimeMillis();
}

public ArrayList<File> getFiles() {
    return files;
}

public void setFiles(ArrayList<File> files) { 
    this.files = files;
}

public int getUploadsAvailable() {
    return uploadsAvailable;
}

public void setUploadsAvailable(int uploadsAvailable) {
    this.uploadsAvailable = uploadsAvailable;
}

public boolean isAutoUpload() {
    return autoUpload;
}

public void setAutoUpload(boolean autoUpload) {
    this.autoUpload = autoUpload;
}

public boolean isUseFlash() {
    return useFlash;
}

public void setUseFlash(boolean useFlash) {
    this.useFlash = useFlash;
}

}

档案类

package com.acc.upload;

public class File {

private String Name;
private String mime;
private long length;
private byte[] data;
public byte[] getData() {
    return data;
}
public void setData(byte[] data) {
    this.data = data;
}
public String getName() {
    return Name;
}
public void setName(String name) {
    Name = name;
    int extDot = name.lastIndexOf('.');
    if(extDot > 0){
        String extension = name.substring(extDot +1);
        if("png".equals(extension)){
            mime="image/png";
        }else {
            mime = "img/unknown";
        }
    }
}
public long getLength() {
    return length;
}
public void setLength(long length) {
    this.length = length;
}

public String getMime(){
    return mime;
}
}

非常感谢。现在我的错误消失了。  我在web.xml中将createTemp文件设置为true但我无法在本地系统的temp文件夹中找到上传的文件。还可以通过更改任何内容将上传的文件存储到本地系统中的所需位置。 web.xml文件中的设置?  请帮助解决这两个问题。

2 个答案:

答案 0 :(得分:0)

关于按钮图标问题,我很确定你不管自己的RichFaces的皮肤,所以你可以从web.xml中删除它:

<context-param>
    <param-name>org.richfaces.skin</param-name>
    <param-value>#{skinBean.skin}</param-value>
</context-param>

RichFaces现在将使用默认外观选项。

根据你的评论,你缺少h:head是必须的,以便为RichFaces包含必要的资源CSS和JavaScript。

此外,您的上一个问题与您的导入有关:

import com.acc.fileupload.utils.FileUploadEvent;
import com.acc.fileupload.utils.UploadedFile;

应替换为:

import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;

其他信息:

您所依据的示例来自 RichFaces 3.3.3 (这在页脚中显示)。使用 RichFaces 4.3.2 this示例仔细检查您的观看代码。

答案 1 :(得分:0)

我也使用richface 4.0.0和spring 4来解决这个问题。当上传文件时,我总是收到此错误消息“请求prolog无法读取”。

我通过以下方式解决了这个问题:将multipart resolever设置为  而不是CommonsMultipartResolver for DispatcherServlet。因为Spring的CommonsMultipartResolver与richface 4.0.0不一致

希望这有帮助!