Skip to content

Commit

Permalink
Bug : 서버에서 Jar 파일 경로 찾지 못하는 오류 수정 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanmin-00 committed Jan 24, 2025
1 parent 6d6de3f commit 67ed4fc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
19 changes: 18 additions & 1 deletion src/main/java/com/ripple/BE/global/excel/ExcelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;

public class ExcelUtils {

// 엑셀 파일을 파싱하여 데이터 리스트로 반환하는 메서드
public static List<Map<String, String>> parseExcelFile(String filePath, int sheetNumber)
throws Exception {
List<Map<String, String>> rows = new ArrayList<>();
try (InputStream is = new FileInputStream(new File(filePath));

// InputStream으로 파일 읽기
try (InputStream is = getInputStream(filePath);
Workbook workbook = new XSSFWorkbook(is)) {

Sheet sheet = workbook.getSheetAt(sheetNumber);
Expand All @@ -35,6 +38,8 @@ public static List<Map<String, String>> parseExcelFile(String filePath, int shee
// 각 행의 데이터를 읽어 Map으로 저장
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) continue;

Map<String, String> rowData = new HashMap<>();
for (int j = 0; j < headers.size(); j++) {
Cell cell = row.getCell(j);
Expand All @@ -46,6 +51,18 @@ public static List<Map<String, String>> parseExcelFile(String filePath, int shee
return rows;
}

// filePath로부터 InputStream 생성
private static InputStream getInputStream(String filePath) throws Exception {
try {
// ClassPathResource로 JAR 내부 리소스 파일 처리
ClassPathResource resource = new ClassPathResource(filePath);
return resource.getInputStream();
} catch (Exception e) {
// 파일이 JAR 내부가 아니라면 FileInputStream으로 처리
return new FileInputStream(new File(filePath));
}
}

// 셀의 값을 문자열로 변환하는 유틸리티 메서드
private static String getCellValueAsString(Cell cell) {
if (cell == null) return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public ResponseEntity<Object> handlePostException(final PostException e) {
public ResponseEntity<Object> handleNewsException(final NewsException e) {
return handleExceptionInternal(e.getErrorCode());
}

@ExceptionHandler(ImageException.class)
public ResponseEntity<Object> handleImageException(final ImageException e) {
return handleExceptionInternal(e.getErrorCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
import com.ripple.BE.learning.exception.errorcode.LearningErrorCode;
import com.ripple.BE.learning.repository.LearningSetRepository;
import com.ripple.BE.learning.repository.QuizRepository;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -39,9 +37,8 @@ public class LearningAdminService {
@Transactional
public void createLearningSetByExcel() {
try {
File file = new ClassPathResource(FILE_PATH).getFile();

List<LearningSet> learningSetList = parseLearningSetsFromExcel(file.getPath());
List<LearningSet> learningSetList = parseLearningSetsFromExcel();
List<LearningSet> existingLearningSets = learningSetRepository.findAll();

Map<String, LearningSet> learningSetMap =
Expand All @@ -67,8 +64,8 @@ public void createLearningSetByExcel() {
existingSet -> existingSet.getName().equals(learningSet.getName())))
.collect(Collectors.toList());

addConceptsToLearningSets(file.getPath(), newLearningSetMap);
addQuizzesToLearningSets(file.getPath(), newLearningSetMap);
addConceptsToLearningSets(FILE_PATH, newLearningSetMap);
addQuizzesToLearningSets(FILE_PATH, newLearningSetMap);

learningSetRepository.saveAll(newLearningSets);

Expand All @@ -78,8 +75,9 @@ public void createLearningSetByExcel() {
}
}

private List<LearningSet> parseLearningSetsFromExcel(String filePath) throws Exception {
return ExcelUtils.parseExcelFile(filePath, LEARNING_SET_SHEET_INDEX).stream()
private List<LearningSet> parseLearningSetsFromExcel() throws Exception {
return ExcelUtils.parseExcelFile(LearningAdminService.FILE_PATH, LEARNING_SET_SHEET_INDEX)
.stream()
.map(LearningSetDTO::toLearningSetDTO)
.map(LearningSet::toLearningSet)
.collect(Collectors.toList());
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/com/ripple/BE/term/service/TermAdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import com.ripple.BE.term.exception.TermException;
import com.ripple.BE.term.repository.TermJdbcRepository;
import com.ripple.BE.term.repository.TermRepository;
import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -39,10 +37,7 @@ public class TermAdminService {
@Transactional
public void createTermByExcel() {
try {

File file = new ClassPathResource(FILE_PATH).getFile();

List<Term> termList = parseTermFromExcel(file.getPath());
List<Term> termList = parseTermFromExcel();
Set<String> existingTitles =
termRepository.findAll().stream().map(Term::getTitle).collect(Collectors.toSet());

Expand All @@ -65,8 +60,8 @@ public void createTermByExcel() {
}
}

private List<Term> parseTermFromExcel(String filePath) throws Exception {
return ExcelUtils.parseExcelFile(filePath, TERM_SHEET_INDEX).stream()
private List<Term> parseTermFromExcel() throws Exception {
return ExcelUtils.parseExcelFile(TermAdminService.FILE_PATH, TERM_SHEET_INDEX).stream()
.map(TermDTO::toTermDTO)
.map(Term::toTermEntity)
.collect(Collectors.toList());
Expand Down

0 comments on commit 67ed4fc

Please sign in to comment.