jsp |
<a href="#none" class="download" onclick="fn_Download('c:/nasdata/ast/rtn/201902','파일명.xlsx')">파일명.xlsx</a> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
js |
function fn_Download(filePath,fileName){ $("#frmPopFiledown").find("#filePath").val(filePath); $("#frmPopFiledown").find("#fileName").val(fileName); document.frmPopFiledown.action = "/downloadFiles.json"; document.frmPopFiledown.submit(); } |
controller |
@RequestMapping(value = "//downloadFiles.json", method = RequestMethod.POST) public void downloadAppFile(HttpServletRequest request, HttpServletResponse response) throws Exception { try { Map<String, Object> map = new HashMap<String, Object>(); cardComService.downloadFile(request, response); } catch (Exception e) { e.printStackTrace(); } } |
serviceImpl |
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws Exception { try { String filePath = (String)request.getParameter("FILE_PATH"); // String fileName = (String)request.getParameter("FILE_NAME"); System.out.println(filePath); System.out.println(fileName); //저장경로, 파일이름 File f = new File(filePath + "/" + fileName); BufferedInputStream bis = null; BufferedOutputStream bos = null; byte b[] = new byte[4096]; int read = 0; String fileExtention = fileName.substring(fileName.lastIndexOf(".") + 1); if(f.isFile()){ response.setHeader("Pragma", "no-cache"); response.setHeader("Accept-Ranges", "bytes"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ";"); response.setHeader("Content-Transfer-Encoding", "binary;"); response.setCharacterEncoding("UTF-8"); switch (fileExtention){ case "ppt": response.setContentType("application/vnd.ms-powerpoint; charset=utf-8"); break; case "pptx": response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation; charset=utf-8"); break; case "xls": response.setContentType("application/vnd.ms-excel; charset=utf-8"); break; case "xlsx": response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8"); break; case "doc": response.setContentType("application/msword; charset=utf-8"); break; case "docx": response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8"); break; case "pdf": response.setContentType("application/pdf; charset=utf-8"); break; default :response.setContentType("application/octet-stream; charset=utf-8"); break; } // response.setContentType("application/octet-stream; charset=utf-8"); response.setContentLength((int)f.length());//size setting try{ bis = new BufferedInputStream(new FileInputStream(f)); bos = new BufferedOutputStream(response.getOutputStream()); while((read = bis.read(b)) != -1){ bos.write(b, 0, read); } bis.close(); bos.flush(); bos.close(); }catch(Exception e){ throw new EgovBizException(" 파일 생성 에러가 발생하였습니다."); }finally{ if(bis != null){ bis.close(); } //f.delete(); } }else{ throw new EgovBizException("파일이 잘못 되었습니다."); } } catch (Exception e) { e.printStackTrace(); } } |
댓글