[TIL] 240423

Geehyun(장지현)·2024년 4월 24일

TIL

목록 보기
67/70
post-thumbnail

Today

  • 페이징 처리

    • 쿼리스트링 작성할 때
    StringBuilder sb = new StringBuilder("?page_size=" + this.page_size);
    if(search_type != null) sb.append("&search_type=" + search_type_st + "&search_word=" + this.search_word);
    if(!search_data1.equals("")) sb.append("&search_data1=" + search_data1);
    if(!search_data2.equals("")) sb.append("&search_data2=" + search_data2);
    this.linked_params =  sb.toString();  // 쿼리스트링

    이런식으로 StringBuilder로 만들어서 if문으로 동적으로 처리 후 toString으로 넣어주면 빈값은 안 넣을 수 있음. (ㅈㅅ님! ㄹ님 도움!!!)

  • 검색 조건 처리 추가

  • 파일업로드

    1. 관련 라이브러리 갖고오기
      Apache Commons FileUpload » 1.5
      Apache Commons IO » 2.12.0

    2. 화면간단하게 만들기

      - 단건

      <%@ page contentType="text/html;charset=UTF-8" language="java" %>
      <html>
      <head>
          <meta charset="utf-8">
          <title>Title</title>
      </head>
      <body>
      <form action="/bbs/fileUpload" method="post" enctype="multipart/form-data">
          <div>
              <span>파일업로드</span>
              <input type="file" name="file" id="file">
          </div>
          <div>
              <input type="submit" value="제출">
          </div>
      </form>
      </body>
      </html>

      - 다중

      <%@ page contentType="text/html;charset=UTF-8" language="java" %>
      <html>
      <head>
          <meta charset="utf-8">
          <title>Title</title>
      </head>
      <body>
      <form action="/bbs/fileUpload2" method="post" enctype="multipart/form-data">
          <div>
              <span>파일업로드</span>
              <input type="file" name="files" id="file" multiple>
          </div>
          <div>
              <input type="submit" value="제출">
          </div>
      </form>
      </body>
      </html>

      3.컨트롤러 작성

      - 단건

      @RequestMapping(value="/fileUpload", method= RequestMethod.GET)
          public String fileUploadGET() {
              return "/bbs/fileUpload";
          }
      
          @RequestMapping(value="/fileUpload", method= RequestMethod.POST)
          public String fileUploadPOST(@RequestParam("file") MultipartFile file) {
              String uploadFolder = "D:\\java4\\uploads";
              String fileRealName = file.getOriginalFilename(); //원래 파일의 이름
              long size = file.getSize();
              String fileExt = fileRealName.substring(fileRealName.lastIndexOf("."), fileRealName.length()); // 확장자명
              //엑셀.파.일xxx.xls --> 제일 마지막 인덱스의 . 에서부터 파일이름 끝에를 파싱
      
              log.info("============================");
              log.info("uploadFolder : " + uploadFolder);
              log.info("fileRealName : " + fileRealName);
              log.info("size : " + size);
              log.info("fileExt : " + fileExt);
      
              //새로운 파일명 생성
              UUID uuid = UUID.randomUUID();
              String[] uuids = uuid.toString().split("-");
              String newName = uuids[0];
      
              log.info("uuid : " + uuid);
              log.info("uuids : " + uuids);
              log.info("newName : " + newName);
      
              File saveFile = new File(uploadFolder + "\\" + newName + fileExt);
      
              try {
                  file.transferTo(saveFile);
              } catch (IllegalStateException e) {
                  e.printStackTrace();
              }
              catch(Exception e) {
                  e.printStackTrace();
              }
      
              log.info("============================");
      
              return "/bbs/fileUpload";
          }

      - 다중

      @RequestMapping(value="/fileUpload2", method= RequestMethod.GET)
        public String fileUpload2GET() {
            return "/bbs/fileUpload2";
        }
        @RequestMapping(value="/fileUpload2", method= RequestMethod.POST)
        public String fileUpload2POST(MultipartHttpServletRequest files) {
            String uploadFolder = "D:\\java4\\uploads";
      
            List<MultipartFile> list = files.getFiles("files");
            for(MultipartFile file : list) {
                String fileRealName = file.getOriginalFilename(); //원래 파일의 이름
                long size = file.getSize();
                String fileExt = fileRealName.substring(fileRealName.lastIndexOf("."), fileRealName.length()); // 확장자명
                //엑셀.파.일xxx.xls --> 제일 마지막 인덱스의 . 에서부터 파일이름 끝에를 파싱
      
                log.info("============================");
                log.info("uploadFolder : " + uploadFolder);
                log.info("fileRealName : " + fileRealName);
                log.info("size : " + size);
                log.info("fileExt : " + fileExt);
      
                //새로운 파일명 생성
                UUID uuid = UUID.randomUUID();
                String[] uuids = uuid.toString().split("-");
                String newName = uuids[0];
      
                log.info("uuid : " + uuid);
                log.info("uuids : " + uuids);
                log.info("newName : " + newName);
      
                File saveFile = new File(uploadFolder + "\\" + newName + fileExt);
      
                try {
                    file.transferTo(saveFile);
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
      
                log.info("============================");
            }
            return "/bbs/fileUpload2";
        }

Review

  • 리스트에서 검색 조건 + 페이징 처리를 위해 DTO를 따로 생성해서 그 안에 넣어서 다 갖고오는 식으로 작업함. DTO에서 페이징 처리를 위한 모든 계산을 하니, View페이지에서는 그냥 꺼내서 보여주기만 해도 되서 편리함
  • 정치기 실기 D-4
  • 내일부터 팀 프로젝트 진행 예정!
profile
블로그 이전 했습니다. 아래 블로그 아이콘(🏠) 눌러서 놀러오세요

0개의 댓글