본문 바로가기
프로그램/자바스크립트

fetch 파일다운로드

by cbwstar 2023. 7. 12.
728x90
반응형
javascript
닫기
/* 자바 스크립트 */ function fileDownLoad(fileName,finalFileName,path){ const paramData = { ​​​​​​​​​method: 'POST', ​​​​​​​​​body: JSON.stringify({ fileName, finalFileName,path }), ​​​​​​​​​headers: { ​​​​​​​​​​​​'Content-Type': 'application/json' ​​​​​​​​​} ​​​​}; ‌fetch('/logFarm/downloadFetchFiles.json',paramData) ‌​​.then(res => res.blob()) ‌​​.then(data => { ‌‌​​const a = document.createElement("a"); ‌‌​​a.href = window.URL.createObjectURL(data); ‌‌​​a.download = fileName ‌‌​​a.click(); ‌​​}); }
java
닫기
/* 자바 */ /** * * <pre> * @desc fetch 파일 다운로드 ‌​* @param ‌​* @return ‌​* @return ‌​* @throws ‌​* </pre> ‌​*/ @RequestMapping(value = "/downloadFetchFiles.json" ) public void downloadFetchFiles(HttpServletRequest request, HttpServletResponse response,@RequestBody String jsonData) { ‌‌logger.debug("-------------- 파일다운로드 -------------"); ‌‌logger.debug("sendPushMessage.json jsonData 문자형식 : " + jsonData); ‌‌ ‌‌JsonParser parser = new JsonParser(); ‌​​​​JsonElement element = parser.parse(jsonData); ‌​​​​String getPath = element.getAsJsonObject().get("path").getAsString(); ‌​​​​String getName = element.getAsJsonObject().get("fileName").getAsString(); ‌​​​​String getOrginFileName = element.getAsJsonObject().get("finalFileName").getAsString(); ‌​​​​​​ ‌‌‌‌​ ‌‌logger.debug("getOrginFileName : " + getOrginFileName); ‌‌logger.debug("getName : " + getName); ‌‌logger.debug("getPath : " + getPath); ‌‌//String filePath = EgovProperties.getProperty("Globals.fileStorePath").trim() + getPath +SP+ getOrginFileName; ‌‌String filePath = EgovProperties.getProperty("Globals.fileStorePath").trim() + getOrginFileName; ‌‌logger.debug("filePath : " + filePath); ‌‌ ‌‌Map<String, Object> reMap = new HashMap<>(); ‌​​​​OutputStream os = null; ‌​​​​InputStream is = null; ‌‌BufferedInputStream bis = null; ‌‌BufferedOutputStream bos = null; ‌‌ServletOutputStream sout = null; ‌‌File f = new File(filePath); ‌​​​try { ‌‌‌​​​response.reset(); ‌‌‌​​​response.setCharacterEncoding("utf-8"); ‌‌‌​​​response.setContentType("application/octet-stream"); ‌‌‌​​​response.setHeader("Content-Disposition","attachment;filename=" + new String(getName.getBytes("UTF-8"), "ISO8859-1")); ‌‌‌‌is = new FileInputStream(f); ‌‌‌‌if (is == null) { ‌‌‌‌‌reMap.put("msg", "파일이 존재 하지 않습니다."); ‌‌‌​​​​​​​} ‌‌‌‌sout = response.getOutputStream(); ‌‌‌‌bis = new BufferedInputStream(is); ‌‌‌‌bos = new BufferedOutputStream(sout); ‌‌‌‌byte[] buff = new byte[2048]; ‌‌‌‌int bytesRead; ‌‌‌‌while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { ‌‌‌‌​​​bos.write(buff, 0, bytesRead); ‌‌‌‌} ‌‌‌‌bos.flush(); ‌‌‌‌bos.close(); ‌‌‌‌bis.close(); ‌‌‌‌is.close(); ‌‌​​​​​​​​sout.close(); ‌‌‌// os.close(); ‌​​​} catch (IOException e) { ‌‌​​​​​​reMap.put("msg", "처리중 오류가 발생하였습니다."); ‌​​​} catch (Exception e) { ‌‌​​​reMap.put("msg", "처리중 오류가 발생하였습니다."); ‌​​​} finally ‌​​​{ ‌‌​​​if (bis != null) ‌‌​​​{ ‌‌‌​​​try ‌‌‌​​​{ ‌‌‌‌​​​bis.close(); ‌‌‌​​​} catch (RuntimeException e) { ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌‌​​​catch (Exception e) ‌‌‌​​​{ ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌​​​} ‌‌​​​if (bos != null) ‌‌​​​{ ‌‌‌​​​try ‌‌‌​​​{ ‌‌‌‌​​​bos.close(); ‌‌‌​​​} catch (RuntimeException e) { ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌‌​​​catch (Exception e) ‌‌‌​​​{ ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌​​​} ‌‌​​​if (is != null) ‌‌​​​{ ‌‌‌​​​try ‌‌‌​​​{ ‌‌‌‌​​​is.close(); ‌‌‌​​​} catch (RuntimeException e) { ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌‌​​​catch (Exception e) ‌‌‌​​​{ ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌​​​} ‌‌​​​if (sout != null) ‌‌​​​{ ‌‌‌​​​try ‌‌‌​​​{ ‌‌‌‌​​​sout.close(); ‌‌‌​​​} catch (RuntimeException e) { ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌‌​​​catch (Exception e) ‌‌‌​​​{ ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌​​​} ‌‌​​​if (os != null) ‌‌​​​{ ‌‌‌​​​try ‌‌‌​​​{ ‌‌‌‌​​​os.close(); ‌‌‌​​​} catch (RuntimeException e) { ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌‌​​​catch (Exception e) ‌‌‌​​​{ ‌‌‌‌​​​f = null; ‌‌‌​​​} ‌‌​​​} ‌​​​} }
728x90
반응형

'프로그램 > 자바스크립트' 카테고리의 다른 글

국세청 사업자번호 진위확인  (0) 2021.12.14


"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."