Spring + jasperreport (5) Download 기능 추가

북마크 추가

REPORT DOWNLOAD 기능 추가. Report 출력 시, WEB_INF/reports/ 폴더에 생성된 파일 다운로드

 

1.***-servlet.xml 설정

 

 

 

<bean id="download" class="egovframework.com.aam.report.DownloadView" />  // DownloadView.java를 생성할 위치


<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">

  <property name="order" value="0"/>

</bean>


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  <property name="prefix" value="/" />

  <property name="suffix" value=".jsp" />

  <property name="order">

  <value>1</value>

  </property>

</bean> 

 

 

2. ReportController.java

 

@RequestMapping("/reports/Download.do")

public ModelAndView DownloadController(String type) throws UnsupportedEncodingException {

  System.out.println(type);

  String fileName = null;

  URL location = getClass().getResource("/word.jasper");

  String reportPath = location.getPath();

  String path = reportPath.substring(0, reportPath.length() - 11);


  if(type.equals("excel")){

  fileName=prjName +excelName;

  }else if(type.equals("word")){

  fileName=prjName + wordName;

  }


  path = new String(path.getBytes("8859_1"), "UTF-8");

  fileName = new String(fileName.getBytes("8859_1"), "UTF-8");

  String fullPath = path + fileName;


  File file = new File(fullPath);


  return new ModelAndView("download", "downloadFile", file);

} 

 

 

3. DownloadView.java

 

public class DownloadView extends AbstractView{

    public DownloadView(){

        super.setContentType("application/download; charset=utf-8");   

    }

    @Override

protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

         File file = (File)model.get("downloadFile"); //downloadFile의 이름으로 삽입된 파일!

        

         System.out.println("DownloadView --> file.getPath() : " + file.getPath());       

         System.out.println("DownloadView --> file.getName() : " + file.getName());

        

         response.setContentType(getContentType());       

         response.setContentLength((int)file.length());       

        

         String userAgent = request.getHeader("User-Agent");       

        

         boolean ie = userAgent.indexOf("MSIE") > -1;               

         String fileName = null;

        

             if(ie){                        

                 fileName = URLEncoder.encode(file.getName(), "utf-8").replace("+","%20");                

             } else {

                 fileName = new String(file.getName().getBytes("utf-8"), "iso-8859-1").replace("+","%20");

             }// end if; 

response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");        

         response.setHeader("Content-Transfer-Encoding", "binary");                

         OutputStream out = response.getOutputStream();                

         FileInputStream fis = null;

        

         try {                        

             fis = new FileInputStream(file);                        

             FileCopyUtils.copy(fis, out);                                 

         } catch(Exception e){                        

             e.printStackTrace();                    

         }finally{                        

             if(fis != null){                                

                 try{                   

                     fis.close();               

                 }catch(Exception e){

                     e.printStackTrace();                    

                 }           

             }                    

         }// try end;                

        

         out.flush();


    }

}

AD
관리자
2014-07-13 20:30
SHARE