2022.02.07 TIL

서승원·2022년 2월 7일
0

TIL

목록 보기
63/68

https://github.com/crimy/gocom/
jsp에서 session을 이용한 로그인 기능 수정
File 업로드 시스템 구현중.
업로드까지 완료했고 파일이 temp2 폴더에 생성되는것 확인
게시판에 출력, 글 작성시 미리보기 구현중

오늘 한 실수

1
2
3
4
5
6
7
8
9
10
11
<%@ page session="true" %>
 
<c:choose>    
    <c:when test="${sessionScope.name == null}"><a href="/gocom/member/login">로그인</a>
    <a href="/gocom/member/signin">회원가입</a>    
    </c:when>
    
    <c:otherwise>${sessionScope.name}님 환영합니다
    <a href="/gocom/member/logout">로그아웃</a>
    </c:otherwise>
</c:choose>    
cs

jsp 파일에서 session을 통해 파라미터에 접근하기 위해서는 page session attirbute를 true로 바꿔줘야 한다. session을 통해 로그인 정보를 게시판 INSERT QUERY문에서 가져올 수 있는 것은 확인 됐지만 모든 JSP 파일 상단에 import 될 메뉴바에서 세션에 대한 정보로 로그인 여부를 확인해 메뉴 바의 항목을 달리 하는 것에 실패했다. sessionScope와 EL문을 이용해서 접근해보려 했지만 여전히 잘 되지 않았고, page session 이라는 항목을 발견했다. 해당 페이지에서 session을 사용할 지에 대한 여부를 정해주는 속성으로 true로 바꿔주었고, 정상적으로 출력이 됐다.

1
2
3
4
5
6
7
8
9
10
11
    <dependency>
        <groupId>commons-fileupload</groupId>
           <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
    </dependency>
    
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.4</version>
    </dependency>
cs
1
2
3
4
5
       <beans:bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <beans:property name="maxUploadSize" value="52428800" />
        <beans:property name="maxInMemorySize" value="1000000" />
        <beans:property name="defaultEncoding" value="utf-8" />
    </beans:bean>
cs

이미지 업로드를 위한 두 dependency를 pom.xml에 추가했다. 그리고 servlet-context.xml에 해당 bean을 추가해 MulitpartFile에 대한 접근을 스프링에서 관리하게 해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    public static String createFsn(MultipartFile file) throws Exception {
        String ofn = file.getOriginalFilename();
        UUID uuid = UUID.randomUUID();
        String fsn = uuid.toString();
        File saveFile = new File( path() , fsn + "_" + ofn );
        try {
            if(file.getSize() != 0) {
            if(!saveFile.exists()) {
                if(saveFile.getParentFile().mkdir()) {
                    saveFile.createNewFile();
                }
            }
            file.transferTo(new File( path() , fsn + "_" + ofn ));
            }
        } catch( Exception e ) {
            e.printStackTrace();
        }
        
 
        return uuid.toString();        
    }
}
 
cs

이를 이용해서 file의 경로를 지정하고 ,중복을 막기위한 UUID로 생성한 fsn의 String에 ofn을 덧붙여 파일 형식 문제를 해결하고, createNewFile로 임시 파일을 만든 후 transferTo로 MultipartFile을 이용해 받아온 업로드한 파일을 임시 파일에 덮어쓰는 식으로 구현했다.

한번에 많은 클래스의 파일들을 관리하다보니 오타나 변수가 바뀌는 실수들이 꽤 있었고, 그것들을 바로잡는데 시간을 꽤 소모했다.
실수를 줄이고 설계를 먼저 끝낸 뒤 어떻게 짤지 고민해보자.

profile
2년차 백엔드 개발자, crimy

0개의 댓글