我使用素数,我想从数据库下载不同格式(pdf,jpg,png,)的文件但我没有成功意识到这一点我看过很多这样的话题,但他们的方法对我不起作用这是 HTML :
<ui:repeat value="#{histCommController.selectedCommande.listFichiers}" var="jjjjj">
<tr>
<td>
<h:outputLabel style="font-weight: bold;" value="#{jjjjj.nom}" />
</td>
<td>
<h:outputLabel style="font-weight: bold;" value="#{jjjjj.taille}" />
</td>
<td>
<p:commandButton id="downloadLink" value="Download" ajax="false"
icon="ui-icon-arrowthichk-s">
<p:fileDownload value="#{jjjjj.convertFichieru}" />
</p:commandButton>
</td>
</tr>
</ui:repeat>
这是Java:
public StreamedContent convertFichier(byte[] bytes) {
InputStream is = new ByteArrayInputStream(bytes);
System.out.println("size file : "+bytes.length);
StreamedContent image = new DefaultStreamedContent(is);
System.out.println("dans le convertisseur : "+image.getContentType());
return image;
}
always, image.getContentType() 返回 null和 bytes.length 不为空
你有什么想法吗
谢谢
我只知道问题所在,我将下载链接放在对话框中,因为当我在对话框外进行测试时,它可以工作这是它工作的测试:
<h:form>
<p:commandLink id="downloadLink" value="Download" ajax="false">
<p:fileDownload value="#{histCommController.test}" />
</p:commandLink>
</h:form>
这是对话框中的测试:
<p:dialog header="Car Detail" widgetVar="carDialog" resizable="false" id="carDlg"
showEffect="fade" hideEffect="explode" modal="true">
<table id="gradient-style" >
<tr style="border: hidden;">
<th>
<h:outputLabel value="Model:" />
</th>
<td>
<h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.id}" />
</td>
</tr>
<tr style="border: hidden;">
<th>
<h:outputLabel value="Year:" />
</th>
<td>
<h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.etat}" />
</td>
</tr>
<tr style="border: hidden;">
<th>
<h:outputLabel value="Manufacturer:" />
</th>
<td>
<h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.dateEnvoi}" />
</td>
</tr>
<tr style="border: hidden;">
<th>
<h:outputLabel value="Color:" />
</th>
<td>
<h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.dateLivraisonRecommande}" />
</td>
</tr>
</table>
<table id="gradient-style" >
<th>Nom Fichier</th><th>Taille</th><th>Télécharger</th>
<ui:repeat value="#{histCommController.selectedCommande.listFichiers}" var="jjjjj">
<tr>
<td>
<h:outputLabel style="font-weight: bold;" value="#{jjjjj.nom}" />
</td>
<td>
<h:outputLabel style="font-weight: bold;" value="#{jjjjj.taille}" />
</td>
<td>
<p:commandLink id="downloadLink" value="Download" ajax="false">
<p:fileDownload value="#{histCommController.test}" />
</p:commandLink>
</td>
</tr>
</ui:repeat>
</table>
</p:dialog>
后者不起作用
您知道如何在对话框中使用下载链接
吗提前谢谢你
您应该自己设置内容类型。看看 DefaultStreamedContent 的构造函数。所以而不是
StreamedContent image = new DefaultStreamedContent(is);
你应该写
StreamedContent image = new DefaultStreamedContent(is, "image/jpeg", "fileName.jpg");
请确保使用正确的内容类型和文件名扩展名。