이 게시물은 참조하지 않는 것을 권장합니다.
목차
1. servlet-context.xml 에 BeanNameViewResolver 등록
2. DownloadView 작성
3. servlet-context.xml 에 DownloadView 등록
3. Controller 작성
4. View 작성
1. servlet-context.xml 에 BeanNameViewResolver 등록
제일 먼저 할 일은 servlet-context.xml 에 BeanNameViewResolver 를 등록하는 것이다.
기존에 InternalResourceViewResolver 를 등록했으면, 속성 값 order 를 1로 주고,
BeanNameViewResolver 를 0으로 준다.
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/">
<beans:property name="suffix" value=".jsp">
<beans:property name="order" value="1">
</beans:bean>
<beans:bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<beans:property name="order" value="0">
</beans:bean>
2. DownloadView 작성
그 다음으로 할 일은 DownloadView 클래스를 작성 하는 것 이다.
DownloadView 는 AbstractView 를 상속받는다.
그러면 renderMergedOutputModel() 를 구현해야 하는데, 서버에서 클라이언트로 출력
하는 로직을 이 부분에서 작성한다.
public class DownloadView extends AbstractView {
@Override
protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception {
String originalFileName = (String) map.get("originalFileName");
File file = (File) map.get("downloadFile");
String userAgent = request.getHeader("User-Agent");
String contentType = request.getContentType();
int contentLength = (int) file.length();
response.setContentType(contentType);
response.setContentLength(contentLength);
boolean ie = userAgent.indexOf("MSIE") > -1;
String fileName = null;
if(ie == true) {
fileName = URLEncoder.encode(originalFileName, "UTF-8");
} else {
fileName = new String(originalFileName.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 ioe) { }
}
}
out.flush();
}
}
3. Controller 작성
다음은 Controller 를 작성할 차례이다. View 에서 파일 다운로드에 대한 요청을 받을
Controller 를 작성하면 된다.
@Controller
@RequestMapping(value="/articleFile")
public class ArticleFileController {
@Autowired
private ArticleFileService articleFileService;
@RequestMapping(value="/download")
public ModelAndView download(HttpServletRequest request) throws Exception {
String hashFileName = ServletRequestUtils.getStringParameter(request, "hashFileName");
String basePath = CommonUtils.convertFileSeparator(Const.UPLOAD_FILE_PATH);
ArticleFile articleFile = new ArticleFile();
articleFile.setHashFileName(hashFileName);
articleFile = articleFileService.getArticleFileByHashFileName(articleFile);
File file = new File(basePath + File.separator + hashFileName);
ModelAndView mv = new ModelAndView();
mv.setViewName("downloadView");
mv.addObject("downloadFile", file);
mv.addObject("originalFileName", articleFile.getOriginalFileName());
return mv;
}
}
4. View 작성
View 에서는 특별히 해줄 것은 없다. 다만 지금의 경우에는 hashFileName 을 요청 한다.
<a href="/articleFile/download?hashFileName=${articleFile.hashFileName}">${articleFile.originalFileName}</a>
댓글 없음:
댓글 쓰기