<!-- fileViewResolver(download) --> <bean id="fileDownloadView" class="egovframework.synervelly.file.web.FileDownloadView" /> <bean id="fileViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="0" /> </bean> |
@RequestMapping(value="/admin/templet/insertTempletRegit.do") public ModelAndView insertTempletAdmin(@ModelAttribute("templetVO") TempletVO templetVO, @ModelAttribute("SessionVO") SessionVO sessionVO, HttpServletRequest request, ModelMap model) throws Exception { File file = new File("C:/Users/SeungGyun/Downloads","SDFormatterv3.1.zip"); return new ModelAndView("fileDownloadView", "downloadFile", file); } |
import java.io.File; 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; charset=utf-8"); }
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { File file = (File) model.get("downloadFile"); 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"); } else { fileName = new String(file.getName().getBytes("utf-8"), "iso-8859-1"); } 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); } finally { if (fis != null) try { fis.close(); } catch (IOException ex) { } } out.flush(); } } |
작동 방식 리턴값이을 파일 다운로드 클레스로 넘기면 리턴 리졸버에서
파일 다운로드 방식으로 사용자에게 전송함 .
RECENT COMMENT