我正在尝试实现所有功能的下载,我的要求是文件应以.zip压缩,此处为UI
<p:toolbar>
<f:facet name="right">
<p:commandButton value="Download all photos" ajax="false" actionListener="#{ManageActivities.getAllParticipantPhoto}" onclick="PrimeFaces.monitorDownload(start, stop);" icon="ui-icon-image">
<p:fileDownload value="#{ManageActivities.file}" />
</p:commandButton>
</f:facet></p:toolbar>
Run Code Online (Sandbox Code Playgroud)
这是托管bean代码
private StreamedContent file;
public void getAllParticipantPhoto() {
ByteArrayInputStream bis = new ByteArrayInputStream(zipBytes());
InputStream stream = bis;
file = new DefaultStreamedContent(stream, "zip", "photos.zip");
Run Code Online (Sandbox Code Playgroud)
}
private byte[] zipBytes () {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
try{
for(Participants p : partcpnts){
if(p.getPhoto() != null){
ZipEntry entry = new ZipEntry(p.getFirstName()+".jpg");
zos.putNextEntry(entry);
zos.write(p.getPhoto());
}
}
}catch(Exception e){
e.printStackTrace();
}
return baos.toByteArray();
}
Run Code Online (Sandbox Code Playgroud)
我可以成功将文件下载为ZIP,但是无法解压缩,Windows提示“无法打开文件作为存档”错误
改变中
file = new DefaultStreamedContent(stream, "zip", "photos.zip");
Run Code Online (Sandbox Code Playgroud)
至
file = new DefaultStreamedContent(stream, "application/zip", "photos.zip", Charsets.UTF_8.name());
Run Code Online (Sandbox Code Playgroud)
固定它
| 归档时间: |
|
| 查看次数: |
3050 次 |
| 最近记录: |