1. library 추가

- version 1.1 이상 필요

- commons-fileupload.jar

- commons-io.jar


2. Bean 주입 ( applicationContext.xml )

-[bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /]

- 이부분에 대한 추가적인 설정이나 설명은 각자 찾아보길 바란다.


3. Source part

// 파일 첨부를 위해 추가 import 구문

import org.springframework.util.FileCopyUtils;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.multipart.MultipartHttpServletRequest;

import org.springframework.web.multipart.commons.CommonsMultipartResolver;


public class ActionClassName extends MappingDispatchActionSupport {


       private CommonsMultipartResolver cResolver;

       // Multipartrequest를 컨트롤하는 객체를 주입받는다.

       cResolver = (CommonsMultipartResolver) getWebApplicationContext().getBean("multipartResolver");    

       

       public ActionForward adminGoodStoreSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {


        MultipartHttpServletRequest mrequest = cResolver.resolveMultipart(request);

        String realPath      = request.getSession().getServletContext().getRealPath("/");    // 실제 서버의 주소는 이런식으로 가져올 수도 있다.

        String uploadPath    = realPath + customPath;


          // Multipartrequest 에서 파일 객체부분을 가저온다.

        Map fileMap = mrequest.getFileMap();


        for (Iterator it=fileMap.entrySet().iterator(); it.hasNext();) {

            Entry entry      = (Entry)it.next();

            MultipartFile mf = (MultipartFile)entry.getValue();


            if (!mf.isEmpty()) {

                if(mf.getSize() > 0){

       

                    // 원본 파일명

                    String originalFilename = mf.getOriginalFilename();

                    // 새로운 파일명은 시간(밀리세컨드까지)으로 변경하고 

                    // 확장자만을 갖는다.

                    String newFilename =  System.currentTimeMillis() + originalFilename.substring(originalFilename.length() -4, originalFilename.length());;


                    File fOri = new File(uploadPath, newFilename);


                    // 실제 파일을 작성한다.

                    InputStream is = mf.getInputStream();

                    OutputStream os = new FileOutputStream(fOri);

                    FileCopyUtils.copy(is, os);


                    /* 

                     * 여기에서 파일 필드를 구분하여 처리를 한다.

                     * mf.getName() 또는 entry.getKey().toString() 으로 

                     * input Tag의 name을 확인할 수 있다.

                     */

                    if"file1".equals(mf.getName()) ) {

                            /* 파일 필드1에 대한 처리 구역 */

                    } else if"file2".equals(mf.getName()) ) {

                            /* 파일 필드2에 대한 처리 구역 */

                    } else if"file3".equals(mf.getName()) ) {

                            /* 파일 필드3에 대한 처리 구역 */

                    }


                    os.close();

                    is.close();


                }

            }

        }

        return mapping.findForward(Constants.SUCCESS_KEY);

    }

       

}

       

       

4. JSP Tag part

<html>

<head>

</head>

<body>

....

<tr>

<td align="center" width="100">첨부1</td>

<td colspan="3"><input type="file" name="file1" id="file1"></td>

</tr>

<tr>

<td align="center" width="100">첨부2</td>

<td colspan="3"><input type="file" name="file2" id="file2"></td>

</tr>

<tr>

<td class="rowtitle" align="center" width="100">첨부3</td>

<td colspan="3"><input type="file" name="file3" id="file3"></td>

</tr>

...

</body>

</html>


+ Recent posts