Java代码:
@ManagedBean(name="fileDownloadController")
@ViewScoped
public class MBfileDownloadController {
private StreamedContent file;
private List<File> listfiles=new ArrayList<File>();
private String fileName;
private String ruteGluster;
public MBfileDownloadController() throws NamingException {
Context context = new InitialContext();
ruteGluster = (String) context.lookup("java:/util/glusterFS");
if (ruteGluster.charAt(ruteGluster.length() - 1) != '/') {
ruteGluster = ruteGluster + "/";
}
File filestream=new File(ruteGluster + listfiles);
try {
file = new DefaultStreamedContent(new FileInputStream(filestream), new MimetypesFileTypeMap().getContentType(filestream));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<File> getListfiles() {
File folder = new File(ruteGluster + "reportesEstaticos/spools");
File[] listOfFiles = folder.listFiles();
listfiles=Arrays.asList(listOfFiles);
for(int i=0;i<listfiles.size();i++){
listfiles.get(i);
}
System.out.println("The List of file are" + listfiles);
return listfiles;
}
public void setListfiles(List<File> listfiles) {
this.listfiles = listfiles;
}
public String getFileName() {
getListfiles();
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public StreamedContent getFile() {
return this.file;
}
}
xhtml:
<h:dataTable value="#{fileDownloadController.listfiles}" var="x"
bgcolor="#F1F1F1" border="10" cellpadding="5"
cellspacing="3" first="0" rows="15" width="50%"
summary="This is a JSF code to create dataTable.">
<h:column>
<f:facet name="header">
<h:outputText value="Spools"></h:outputText>
</f:facet>
<h:commandLink value="#{x.name}">
<i class="fa fa-download" aria-hidden="true" style="width: 20px"></i>
<p:fileDownload value="#{fileDownloadController.file}" />
</h:commandLink>
</h:column>
</h:dataTable>
当我执行代码时,出现以下异常,并且不下载文件,但表显示了文件并且表很好。