问题描述
你好,我正在做文件上传并使用JSF和Primefaces下载相同的文件操作。我正在使用来自不同论坛和博客的技术(BelusC的博客和Primefaces Showcase)。
这是我的代码:
index.xhtml
< h:form>
< p:fileUpload showButtons =falselabel =附加Refrral
auto =truefileUploadListener =#{fileBean.uploadListener}/>
< / h:form>
< h:form>
< p:commandLink>
查看已上传文件
< p:fileDownload value =#{fileBean.refrralFile}/>
< / p:commandLink>
< / h:form>
FileBean.java
私人StreamedContent refrralFile;
public void uploadListener(FileUploadEvent evt)throws异常
{
UploadedFile fx = evt.getFile();
文件mainDir =新文件(C:/,fileStorage);
if(!mainDir.exists())
{
mainDir.mkdir();
}
文件subDir = new File(mainDir,AttachedRefrrals);
if(!subDir.exists())
{
subDir.mkdir();
}
String fileName = fx.getFileName();
文件f = new File(subDir,fileName);
FileOutputStream fos = new FileOutputStream(f);
IOUtils.copy(fx.getInputstream(),fos);
InputStream is =((ServletContext)FacesContext.getCurrentInstance()。getExternalContext()。getContext())。getResourceAsStream(f.getAbsolutePath());
refrralFile = new DefaultStreamedContent(is,new MimetypesFileTypeMap()。getContentType(f),fileName);
}
public StreamedContent getRefrralFile(){
return refrralFile;
}
使用上面的代码文件正在成功上传,但如果我点击文件下载链接ths throwing例外:
java.lang.IllegalStateException:getOutputStream()已被调用此响应
我使用了 FacesContext#responseComplete(),因为它已经建议了很多地方,现在下载链接不工作所有。
如果我的技术或代码错了,请更正我,如果你知道,建议任何更好的方式。
< p:commandLink>
默认情况下触发ajax请求。您不能通过ajax下载文件。负责处理ajax请求的JavaScript并不知道与检索的二进制文件有什么关系,这与预期的XML响应有很大的不同。 JavaScript出于明显的安全原因,没有任何设备可以触发与任意内容的另存为对话框。
因此,要解决您的具体问题,请使用
p:commandLink ajax =false>
或只是
code>< H:commandLink>
另请参见:
- - 这也明确显示
ajax =false
Hello I'm doing File upload and download same file operation using JSF and Primefaces.
I'm using techniques from different forums and blogs combined(BelusC 's blog and Primefaces Showcase).
Here is my code:
index.xhtml
<h:form>
<p:fileUpload showButtons="false" label="Attach Refrral"
auto="true" fileUploadListener="#{fileBean.uploadListener}"/>
</h:form>
<h:form >
<p:commandLink>
See Uploaded File
<p:fileDownload value="#{fileBean.refrralFile}"/>
</p:commandLink>
</h:form>
FileBean.java
private StreamedContent refrralFile;
public void uploadListener(FileUploadEvent evt)throws Exception
{
UploadedFile fx = evt.getFile();
File mainDir = new File("C:/","fileStorage");
if(!mainDir.exists())
{
mainDir.mkdir();
}
File subDir = new File(mainDir,"AttachedRefrrals");
if(!subDir.exists())
{
subDir.mkdir();
}
String fileName = fx.getFileName();
File f = new File(subDir,fileName);
FileOutputStream fos = new FileOutputStream(f);
IOUtils.copy(fx.getInputstream(), fos);
InputStream is = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(f.getAbsolutePath());
refrralFile = new DefaultStreamedContent(is, new MimetypesFileTypeMap().getContentType(f), fileName);
}
public StreamedContent getRefrralFile() {
return refrralFile;
}
Using above code File is uploading Successfully but if I click file download link ths throwing exception:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
I used FacesContext#responseComplete(), as its been suggested many places, now download link is not working at all.
Please correct me if I'm wrong in my technique or code and suggest any better way if you know.
The <p:commandLink>
fires by default an ajax request. You can't download files via ajax. JavaScript, who's responsible for processing the ajax request, has no clue what to do with retrieved binary file which is quite different from the expected XML response. JavaScript has for obvious security reasons no facilities to trigger a Save As dialogue with arbitrary content.
So, to fix your concrete problem, use
<p:commandLink ajax="false">
or just
<h:commandLink>
See also:
- How to provide a file download from a JSF backing bean?
- PrimeFaces
<p:fileDownload>
showcase page - this also explicitly showsajax="false"
这篇关于p:fileDownload bean方法被调用,但文件下载不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!