File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed
src/main/java/cmf/commitField/domain/File/service Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ package cmf .commitField .domain .File .service ;
2+
3+ import org .springframework .boot .context .properties .ConfigurationProperties ;
4+ import org .springframework .stereotype .Component ;
5+
6+ //@Configuration
7+ @ Component
8+ @ ConfigurationProperties (prefix = "custom.file" )
9+ public class FileProperties {
10+ private String uploadDir ;
11+
12+ public String getUploadDir () {
13+ return uploadDir ;
14+ }
15+
16+ public void setUploadDir (String uploadDir ) {
17+ this .uploadDir = uploadDir ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package cmf .commitField .domain .File .service ;
2+
3+ import org .springframework .stereotype .Service ;
4+ import org .springframework .web .multipart .MultipartFile ;
5+
6+ import java .io .IOException ;
7+ import java .nio .file .Files ;
8+ import java .nio .file .Path ;
9+ import java .nio .file .Paths ;
10+ import java .nio .file .StandardCopyOption ;
11+
12+ @ Service
13+ public class FileService {
14+
15+ // resources/static/uploads 폴더 경로
16+ private final String UPLOAD_DIR = "src/main/resources/static/uploads" ; // 상대 경로
17+
18+ // 파일 저장 메소드
19+ public String saveFile (MultipartFile file ) throws IOException {
20+ // 파일 이름을 유니크하게 생성
21+ String filename = System .currentTimeMillis () + "_" + file .getOriginalFilename ();
22+
23+ // 파일 경로 생성
24+ Path path = Paths .get (UPLOAD_DIR , filename ); // static/uploads 폴더에 저장
25+
26+ // 디렉토리가 존재하지 않으면 생성
27+ Files .createDirectories (path .getParent ());
28+
29+ // 파일 저장
30+ Files .copy (file .getInputStream (), path , StandardCopyOption .REPLACE_EXISTING );
31+
32+ // 저장된 파일의 URL 반환 (웹에서 접근할 수 있는 경로)
33+ return "/uploads/" + filename ; // 클라이언트가 접근할 수 있는 URL 반환
34+ }
35+ }
36+
You can’t perform that action at this time.
0 commit comments