FRAMEWORK/SPRING
Spring MVC download files(04)
적외선
2014. 12. 2. 14:52
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
● 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>
|
결과
출처 - http://0px.kr/326