security-context.xml 에 등록할 사항은 없다.

 

1. util.js

    /**
     * 템플릿 파일다운로드
     * UT.downloadTmpl("/resource/template.xlsx")
     * 
     */
    downloadTmpl : function(templateFilePath)
    {
        var queryString = UT.formatString("{0}={1}", "filePath", templateFilePath);
        var _url = "/downloadTmpl.do?"+ CipherUtil.encrypt(queryString);
        UT.postSubmit(_url, {}, "_self", true);
    }

 

2. SharedController.java

    /**
     * 템플릿 파일다운로드
     *
     * @author : 
     * @param : param
     * @return : Response
     * @Date : 
     * @Method Name : downloadTmpl
     */
    @RequestMapping(value="/downloadTmpl.do")
    public ResponseEntity<InputStreamResource> downloadTmpl(HttpServletRequest request, HttpServletResponse response)
    {
        String _queryString = request.getQueryString();
        
        try
        {
            if (StringUtils.isNotEmpty(_queryString))
            {
                _queryString = cipherUtil.decrypt(_queryString);
                
                String[] param = _queryString.split("=");
                
                if (param.length == 2)
                {
                    String key     = param[0];
                    String value   = param[1];
                    
                    if ("filePath".equals(key))
                    {
                        String _rootDir = request.getServletContext().getRealPath("/");
                        String _filePath = String.format("%s%s%s", _rootDir, File.separator, value);
                        File _file = new File(_filePath);
                        
                        InputStreamResource resource = new InputStreamResource(new FileInputStream(_filePath));
                        String _fileName = URLEncoder.encode(_file.getName(), "UTF-8");
                        
                        HttpHeaders responseHeaders = new HttpHeaders();
                        responseHeaders.set("Content-Length", String.valueOf(_file.length()));
                        //responseHeaders.set("Content-Type", mimeTypeUtil.mimeTypeForFileExtension(FilenameUtils.getExtension(_fileName)) + "; charset=UTF-8");
                        responseHeaders.set("Content-Type", "application/octet-stream; charset=UTF-8");
                        responseHeaders.set("Content-Disposition", "attachment; filename=\"" + _fileName + "\"; filename*=UTF-8''" + _fileName);
                        responseHeaders.set("file-name", _fileName);
                        
                        return new ResponseEntity(resource, (MultiValueMap)responseHeaders, HttpStatus.OK);
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }