我目前正在研究 JSF PrimeFaces,我想下载我的项目名称“CV”文件夹中存在的文件,但这里面临的问题是下面的代码
文件上传:
<h:form enctype="multipart/form-data">
<p:fileUpload
fileUploadListener="#{fileUploadController.handleFileUploadCv}"
mode="advanced" update="messages"
allowTypes="/(\.|\/)(doc|docx)$/" />
<p:growl id="messages" showDetail="true" />
</h:form>
文件下载:
<h:form id="form11">
<p:commandButton id="downloadLink" value="Download Cv" ajax="false"
onclick="PrimeFaces.monitorDownload(start, stop)"
icon="ui-icon-arrowthichk-s">
<p:fileDownload value="#{fileDownloadController.file}" />
</p:commandButton>
</h:form>
这是 Controller 类:
import java.io.InputStream;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
@ManagedBean
@SessionScoped
public class FileDownloadController {
private StreamedContent file;
public FileDownloadController() {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("D:/Final Year Project/displayjob-portlet/docroot/cv/Junaid.cv");
System.out.print("inside download111");
file = new DefaultStreamedContent(stream);
}
public StreamedContent getFile() {
return file;
}
}
最佳答案
您正在尝试从您的电脑而不是服务器获取文件。由于应用程序运行在服务器上,因此根目录是您的服务器域根目录。 尝试将文件放入服务器并更改其目录。 例如,您可以在服务器的根目录中创建一个名为 filesToDownload 的文件夹,并将文件放入其中。然后你应该像这样找到它:
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("filesToDownload/Junaid.cv");
我知道这个问题有点老了,但由于我面临同样的问题,我认为这个答案可能对其他人有帮助。
关于java - JSF Primefaces 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14941501/