* View 정의
/**
* 파일 다운로드시 Controller 에서 return 할 목적으로 생성한 view
*
* @author STEVE
*/
@Slf4j
@Component("fileDownloadView")
public class FileDownloadView extends AbstractView {
private FileInputStream fin = null;
private BufferedInputStream bis = null;
private ServletOutputStream sout = null;
private BufferedOutputStream bos = null;
@SuppressWarnings("rawtypes")
@Override
protected void renderMergedOutputModel(Map params, HttpServletRequest request, HttpServletResponse response) throws Exception {
String serverFilePath = (String) params.get("serverFilePath"); // 다운로드 받을 파일경로 (서버 임시저장파일 전체경로)
String clientFileName = (String) params.get("clientFileName"); // 다운로드 받을 때 저장할 파일명 (클라이언트가 보는)
response.setContentType("application/octet-stream;");
File file = new File(serverFilePath);
try {
if (file.exists()) {
response.setHeader("Content-Disposition", "attachment; filename=" + new String(clientFileName.getBytes("UTF-8"), "ISO8859_1") + ";");
byte buff[] = new byte[2048];
int bytesRead;
fin = new FileInputStream(file);
bis = new BufferedInputStream(fin);
sout = response.getOutputStream();
bos = new BufferedOutputStream(sout);
while ((bytesRead = bis.read(buff)) != -1) {
bos.write(buff, 0, bytesRead);
}
bos.flush();
fin.close();
sout.close();
bis.close();
bos.close();
} else {
// 자체 예외발생
throw new Exception("해당 파일이 존재하지 않습니다.");
}
} catch (Exception e) {
log.error("", e);
} finally {
if (fin != null) {
fin.close();
}
if (sout != null) {
sout.close();
}
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
}
}
* Controller method 적용
// 파일 다운로드를 위한 값 설정 (FileDownloadView) 참조
model.addAttribute("serverFilePath", serverFilePath);
model.addAttribute("clientFileName", pdfFileName);
// 파일 다운로드
return "fileDownloadView";