POI 라이브러리를 이용하여 엑셀 다운로드 시 엑셀 파일명을 만들어주는데, 파일명에 쉼표가 들어가니 다음과 같은 에러가 났다.
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : The part /docProps/core.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller@1d02d97
검색해보니 파일 경로에 특수문자가 포함되어 있어 나는 오류였다.
파일명을 우리가 임의로 지정하는것이 아니라 '사용자가 입력한 데이터 + 날짜' 이런식으로 저장해주다 보니 파일명을 가공하는 작업이 필요했다.
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");
String formatedNow = now.format(formatter);
String fileName = bldNm+"_"+formatedNow;
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20");
response.setContentType("ms-vnd/excel");
response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(encodedFileName.getBytes("euc-kr"), "8859_1") + ".xlsx\"");
기존에 fileName을 쓰다가 하단에 encodedFileName을 추가해 파일 이름을 URL 인코딩 해주었다.
UTF-8로 인코딩하여 특수문자를 처리해주고, replaceAll로 공백이 인코딩될 때 '+'로 변환되는 문제를 '%20'으로 교체하여 처리해주었다.
이렇게 써주면 에러 해결!