Icefaces树+ outputResource

时间:2012-01-23 20:42:53

标签: jsf tree download icefaces

我实现了一个带有outputResource的树作为"内容" (见Downloading file from IceFaces tree)。

但是,当我尝试下载文件时(我只有txt或csv文件),我收到了HTTP 500状态错误。

树结构类似于

  Root
  |-infoFiles
  | |-A.csv
  | |-B.csv
  |-validateFiles
  | |-C.txt
  | |-D.txt

点击资源后异常是

java.io.FileNotFoundException: C:\SRC\dataFiles\998\validateFiles (Access denied)
    java.io.FileInputStream.open(Native Method)
    java.io.FileInputStream.<init>(FileInputStream.java:138)
    java.io.FileInputStream.<init>(FileInputStream.java:97)
    mx.gob.sagarpa.utilidades.FileResource.open(FileResource.java:39)
    com.icesoft.faces.component.outputresource.RegisteredResource.open(OutputResource.java:474)
    com.icesoft.faces.context.ResourceRegistryLocator$DynamicResourceDispatcherAdapter$DynamicResourceAdapter.open(ResourceRegistryLocator.java:117)
    org.icefaces.impl.push.DynamicResourceDispatcher$ResourceServer.respond(DynamicResourceDispatcher.java:224)
    org.icefaces.impl.push.DynamicResourceDispatcher$ResourceServer.handleResourceRequest(DynamicResourceDispatcher.java:201)
    org.icefaces.impl.push.DynamicResourceDispatcher$Mapping.handleResourceRequest(DynamicResourceDispatcher.java:370)
    org.icefaces.impl.push.DynamicResourceDispatcher.handleResourceRequest(DynamicResourceDispatcher.java:89)
    org.icefaces.application.ResourceRegistry.handleResourceRequest(ResourceRegistry.java:75)
    org.icefaces.impl.application.WindowScopeManager.handleResourceRequest(WindowScopeManager.java:165)
    javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:125)
    javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:125)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:591)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

有时只会使用 csv 文件,有时甚至会使用 txt

Tree.xhtml

                <ice:tree id="tree"
                          value="#{treeBean.model}"
                          var="item"
                          hideRootNode="false"
                          hideNavigation="false"
                          >
                    <ice:treeNode>
                        <f:facet name="icon">
                            <ice:panelGroup style="display: inline">
                                <h:graphicImage value="#{item.userObject.icon}" />
                            </ice:panelGroup>
                        </f:facet>
                        <f:facet name="content">
                            <ice:panelGroup style="display: inline-block">
                                <ice:outputResource resource="#{item.userObject.resource}"
                                                    fileName="#{item.userObject.resource.filename}"
                                                    mimeType="#{item.userObject.resource.mimeType}"
                                                    />
                            </ice:panelGroup>
                        </f:facet>
                    </ice:treeNode>
                </ice:tree>

TreeBean.java

