file download
파일 다운로드를 위하여 jsp 파일에 javascript 함수를 추가합니다.
|
1
2
3
4
5
6
7
8
9
10
11
12
|
//첨부파일 다운로드를 위한 요청function fnFileDown(odrNo){
// var 설정
var url = "<c:url value='/your_url/fileDown.do' />"
, param = {
parma1 : document.frm.param1.value
, param2 : document.frm.param2.value
}
callDown(url, param);
//downloadURL(url, param);
} |
임시로 form을 생성하여, 요청을 한 후에 제거하는 방식을 이용하려면 아래의 함수를 호출합니다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
function callDown(url, data, method){
// url과 data를 입력받음
if( url && data ){
// data 는 string 또는 array/object 를 파라미터로 받는다.
data = typeof data == 'string' ? data : jQuery.param(data);
// 파라미터를 form의 input으로 만든다.
var inputs = '';
$.each(data.split('&'), function(){
var pair = this.split('=');
inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />';
});
// request를 보낸다.
$('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
.appendTo('body').submit().remove();
};
}; |
다운로드를 위한 iframe을 생성하여 요청할 때에는 아래의 코드를 호출합니다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function downloadURL(url, data) {
var hiddenIFrameID = 'hiddenDownloader',
data = typeof data == 'string' ? data : $.param(data);
url += "?"+data;
iframe = document.getElementById(hiddenIFrameID);
if (iframe === null) {
iframe = document.createElement('iframe');
iframe.id = hiddenIFrameID;
iframe.style.display = 'none';
document.body.appendChild(iframe);
}
iframe.src = url;
}; |
request 처리
BLOB 데이터를 바이너리 스트림으로 변환하여 반환합니다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
@RequestMapping(value = "fileDown.do")
public void fileDown(HttpServletRequest request, HttpServletResponse response,
@RequestParam Map<String, Object> params) throws Exception {
Map<String, Object> fileInfo = // 다운로드 대상의 파일정보를 할당합니다.
String fileName = // 다운로드 대상 파일명을 할당합니다.
String clientFileNm = "";
if (request.getHeader("user-agent").indexOf("MSIE") != -1) {
// 브라우저가 IE 일 경우는 공백이 '+' 로 인코딩된는 것을 다시 공백으로 바꿔줌
clientFileNm = URLEncoder.encode(fileName, "UTF8").replaceAll("\\+", " ");
} else {
// 브라우저가 IE 가 아닐 경우는 8859-1 로 인코딩함
clientFileNm = new String(fileName.getBytes("UTF-8"));
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Type", "application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + clientFileNm + "\"");
response.setHeader("Content-Transfer-Encoding", "binary;");
response.setHeader("Pragma", "no-cache;");
response.setHeader("Expires", "-1;");
OutputStream out = response.getOutputStream();
java.sql.Blob blob = (Blob) fileInfo.get(gv.FILE_CNTN);
IOUtils.copy(blob.getBinaryStream(), out);
out.flush();
out.close();
return;
} |
파일 다운로드시에 설정한 파일명으로 다운이 되지 않고, 요청한 URL명으로 파일이 다운로드 된다면,
response.setHeader("Content-Disposition", "attachment; filename=\"" + clientFileNm + "\"");
이 부분에 오타가 있는지 확인이 필요합니다.
참고
'Programming > Spring' 카테고리의 다른 글
| [spring] 프레임워크 개요 (0) | 2016.04.17 |
|---|---|
| [spring] Servlet & EJB & Spring (0) | 2016.04.17 |
| [Spring] File Upload (0) | 2015.08.12 |
| [Spring] Getting Started Guides (0) | 2013.10.17 |
| [Spring] Spring Docs (0) | 2013.10.17 |
댓글