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>
Когда я выполняю свой код, я получаю следующее исключение и не загружаю файл, но в таблице отображаются файлы, и таблица в порядке.
Включая трассировку стека.
Привет, вот трассировка: [ОШИБКА] [21-02-19 10:00:26] org.jboss.stdio.AbstractLoggingWriter:write(71) -> java.io.FileNotFoundException: [ОШИБКА] [21-02-19 10:00:26] org.jboss.stdio.AbstractLoggingWriter:write(71) -> в java.io.FileInputStream.open0(собственный метод)