YJ의 새벽
Spring 13 ( 프로필 올리기 ( 이미지 수정 ) ) 본문
---pom.xml 에 파일업로드를 위한 메이븐 추가
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
--- root-context.xml 에 파일업로드를 위한 빈 등록, 추가
<!--
파일 업로드를 위한 MutipartResolver 구현체 CommonsMultipartResolver bean 등록
-> CommonsMultipartResolver를 bean으로 등록하면
multipart/form-data 형식으로 요청 시 input type="file" 태그를 자동적으로 인식하여 MultipartFile 객체로 반환하고
파일 외의 데이터(정수, 문자열 등의 텍스트 데이터)는 기존처럼 사용 가능(MultipartRequest 필요 없음)
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600"/>
<property name="maxUploadSizePerFile" value="104857600"/>
<property name="maxInMemorySize" value="104857600"/>
</bean>
<!--
104857600 byte == 100MB
20971520 byte == 20MB
maxUploadSize
: 한 요청당 업로드가 허용되는 최대 용량을 바이트 단위로 설정.
-1 은 제한이 없다는 뜻으로 이 프로퍼티를 지정하지 않을때 기본값.
maxUploadSizePerFile
: 한 파일당 업로드가 허용되는 최대 용량을 바이트 단위로 설정.
-1 은 제한이 없다는 뜻으로 이 프로퍼티를 지정하지 않을때 기본값.
maxInMemorySize
: 디스크에 저장하지 않고 메모리에 유지하도록
허용하는 바이트 단위의 최대 용량을 설정.
사이즈가 이보다 클 경우 이 사이즈 이상의 데이터는 파일에 저장됩니다.
기본값은 10240 바이트.
-->
--- form 을 참고하자.
-- 파일명 변경해주는 Util 클래스 추가 .
package edu.kh.comm.common;
import java.text.SimpleDateFormat;
public class Util {
// 파일명 변경 메소드
public static String fileRename(String originFileName) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String date = sdf.format(new java.util.Date(System.currentTimeMillis()));
int ranNum = (int) (Math.random() * 100000); // 5자리 랜덤 숫자 생성
String str = "_" + String.format("%05d", ranNum);
String ext = originFileName.substring(originFileName.lastIndexOf("."));
return date + str + ext;
}
// 크로스 사이트 스트립트 공격을 방지 하기 위한 메소드
public static String XSSHandling(String content) {
if (content != null) {
content = content.replaceAll("&", "&");
content = content.replaceAll("<", "<");
content = content.replaceAll(">", ">");
content = content.replaceAll("\"", """);
}
return content;
}
// 크로스 사이트 스트립트 해제
public static String XSSClear(String content) {
if (content != null) {
content = content.replaceAll("&", "&");
content = content.replaceAll("<", "<");
content = content.replaceAll(">", ">");
content = content.replaceAll(""", "\"");
}
return content;
}
// 개행문자 처리
public static String newLineHandling(String content) {
return content.replaceAll("(\r\n|\r|\n|\n\r)", "<br>");
}
// 개행문자 해제
public static String newLineClear(String content) {
return content.replaceAll("<br>", "\n");
}
}
--- MyPageController . 컨트롤러 클래스
---MyPageServiceImpl 클래스, 서비스
--- MyPageDAO . DB접근 클래스
--- myPage-mapper.xml 이미지수정 쿼리문 추가
'Spring > Spring' 카테고리의 다른 글
Spring 15 ( interceptor ) , 게시판 페이지 목록 (0) | 2023.05.01 |
---|---|
Spring 14 ( log4jdbc 설정 ) (0) | 2023.05.01 |
Spring 12-1 ( 암호화비밀번호 조회 + 회원탈퇴 ) (0) | 2023.04.28 |
Spring 12 ( 비밀번호변경 BCryptPasswordEncoder ) (0) | 2023.04.28 |
Spring 11 ( myPage ( 정보수정 ) (0) | 2023.04.28 |
Comments