서버에서 pdf 파일을 다운로드하고 싶습니다. 나는 바람둥이를 사용하고 있습니다. struts2에서 애플리케이션 개발하기. 내 JSP 코드 다운로드 링크에서 서버에서 파일을 다운로드하지 않습니다.
은 다음과 같습니다 :<td>
<a href='<s:url action='downloadPdf'> </s:url>'>
Download PDF</a>
</td>
내 struts.xml은 다음과 같습니다
<action name="downloadPdf" class="com.stp.portal.view.SearchServicePortlet" method="downloadPdf">
</action>
Action 클래스는 다음과 같습니다
public void downloadPdf() throws Exception
{
HttpServletResponse response = null;
try
{
response.setContentType ("application/pdf");
File f = new File ("D:\\abc.pdf");
response.setHeader ("Content-Disposition", "attachment;filename=abc.pdf");
InputStream inputStream = new FileInputStream(f);
ServletOutputStream servletOutputStream = response.getOutputStream();
int bit = 256;
int i = 0;
try
{
while ((bit) >= 0)
{
bit = inputStream.read();
servletOutputStream.write(bit);
}
}
catch (Exception ioe)
{
ioe.printStackTrace(System.out);
}
servletOutputStream.flush();
inputStream.close();
}
catch(Exception e)
{
}
}
public String generateGraph() throws Exception
{
return "success";
}
}
내 문제입니다 다운로드 링크를 클릭하면 파일이 다운로드되지 않습니다. abc.pdf 파일이 로컬 디스크 D 안에 있습니다. 무엇이 잘못되었는지 알 수 없습니다. 누군가가 이것에 관해 나를 도울 수 있으면 정말로 감사 할 것이다.
미리 감사드립니다. 이런 식으로 코드를 변경
디버깅을 시도 했습니까? 파일이 디스크에서 읽혀지고 실제로 응답으로 되돌아 왔는지 확인할 수 있습니까? – mthmulders
게다가 성능을 높이기 위해 한번에 한 바이트 씩 읽거나 쓰는 대신에'byte []'를 사용하는 것이 흥미로울 것입니다. – mthmulders
finally 블록에서 입력 스트림을 닫아야합니다. 한 가지 이유 또는 다른 이유로 I/O 오류가 발생하여 close()에 도달하지 않은 경우 파일 설명자를 누설합니다. – fge