2013-07-18 6 views
0

서버에서 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 안에 있습니다. 무엇이 잘못되었는지 알 수 없습니다. 누군가가 이것에 관해 나를 도울 수 있으면 정말로 감사 할 것이다.

미리 감사드립니다. 이런 식으로 코드를 변경

+0

디버깅을 시도 했습니까? 파일이 디스크에서 읽혀지고 실제로 응답으로 되돌아 왔는지 확인할 수 있습니까? – mthmulders

+1

게다가 성능을 높이기 위해 한번에 한 바이트 씩 읽거나 쓰는 대신에'byte []'를 사용하는 것이 흥미로울 것입니다. – mthmulders

+0

finally 블록에서 입력 스트림을 닫아야합니다. 한 가지 이유 또는 다른 이유로 I/O 오류가 발생하여 close()에 도달하지 않은 경우 파일 설명자를 누설합니다. – fge

답변

0

시도 :이 코드에

response.setContentType("application/force-download"); 
File f = new File ("D:\\abc.pdf"); 
response.addHeader("Content-Disposition", "attachment; filename=\"abc.pdf\""); 
InputStream inputStream = new FileInputStream(f); 
BufferedOutputStream out = 
       new BufferedOutputStream(response.getOutputStream()); 
byte by[] = new byte[32768]; 
int index; 
if (inputStream != null) { 
    index = inputStream.read(by, 0, 32768); 
} else { 
    index = -1; 
} 
while (index != -1) { 
    out.write(by, 0, index); 
    index = inputStream.read(by, 0, 32768); 
} 
out.flush(); 
+0

답장을 보내 주셔서 감사합니다. 그러나 나를 위해 최선을 다하고 있습니다. response.setContentType ("application/force-download"); 이 콘솔에서 오류가 발생합니다. – user2594235

+0

'오류 발생'이란 무엇입니까? 예외입니까? 그 행은 아무런 문제도 일으키지 않아야합니다. – MaVRoSCy

+0

[해킹을 절대 사용하지 마시오. @] (http://stackoverflow.com/a/10616753/1654265) ...'Content-Disposition : attachment'는 파일을 확인하기에 충분하다. (항상 열려 있고 항상 다운로드 등) –

0

변경합니다. 입력 스트림의 처리를 참고 :이 올바르게 I/O 오류가 발생할 경우 닫혀 : 당신이 그것을 줄 수있는 경우에

final byte[] buf = new byte[16384]; 
int count; 

final InputStream in = new FileInputStream(...); 
final OutputStream out = servlet.getOutputStream(); 

try { 
    while ((count = in.read(buf)) != -1) 
     out.write(buf, 0, count); 
    out.flush(); 
} catch (...) { 
} finally { 
    in.close(); 
} 

, 구아바와 Closer I을 처리하기 위해/O 리소스를 사용합니다.

0

전체 포털 페이지에 포함되지 않은 다운로드 된 사용자 정의 유형이 포함 된 Liferay 용 sample-struts portlet이 있습니다 (예 : 콘텐츠 유형은 일반적인 포털의 HTML과 다를 수 있습니다. this example에서 이미지/jpeg입니다. 문제에 따라 이것을 적용하십시오.

관련 문제