2012-06-12 6 views
2

나는 파일 이름의 위치는 JSP 스피 호출 제어 방법 다운로드에서 다운로드 옵션의 파일 name.On 클릭은 위의 "URL"이Spring MVC를 사용하여 URL에서 파일을 다운로드하는 방법은 무엇입니까?

<a href='<c:url value="/licensing/download.sp?name=${namelist.name}&downloadUrl=${namelist.url}"/>'> 

<img src="/images/download.gif" alt="Download" border="0" align="right"> 

처럼 내 JSP에서 다운로드 옵션입니다 데 , 컨트롤러 있음

public ModelAndView download(HttpServletRequest request, HttpServletResponse response, DevTechBean devTechBean) throws Exception { 
     cat.debug("MySuiteListController: download: begin"); 
     ModelAndView modelView = super.handleLicensingRequest(request, response); 
     String name = request.getParameter("name"); 

     String url1 = request.getParameter("downloadUrl"); 
     cat.debug(" download: url ="+url1); 

     String downloadurl1="https://my.net:8869"+url1; 
     cat.debug(" download: downloadurl ="+downloadurl1); 
    try{ 
     URL url = new URL(downloadurl1); 
     //response.setHeader("Content-Type", "text/csv"); 
     response.setHeader("Content-disposition", "attachment;filename="+name); 
     URLConnection connection = url.openConnection(); 
     InputStream stream = connection.getInputStream(); 
     BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream()); 
     int len; 
     byte[] buf = new byte[1024]; 
     while ((len = stream.read(buf)) > 0) { 
      outs.write(buf, 0, len); 
     } 
     outs.close(); 
    } 
    catch (MalformedURLException e) { 
     cat.error("Error occurrred in url"); 

    } 
    catch (IOException e) { 
     cat.error("Error occurrred "); 

    } 
     String viewName = "swl_download"; 
    modelView.setViewName(viewName); 
return modelView;  

} 

그러나 다운로드를 클릭하면 예외가 발견되지 않습니다. 문제는 url 값 때문이라고 생각합니다. downloadurl =/파일/다운로드/hai.txt의 위 스피 가진 값에

내가 파일을 클릭에

<a href="${namelist.url}"/> 
<img src="/images/download.gif" alt="Download" border="0" align="right"></a><br/><br/></td> 

가 스피 HREF 만주는 여기에 URL을 https://my.net:8869//files/download/hai.txt(but와 브라우저에서 여는 줄 때 이 링크 "/files/download/hai.txt"는 전체 링크가 어떻게 전달되는지 알지 못합니다.

그러나이 링크를 클릭하면 컨트롤러를 호출하여 해당 파일을 팝업으로 열 수 있습니다.

<a href='<c:url value="/download.sp?name=${namelist.name}&downloadUrl=${namelist.url}"/>'> 

파일을 찾을 수 없습니다. 예외입니다. 는 나는 내가

String downloadurl1="https://my.net:8869"+url1; 

이상과 같이에서 추가 한하지만 난이 exception.Please 날이를 해결하는 데 도움이 찾을 수없는 파일을 얻고있다 downloadUrl.so을 인해 생각합니다.

+0

뭔가 라인 1에없는 .. –

+0

@FelixChristy을 : 덕분에 내가 – Rahul

+0

편집 한을 내가 사용하는 것이다'같이 getResourceAsStream()'TXT 파일을 얻을 수 있습니다. – Ved

답변

1

이것은 단지 의사 코드입니다. 필요에 따라 변경하십시오.

InputStream is = getClass().getResourceAsStream("filename"); 

먼저 getClass()가 어떤 디렉토리를 가리키는 지 확인하십시오 (확실치는 않지만 App의 HOME이어야합니다). 그런 다음 파일을 같은 위치에 놓습니다.

희망이 도움이 될 것입니다. source

1

이 시도 :

@RequestMapping(value="/viewAttach", method = RequestMethod.GET) 
public ModelAndView viewAttach(@RequestParam(value="article_id", required = true) String article_ref, HttpSession session, HttpServletResponse response) 
{ 

    /* *** Check Session *** */ 
    try { 

     // Direclty from pier.content.GetContent written by ang94402 

     URL url = new URL(binaryURL);   
     response.setHeader("Content-disposition", "attachment;filename=" + binary.getName()); 

     //Set the mime type for the response 
     response.setContentType("application/pdf"); 

     // URLConnection connection = url.openConnection(); 
     InputStream is = url.openStream(); 

     BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream()); 
     int len; 
     byte[] buf = new byte[1024]; 
     while ((len = is.read(buf)) > 0) { 
      outs.write(buf, 0, len); 
     } 
     outs.close(); 

    } catch (MalformedURLException e) { 
     logger.error("Error ModelAndView.viewMain - MalformedURLException : " + e.toString() + " -- " + e.getStackTrace()[0].toString()); 
     return null; 
    } catch (IOException e) { 
     logger.error("Error ModelAndView.viewMain - IOException : " + e.toString() + " -- " + e.getStackTrace()[0].toString()); 
     return null; 
    } 


    return null; 

} 
관련 문제