전체소스 : spring304.zip

 

 

FileDownloadController.java

package sp.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

 

@Controller
public class FileDownloadController {


 @RequestMapping(value="/filedown.sp")
 public ModelAndView fileDownload(){
  System.out.println("----- FileDownloadController.fileDownload() -----");
  
  ModelAndView modelAndView = new ModelAndView("filedownload");

  return modelAndView;
 }
}

 

FileDownloadView.java

package sp.mvc.view;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView;

 

public class FileDownloadView extends AbstractView {

 

 public FileDownloadView(){
  setContentType("application/download; ccharset=utf-8");
 }
 
 @Override
 protected void renderMergedOutputModel(Map<String, Object> model,
   HttpServletRequest req, HttpServletResponse res) throws Exception {
  // TODO Auto-generated method stub

  System.out.println("---- FileDownloadView.renderMergedOutputModel() ----");
  
  java.io.File file = new java.io.File("C:\\Ins.log");
  
  res.setContentType(getContentType());
  res.setContentLength(100);

  
  String userAgent = req.getHeader("User-Agent");
  String fileName = null;
  
  if(userAgent.indexOf("MSIE") > -1){
   fileName = URLEncoder.encode(file.getName(), "utf-8");
  }else{
   fileName = new String(file.getName().getBytes("utf-8"), "iso-8859-1");
  }
  
  res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
  res.setHeader("Content-Transfer-Encoding", "binary");

  
  OutputStream os = res.getOutputStream();
  FileInputStream fis = null;
  
  try{
   fis = new FileInputStream(file);
   FileCopyUtils.copy(fis, os);
  }finally{
   if(fis != null){
    try{
     fis.close();
    }catch(IOException e){
     System.out.println("exception : " + e.toString());
    }
   }
  }
  os.flush();
  
 }
}

 

main_config.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

 <context:annotation-config/>
 <context:component-scan base-package="sp.mvc.controller"/>
 
 <!-- bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:alwaysUseFullPath="true"/ -->
 <!-- bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" p:alwaysUseFullPath="true"/ --> 
 
  
 <!-- ViewResolver 설정 -->
 <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="1"/>
 
 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:order="2" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"></bean>


 
 <!-- 각종 bean 설정 -->
 <bean id="filedownload" class="sp.mvc.view.FileDownloadView"></bean>
 
</beans>

● BeanNameViewResolver : 뷰 이름과 동일한 이름을 갖는 빈을 뷰 객체로 사용

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 
  <display-name>spring304</display-name>

 <filter>
  <filter-name>characterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>

 <filter-mapping>
  <filter-name>characterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>


 <!-- 공통 빈 설정
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>
  
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 -->
 
 <servlet>
  <servlet-name>spring304</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/main_config.xml
   </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>spring304</servlet-name>
  <url-pattern>*.sp</url-pattern>
 </servlet-mapping>


</web-app>

 

 

결과

요청URL

   http://localhost:8090/spring304/filedown.sp

 

----- FileDownloadController.fileDownload() -----
---- FileDownloadView.renderMergedOutputModel() ----

 

 

 

 

[참고자료] Spring 3.0 프로그래밍-최범균

+ Recent posts