다운로드처리 보완

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

View File

@ -13,61 +13,70 @@ import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView; 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 { public class DownloadView extends AbstractView {
@Override @Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
throws Exception {
File file = (File)model.get("downloadFile"); File file = (File) model.get("downloadFile");
if(file != null) {
String fileName =(String)model.get("fname");
String encfileName = null;
String userAgent = request.getHeader("User-Agent");
if(userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident") > -1){ if(file == null) return;
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++) {
char c = fileName.charAt(i);
if(c > '~') {
sb.append(URLEncoder.encode(""+c, "UTF-8"));
}else {
sb.append(c);
}
}
encfileName = sb.toString();
}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(); String fileName = (String) model.get("fname");
FileInputStream fis = null; String encfileName = null;
try { String userAgent = request.getHeader("User-Agent");
fis = new FileInputStream(file);
FileCopyUtils.copy(fis, out);
} catch(Exception e){
e.printStackTrace();
}finally{
if(fis != null){
try{
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(out != null) { if (userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident") > -1) {
out.flush(); 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++) {
char c = fileName.charAt(i);
if (c > '~') {
sb.append(URLEncoder.encode("" + c, "UTF-8"));
} else {
sb.append(c);
}
}
encfileName = sb.toString();
} 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");
try( FileInputStream fis = new FileInputStream(file);
OutputStream out = response.getOutputStream())
{
FileCopyUtils.copy(fis, out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
} }
} }