Preloader image
DDD

자바

Map + File multiple Upload 패턴

작성자 관리자 (admin)
조회수 566
입력일 2020-05-28 15:23:13

-- JSP

    <form id="sin_send_form" name="sin_send_form" method="post" enctype="multipart/form-data" action="/admin/upload_process.do">
    <input type="hidden" name=mode          value="update">

    <table width='98%' align='center' id='list_table' class='table_in_border'>
        <tr>
            <th class="pl">파일  - ${URI}</th>
            <td class="tl">
                <input type="text" name="bbs_name" id="bbs_name" style="width:80%">
                <input type="file" name="file" id="file" style="width:80%" multiple>
            </td>
        </tr>
        <tr>
            <th class="pl">&nbsp; </th>
            <td class="tl">
                <button type=submit class="btn btn-small btn-success" style='width:160px;'> 저장  </button>
            </td>
        </tr>
    </table>
    </form>

-- JAVA

    // Map + File Upload 패턴
    @RequestMapping(value = {"/upload_ajax_process.do", "/admin/upload_process.do"}, method = { RequestMethod.POST, RequestMethod.GET })
    public String upload_process
    (
          ModelMap                             model
        , HttpServletRequest                 request
        , HttpServletResponse               response
        , HttpSession                        session
        , @RequestParam Map  params
        , com_file_array                         cfa
    )
    throws Exception
    {
        logger.debug( "●● upload_process ●●");
        logger.debug( "●● params : " + params.get("mode") );

        // common control
        String URI  = request.getRequestURI();
        boardService.bbs_board_list_ajax(params);

        /*
         *  @ File Upload
         *  @ Param : file
         */
        List<MultipartFile> files     = cfa.getFile();
        List<String>        fileNames = new ArrayList<String>();

        logger.debug("●●-> 오브젝트           : " + files       ); // 파일 오브젝트
        logger.debug("●●-> 업로드 파일 개수 : " + files.size()); // 업로드 파일 개수

        int    pos            =    0; // index of
        String ext            = null; // 업로드 된 파일의 혹장자
        String real_filename  = null; // DB에 실제 들어가는 파일명
        String fileName       = null; // 업로드 된 파일명
        long r_time           =    0; // 
        int  r_count          =    0; // 
        String file_rand_name = null;
        
        // 파일이 존재하는 경우만 적용
        if ( files.size() > 0 )
        {
            String path1 = "C:/upload_real/"; // 메인 업로드 디렉토리
            String path2 = "C:/upload_db/";  // 백업 업로드 디렉토리
            
            // multiple upload - loop
            for (MultipartFile multipartFile : files)
            {
                fileName = multipartFile.getOriginalFilename();
                fileNames.add(fileName);

                pos            = fileName.lastIndexOf( "." )      ; // 순서
                ext            = fileName.substring( pos + 1 )    ; // 확장자
                r_time         = System.currentTimeMillis()       ; // 난수용 날짜
                r_count        = (int)(Math.random()*1000000)     ; // 난수용 번호
                file_rand_name = (String) (r_time + "_" + r_count); // 난수 파일명 생성
                real_filename  = file_rand_name + "." + ext       ; // 사업자번호로 변경된 파일명 생성

                // 파일 공통 테이블 입력 (변동)
                logger.debug("●●-> fileName : " + fileName);
                File uFile = new File( path1 + fileName      ); // 실제 파일 위치

                try
                {
                    logger.debug("●●-> 원래파일 : " + path1 + fileName);
                    multipartFile.transferTo(uFile); // 파일 저장

                    logger.debug("●●-> 복사파일 : " + path2 + real_filename );
                    File tFile = new File( path2 + real_filename );
                    CommonUtil.FileCopy( uFile, tFile ); // source file , target file
                }
                catch (IOException e)
                {
                    logger.debug( "●●-> IOException : " + e );
                }
            }
        }

        logger.debug( "●●-> 원래 파일명 : " + "R" + fileName + "_000001" );

        return "redirect:" + "/admin/upload.do?URI=" + URI;
    }
 

-- com_file_array.java (VO)

package com.erp.vo;

import java.util.List;
import org.springframework.web.multipart.MultipartFile;
import com.erp.vo._common_field;

public class com_file_array
{
    /*
     *  @ com_file_array - FILE
     */
    private int    mna_num      ; // 등록번호
    private int    mno_num_key  ; // 해당번호
    private String mna_type     ; // 해당코드
    private String mna_part     ; // 종류

    private int    mna_position ; // 출력순서
    private String mna_view     ; // 승인유무
    private String mna_file     ; // 파일
    private String mna_file_org ; // 파일제목
 

    private List<MultipartFile> file; // 파일 업로드 - multiple (전체공통)
.
. 생략 ( getter & setter )
.

}

^