@ManagedBean
@ViewScoped
public class TreeBean implements Serializable {

private DefaultTreeModel model;
public final String openFolderImg = "./img/tree_folder_open.gif";
public final String closeFolderImg = "./img/tree_folder_close.gif";
public final String fileImg = "./img/tree_document.gif";
@ManagedProperty("#{userBean}")
private UserBean userBean;

@PostConstruct
public void init() {
    // create root node with its children expanded
    DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
    FileResourceUserObject rootObject = new FileResourceUserObject(rootTreeNode);
    rootObject.setText("Sistema de Rendición de Cuentas");
    rootObject.setExpanded(true);
    rootObject.setResource(new FileResource("Sistema de Rendición de Cuentas", null));
    rootObject.setBranchContractedIcon(openFolderImg);
    rootObject.setBranchExpandedIcon(closeFolderImg);
    rootTreeNode.setUserObject(rootObject);

    // model is accessed by the ice:tree component
    model = new DefaultTreeModel(rootTreeNode);
    File f = new File("./998/");
    createTree(f, rootTreeNode);
}

public DefaultTreeModel getModel() {
    return model;
}

public UserBean getUserBean() {
    return userBean;
}

public void setUserBean(UserBean userBean) {
    this.userBean = userBean;
}

private void createTree(File fileRoot, DefaultMutableTreeNode treeRoot) {
    File[] files = fileRoot.listFiles();
    DefaultMutableTreeNode branchNode;
    for (File f : files) {
        if (f.isDirectory()) {
            branchNode = new DefaultMutableTreeNode();
            FileResourceUserObject branchObject = new FileResourceUserObject(branchNode);
            branchObject.setExpanded(false);
            branchObject.setText(f.getName());
            branchObject.setResource(new FileResource(f.getName(), f.getAbsolutePath()));
            branchObject.setBranchContractedIcon(openFolderImg);
            branchObject.setBranchExpandedIcon(closeFolderImg);
            branchNode.setUserObject(branchObject);
            treeRoot.add(branchNode);
            createTree(f, branchNode);
        }
        if (f.isFile()) {
            branchNode = new DefaultMutableTreeNode();
            FileResourceUserObject branchObject = new FileResourceUserObject(branchNode);
            branchObject.setText(f.getName());
            branchObject.setResource(new FileResource(f.getName(), f.getAbsolutePath()));
            branchObject.setLeaf(true);
            branchObject.setLeafIcon(fileImg);
            branchNode.setUserObject(branchObject);
            treeRoot.add(branchNode);
        }
    }

    return;
}

}

FileResourceUserObject.java

public class FileResourceUserObject extends IceUserObject{

    private FileResource resource;

    public FileResourceUserObject(DefaultMutableTreeNode wrapper) {
        super(wrapper);
    }

    public FileResource getResource() {
        return resource;
    }

    public void setResource(FileResource resource) {
        this.resource = resource;
    }

}

FileResource.java

public class FileResource implements Resource{

    private String filename;
    private String fileAbsolutePath;
    private String mimeType;
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();

    public FileResource(String filename, String fileAbsolutePath) {
        this.filename = filename;
        this.fileAbsolutePath = fileAbsolutePath;
        this.mimeType = ec.getMimeType(filename);
    }

    @Override
    public String calculateDigest() {
        return filename;
    }

    @Override
    public InputStream open() throws IOException {
        return new FileInputStream(fileAbsolutePath);
    }

    @Override
    public Date lastModified() {
        return new Date();
    }

    @Override
    public void withOptions(Options optns) throws IOException {

    }

    public String getFileAbsolutePath() {
        return fileAbsolutePath;
    }

    public void setFileAbsolutePath(String fileAbsolutePath) {
        this.fileAbsolutePath = fileAbsolutePath;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getMimeType() {
        return mimeType;
    }

}

更新

我注意到当我得到 500状态错误时,FileNotFoundException总是指向相同的路径 java.io.FileNotFoundException:C:\ SRC \ dataFiles \ 998 \ validateFiles (访问被拒绝) 我开始认为我的所有FileResources都指向同一条路径...为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

有一个关于IceFaces页面上的错误的报告,它说使用多个outputResource标签有一个奇怪的行为。请参阅此链接http://jira.icefaces.org/browse/ICE-3667 我认为这是因为我有树和许多outputResource标签。

此外,我还读到使用outputResource标记它在渲染时创建一个Object(或者类似于我对JSF很新的东西以及所有相关的东西)并且它是内存消耗的并且它更好使用servlet执行下载。见http://www.dantoomeysoftware.com/pencils-down/2009/09/08/dont-use-icefaces-resource-for-download-use-a-download-servlet/

所以最好实现一个servlet,你可以在http://balusc.blogspot.com/2007/07/fileservlet.html中找到有用的信息(如果你对所有这些东西都不熟悉的话)Custom download servlet