這是我xhtml-code
下載文件
<p:tree value="#{documentsController.root}"
var="node" selectionMode="single"
dynamic="true">
<p:treeNode
expandedIcon="ui-icon-folder-open"
collapsedIcon="ui-icon-folder-collapsed">
<h:outputText value="#{node}" />
</p:treeNode>
<p:treeNode type="file" icon="ui-icon-document">
<h:outputText value="#{node}"/>
</p:treeNode>
<p:ajax event="select" listener="#{documentsController.onNodeSelect}"/>
</p:tree>
,這是支持bean
@ManagedBean
@ViewScoped
public class DocumentsController implements Serializable {
TreeNode root;
//TreeNode[] selectedNodes;
@PostConstruct
public void init() {
root = new DefaultTreeNode("SRC", null);
TreeNode node0 = new DefaultTreeNode("A", root);
TreeNode node1 = new DefaultTreeNode("B", root);
TreeNode node2 = new DefaultTreeNode("C", root);
TreeNode node3 = new DefaultTreeNode("file", "D", node0);
TreeNode node4 = new DefaultTreeNode("file", "E", node0);
TreeNode node5 = new DefaultTreeNode("file", "F", node0);
String p = "C:\\Users\\federico.martinez\\Desktop\\a.wmv";
TreeNode node6 = new DefaultTreeNode("file", new File(p), node1);
TreeNode node7 = new DefaultTreeNode("file", "h", node1);
TreeNode node8 = new DefaultTreeNode("file", "i", node1);
}
public TreeNode getRoot() {
return root;
}
/*
public void setSelectedNodes(TreeNode[] selectedNodes){
this.selectedNodes = selectedNodes;
}
public TreeNode[] getSelectedNodes(){
return selectedNodes;
}*/
public void onNodeSelect(NodeSelectEvent event) {
if (event.getTreeNode().getType().equals("file")) {
File file = new File(event.getTreeNode().getData().toString());
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.setResponseHeader("Content-Type", externalContext.getMimeType(file.getName()));
externalContext.setResponseHeader("Content-Length", String.valueOf(file.length()));
externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = externalContext.getResponseOutputStream();
IOUtils.copy(input, output);
} catch (FileNotFoundException ex) {
System.out.println("FileNotFound: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("IO: " + ex.getMessage());
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
}
facesContext.responseComplete();
}
}
}
但它不是下載文件,我用戰斧樹這種下載方法和它的工作,現在我想與PrimeFaces,但這不會下載該文件,並沒有錯誤!
任何意見可能是錯誤的?
在此先感謝!
UPDATE
我有這樣的修改,但不能讓它下載文件。
<p:tree value="#{documentsController.root}"
var="node" selectionMode="single"
dynamic="true">
<p:treeNode
expandedIcon="ui-icon-folder-open"
collapsedIcon="ui-icon-folder-collapsed">
<h:outputText value="#{node}" />
</p:treeNode>
<p:treeNode type="file" icon="ui-icon-document">
<p:commandButton value="#{node}" ajax="false">
<p:fileDownload value="#{documentsController.download(node)}" />
</p:commandButton>
</p:treeNode>
</p:tree>
和backbean是
public void download(String path) {
File f = new File(path);
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
InputStream is = ((ServletContext)externalContext.getContext()).getResourceAsStream(path);
file = new DefaultStreamedContent(is, externalContext.getMimeType(f.getName()), f.getName());
}
public StreamedContent getFile(){
return file;
}
我現在沒有時間去測試它 - 但有可能是因爲其在「吃」您的請求下載的primefaces API中的AJAX調用。你應該看看,而不是使用actionlistener顯示下載文件的
和'ajax =「false」'。這將創建一個鏈接(它可以提前動態更改),該鏈接將持續而不是觸發更新。我在桌子上遇到了類似的問題,並且很快就將其解決了。祝你好運。 – 2011-12-22 01:37:53非常感謝!
替換了ajax標籤?或者treeNode標籤(顯示文檔的標籤)? – BRabbit27 2011-12-22 05:21:10