Preloader image
DDD

자바

multi/part upload

작성자 관리자 (admin)
조회수 102
입력일 2021-04-27 01:08:56

-- view

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<pre>
    <form name="fileForm" action="requestupload2" method="post" enctype="multipart/form-data">
        <input multiple="multiple" type="file" name="file" />
        <input type="text" name="src" />
        <input type="submit" value="전송" />
    </form>

</pre>
</body>
</html>

-- Contoroller

    @RequestMapping(value = "requestupload2")
    public String requestupload2(MultipartHttpServletRequest mtfRequest)
    {
        List<MultipartFile> fileList = mtfRequest.getFiles("file");
        String src = mtfRequest.getParameter("src");
        System.out.println("src value : " + src);

        String path = "D:/temp/";
        String originFileName;
        long fileSize;
        String safeFile;

        for (MultipartFile mf : fileList)
        {
            originFileName = mf.getOriginalFilename(); // 원본 파일 명
            fileSize       = mf.getSize()            ; // 파일 사이즈

            System.out.println("originFileName : " + originFileName);
            System.out.println("fileSize       : " + fileSize);

            safeFile = path + System.currentTimeMillis() + originFileName;
            try {
                mf.transferTo(new File(safeFile));
            } catch (IllegalStateException e) {
                logger.info("●●●●●● IllegalStateException : " + e.toString());
            } catch (IOException e) {
                logger.info("●●●●●● IOException : " + e.toString());
            }
        }
        return "redirect:/upload_p.jsp";
    }

^