如何为任何内容类型(pdf/zip/docx/jpeg 等)制作可下载的命令链接
How to make a downloadable commandLink for any content type (pdf/zip/docx/jpeg etc)
我想创建一个可下载的 link.I 使用 primefaces 5.0 和 jsf 2。2.I 有一些文件(pdf、zip/rar、docx、jpeg 等)我想分享使用 commandLink 在数据 table 上创建此文件。仅可下载 google 中的文件(具有特定内容类型)(例如:download only a pdf)。但我如何才能下载任何内容类型的文件?
我的所有文件都在
c://uploads/files/dir1/foo.zip
---------""------/dir2/boo.pdf
---------""------/dir3/doo.jpeg etc.
您可以在下面看到依赖于特定内容类型的类似代码。
private StreamedContent file;
public FileDownloadView() {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/resources/demo/images/optimus.jpg");
file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
}
public StreamedContent getFile() {
return file;
}
你可以在这部分看到:
DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
只能下载"image/jpg"。我想独立于此section.Is强制为每个内容类型单独编码?
无论数据 table.Thanks 中的内容类型如何,我都想提前共享此文件。
像这样创建新 bean:
public class MyFile {
private String name;
private String type;
private String encoding;
private String path;
// ... Setters and getters ...
public Attachment () {
this.encoding = "UTF-8";
}
}
在您的托管 bean 中
private DefaultStreamedContent streamToDownload;
private MyFile myFile;
// @ here setter and getter too .
public void prepareForDownlaod() {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(myFile.getPath());
streamToDownload= new DefaultStreamedContent(stream, myFile.getType(), myFile.getName(), myFile.getEncoding());
}
在 xhtml 文件中:
<p:commandLink ajax="false" value="Download" action="#{managedBean.prepareForDownlaod()}">
<p:fileDownload value="#{managedBean.streamToDownload}" />
</p:commandLink>
试试这个,希望对你有用。
编辑
我对存储数据的解决方案,例如来自数据库中存储的数据,或者您可以使用 externalContext.getMimeType
设置文件类型,您可以参考 BalusC 对最后一个案例的回答。
我想创建一个可下载的 link.I 使用 primefaces 5.0 和 jsf 2。2.I 有一些文件(pdf、zip/rar、docx、jpeg 等)我想分享使用 commandLink 在数据 table 上创建此文件。仅可下载 google 中的文件(具有特定内容类型)(例如:download only a pdf)。但我如何才能下载任何内容类型的文件?
我的所有文件都在
c://uploads/files/dir1/foo.zip
---------""------/dir2/boo.pdf
---------""------/dir3/doo.jpeg etc.
您可以在下面看到依赖于特定内容类型的类似代码。
private StreamedContent file;
public FileDownloadView() {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/resources/demo/images/optimus.jpg");
file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
}
public StreamedContent getFile() {
return file;
}
你可以在这部分看到:
DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
只能下载"image/jpg"。我想独立于此section.Is强制为每个内容类型单独编码? 无论数据 table.Thanks 中的内容类型如何,我都想提前共享此文件。
像这样创建新 bean:
public class MyFile {
private String name;
private String type;
private String encoding;
private String path;
// ... Setters and getters ...
public Attachment () {
this.encoding = "UTF-8";
}
}
在您的托管 bean 中
private DefaultStreamedContent streamToDownload;
private MyFile myFile;
// @ here setter and getter too .
public void prepareForDownlaod() {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(myFile.getPath());
streamToDownload= new DefaultStreamedContent(stream, myFile.getType(), myFile.getName(), myFile.getEncoding());
}
在 xhtml 文件中:
<p:commandLink ajax="false" value="Download" action="#{managedBean.prepareForDownlaod()}">
<p:fileDownload value="#{managedBean.streamToDownload}" />
</p:commandLink>
试试这个,希望对你有用。
编辑
我对存储数据的解决方案,例如来自数据库中存储的数据,或者您可以使用 externalContext.getMimeType
设置文件类型,您可以参考 BalusC 对最后一个案例的回答。