Jsf Apachce tomcat/7.0.30下载文件时Http状态404错误
我正在尝试使用JSF下载该文件,在运行程序时,将表属性作为路径,它可以正常工作一次,然后出现HTTP状态404。。。我应该如何下载该文件 XHTML代码Jsf Apachce tomcat/7.0.30下载文件时Http状态404错误,jsf,tomcat,download,Jsf,Tomcat,Download,我正在尝试使用JSF下载该文件,在运行程序时,将表属性作为路径,它可以正常工作一次,然后出现HTTP状态404。。。我应该如何下载该文件 XHTML代码 <p:dataTable id="idAttachmentTable" var="attach" value="#{documentMB.attachList1}" selectionMode="single" selection="#{documentMB.s
<p:dataTable id="idAttachmentTable" var="attach"
value="#{documentMB.attachList1}"
selectionMode="single"
selection="#{documentMB.selectedAttachment}"
rowKey="#{attach.attachmentId}">
<f:facet name="header">
Attachments
</f:facet>
<p:column headerText="#{bundle.subject}">
<h:outputText value="#{attach.attachName}" />
</p:column>
<p:column>
<h:commandLink id="getDownload" value="#{attach.attachName}" action="#{documentMB.downLoad}">
<f:setPropertyActionListener target="#{documentMB.selectedAttachment}" value="#{attach}" />
</h:commandLink>
</p:column>
</p:dataTable>
您是否确认每次给定的
迭代,
内部
通过列表documentMB.attachList1
将正确的值设置为正确的属性可能从数据库中检索?是的,每次我通过输出语句检查时它都选择正确的值,每次它都显示正确的值它只是下载pdf文件我应该做什么下载任何格式的文件???java.io.file文件=新的java.io.file(文件路径);对于上面的语句,它可以正常工作,但是每当我尝试通过表属性提供路径来下载任何图像时,就会出现HTTP状态错误404,现在我可以正常工作了。。。。
private static final int DEFAULT_BUFFER_SIZE = 10240;
private String filePath = "C:\\temp\\123.PNG";
public void downLoad() throws IOException {
System.out.println("in download");
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
System.out.println("after Faces");
System.out.println(filePath);
System.out.println(selectedAttachment.getAttachmentName());
java.io.File file = new java.io.File(selectedAttachment.getAttachmentName());
System.out.println(selectedAttachment.getAttachmentName());
if (!file.exists()) {
System.out.println(selectedAttachment.getAttachmentName());
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("application/octet-stream");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
System.out.println(file.getName());
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
input.close();
output.close();
}
context.responseComplete();
}