다운로드처리 보완

This commit is contained in:
KNKIM 2021-08-03 13:22:26 +09:00
parent 3a47fb8185
commit 77f24fb2fa

View File

@ -13,21 +13,43 @@ import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView;
@Component("downloadView")
/**
* <pre>
* @Class Name : DownloadView.java
*
* @Description : 파일 다운로드 VIEW 처리 클래스
*
*
* @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
*
* </pre>
*
* @ ------------ -------- ---------------------------
* @ 수정일 수정자 수정내용
* @ ------------ -------- ---------------------------
* @ 2021. 8. 3. KNKIM 최초 생성
*
*
* @author 이씨플라자 * DIGITALSHIP KNKIM
* @since 2021. 8. 3.
* @version 1.0
*
*/
public class DownloadView extends AbstractView {
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
File file = (File) model.get("downloadFile");
if(file != null) {
if(file == null) return;
String fileName = (String) model.get("fname");
String encfileName = null;
String userAgent = request.getHeader("User-Agent");
if (userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident") > -1) {
encfileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");;
encfileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
} else if (userAgent.indexOf("Chrome") > -1) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < fileName.length(); i++) {
@ -42,32 +64,19 @@ public class DownloadView extends AbstractView {
} else {
encfileName = new String(fileName.getBytes("utf-8"));
}
response.setContentType(getContentType());
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment; filename=\"" + encfileName + "\";");
response.setHeader("Content-Transfer-Encoding", "binary");
OutputStream out = response.getOutputStream();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
try( FileInputStream fis = new FileInputStream(file);
OutputStream out = response.getOutputStream())
{
FileCopyUtils.copy(fis, out);
} catch(Exception e){
e.printStackTrace();
}finally{
if(fis != null){
try{
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(out != null) {
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}