[서버] File I/O

2025. 5. 7. 10:13·서버

[thymeleaf 추가]

스프링스타터로 thymeleaf 추가시

build.gradle에 기존 세팅한게 다 날아감 

Finish누르지않고 추가되는 줄을 복사해서 손수 붙여넣어야함

Finish 누른경우 커맨드z로 되돌리고 붙여넣기

 

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

 

프로젝트에 에러나면 다이나믹모듈 5.0으로 변경 

 

타임리프 세팅도 해야함 thymeleaf.properties 생성해서 openwith Spring properties, enc검색해서 스프링프로퍼티스에 *.properties 추가

 

thymeleaf.properties

spring.thymeleaf.cache=false
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true

#thymeleaf View
spring.thymeleaf.prefix=classpath:/templates
spring.thymeleaf.suffix=.jsp
spring.thymeleaf.mode=html
#controller return JSP,JSTL,thymeleaf 구별하기 위함 
spring.thymeleaf.view-names=/*

 

application.properties에

spring.config.import=thymeleaf.properties

추가해서 임포트하기 

 

[DB 추가]

스프링스타터로 필요한거 다 추가 (jdbc,mybatis,db커넥터) 해서 복사해서

build.gradle 의 dependencies에 추가

	//DB
	implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
	implementation 'org.springframework.boot:spring-boot-starter-jdbc'
	runtimeOnly 'com.mysql:mysql-connector-j'
	implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.4'
	//test : 디버깅용 임시로 디비연결
	testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.4'

 

database.properties

#데이터베이스 여러개 연결시 이거 봉인풀면 망함 하나만쓸때 이렇게 쓰기 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
로컬용(개발시 사용)
spring.datasource.url=jdbc:mysql://서버ip:13306/webapi
#배포용
#spring.datasource.url=jdbc:mysql://localhost:3306/webapi
spring.datasource.username=아이디
spring.datasource.password=비번

#mybatis
mybatis.type-aliases-package=kr.co.koo.*
mybatis.mapper-locations=classpath:/mapper/*.xml

메인properties파일에

spring.config.import=thymeleaf.properties,database.properties

콤마로 이어주기


[코드]

[webfile.html]

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>파일업로드 - Server</title>
</head>
<body>

<form id="frm" method="POST" action="./webfileok.do" enctype="multipart/form-data">
단일 파일 업로드 : <input type="file" name="file1"><br>
<input type="button" value="전송" onclick="fileok()">
</form>

</body>
<script>
function fileok(){
	frm.submit();
}
</script>
</html>

[controller.java]

@PostMapping("/webfileok.do")
	public String webfileok(@RequestParam("file1")MultipartFile file1,
			ServletRequest req
			) throws Exception {
		long filesize = file1.getSize();	//파일용량
//		this.log.info(file1.getOriginalFilename());
		String url = req.getServletContext().getRealPath("/upload/");
		File f = new File(url);
		this.log.info(url);
		
		String copys = url + file1.getOriginalFilename();
		try {
			FileCopyUtils.copy(file1.getBytes(), new File(copys));
		}catch (Exception e) {
			this.log.info(e.toString());
		}
		//spring-boot에서는 RealPath가 src 디렉토리에 생성을 하여 경로룰 생성
		//spring에서는 RealPath가 temp 디렉토리에 생성을 하여 경로를 생성 
		
		return null;
	}

 

배포시 업로드한 파일은 src/upload에 저장됨 

저작자표시 비영리 변경금지 (새창열림)
'서버' 카테고리의 다른 글
  • [서버] 이모저모
  • [배포]
  • [서버] 보안
  • [서버] Docker
9na0
9na0
응애
  • 9na0
    구나딩
    9na0
  • 전체
    오늘
    어제
    • 분류 전체보기 (169) N
      • Web (95) N
      • Java (28)
      • 데이터베이스 (12)
      • 세팅 (11)
      • 과제 (3)
      • 쪽지시험 (2)
      • 정보처리기사 (2)
      • 서버 (13)
  • 블로그 메뉴

    • 링크

      • 구깃
    • 공지사항

    • 인기 글

    • 태그

      net5~10
      ab1
      net2
      io_dto
      net1
      java_io1~10
      net3
      file24
      Oracle
      noticewriteok
      macbook pro m4
      re2
      net4
      re_java10
      file25_t
      spring-boot
      exam1_1~10
      file25
      datalist
      notice_writer
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    9na0
    [서버] File I/O
    상단으로

    티스토리툴바