CAFE

이동쀀

[λ©”λͺ¨] πŸ“Œ 파일 μ—…λ‘œλ“œ μ°Έκ³ 

μž‘μ„±μžμ΄λ™μ€€|μž‘μ„±μ‹œκ°„25.09.29|쑰회수20 λͺ©λ‘ λŒ“κΈ€ 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    /* μƒν’ˆλ“±λ‘ */
    // 1. μ—…λ‘œλ“œ κ²½λ‘œ ν™•보, 2. MultipartFile μ €μž₯(UUID + ν™•μž₯자), 3. vo에 μ΄λ―Έμ§€ κ²½λ‘œ μ„ΈνŒ…, 4. DAO ν˜ΈμΆœ(DB insert) 
    @Override
    public int createProduct(ProductVO vo, List<MultipartFile> files, HttpSession session) {
        // μƒν’ˆ κΈ°λ³Έ μ •보 Insert
        int res = productDAO.insertProduct(vo);
        if(res == 0return 0;
        
        UserVO loginUser = (UserVO) session.getAttribute("sLoginUser");
        // μ—…λ‘œλ“œ κ²½λ‘œ
        String realPath = session.getServletContext().getRealPath("/resources/data/product");
        // λ§Œμ•½ ν•΄λ‹Ή κ²½λ‘œκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ μƒˆλ‘œ μƒμ„±ν•œλ‹€.
        // - dir.exists() : ν•΄λ‹Ή κ²½λ‘œ(폴더/파일)κ°€ μ‘΄μž¬ν•˜λŠ”μ§€ ν™•인
        // - mkdirs()            : ν΄λ”κ°€ μ—†μ„ κ²½μš°, μ€‘κ°„ κ²½λ‘œκΉŒμ§€ μ „λΆ€ μƒμ„±
        File dir = new File(realPath);
        // dir κ²½λ‘œλ₯Ό ν™•μΈν•˜κ³  μ—†μœΌλ©΄ μ€‘κ°„κ²½λ‘œκΉŒμ§€ μƒμ„±
        if(!dir.exists()) dir.mkdirs();
        for(MultipartFile file : files) {
            if(file == null ||  file.isEmpty()) continue;
            // MultipartFile file : ν΄λΌμ΄μ–ΈνŠΈμ—μ„œ μ—…λ‘œλ“œ λœ νŒŒμΌμ„ λ‹΄κ³  μžˆλŠ” κ°μ²΄
            // getOriginalFilename: ν΄λΌμ΄μ–ΈνŠΈμ—μ„œ μ—…λ‘œλ“œν•œ μ›λ³Έ νŒŒμΌλͺ…을 κ°€μ§€κ³  μ˜΄
            String oFileName = file.getOriginalFilename();
            
            // νŒŒμΌμ΄ null이 μ•„λ‹ˆκ³  λΉ„μ–΄μžˆμ§€ μ•Šμ„ λ•Œλ§Œ μ²˜λ¦¬
            if(oFileName != null && !oFileName.isEmpty()) {
                // ν™•μž₯자 μΆ”μΆœ : .jsp, .png λ“± νŒŒμΌλͺ…은 λ°”λ€Œμ–΄λ„ ν™•μž₯μžλŠ” λ™μΌν•˜κ²Œ μ‚¬μš©ν•΄μ•Ό ν•˜κΈ° λ•Œλ¬Έ
                String sFileName = oFileName.substring(oFileName.lastIndexOf("."));
                // UUID + ν™•μž₯자 μ‘°ν•©μœΌλ‘œ μƒˆ νŒŒμΌλͺ… μƒμ„± β†’ μ€‘볡 λ°©μ§€
                // λ‘œκ·ΈμΈλœ μ‚¬μš©μžμ˜ idxλ₯Ό μ‘°ν•©ν•˜μ—¬ λˆ„κ°€ μ˜¬λ ΈλŠ”μ§€ μΆ”적 / μ™ΈλΆ€ λ…ΈμΆœ μ‹œ μ œ 3μžλŠ” idx의 μ˜λ―Έ λͺ¨λ¦„.
                String nFileName = UUID.randomUUID().toString() + "_" + loginUser.getIdx() + sFileName;
                
                // μ—…λ‘œλ“œλœ νŒŒμΌμ„ μ„œλ²„에 μ €μž₯ν•˜λŠ” λ‹¨κ³„
                // 1. νŒŒμΌ κ°μ²΄ μƒμ„±
                // - File ν΄λž˜μŠ€λŠ” μ‹€μ œ λ””μŠ€ν¬(μ„œλ²„)에 μ‘΄μž¬ν•  '파일'을 κ°€λ₯΄ν‚€λŠ” κ°μ²΄
                // - μ—¬κΈ°μ„œλŠ” realPath(폴더 κ²½λ‘œ) + nFileName(파일 μ΄λ¦„) ν•©μ³μ„œ κ²½λ‘œλ₯Ό μ§€μ •
                // - μ΄ μ‹œμ μ—λŠ” νŒŒμΌμ΄ "μƒμ„±λœ κ²ƒμ²˜λŸΌ λ³΄μ΄λŠ” κ°μ²΄"일 λΏ, μ‹€μ œλ‘œ λ””μŠ€ν¬μ— μ“°μ΄μ§€λŠ” μ•ŠμŒ
                File dest = new File(realPath, nFileName);
    
                // 2. μ—…λ‘œλ“œλœ νŒŒμΌ λ°μ΄ν„°λ₯Ό μ‹€μ œ μ„œλ²„에 μ €μž₯
                // - transferTo() : MultipartFile μΈν„°νŽ˜μ΄μŠ€μ— μ •μ˜λœ λ©”μ„œλ“œ
                // - μ—…λ‘œλ“œλœ νŒŒμΌμ˜ λ‚΄μš©μ„ μœ„에 μ§€μ •ν•œ file κ°μ²΄(dest) μœ„μΉ˜μ— μ €μž₯ν•œλ‹€.
                // λ™μž‘방식 : 
                //    (1) μ‚¬μš©μžκ°€ μ—…λ‘œλ“œν•œ νŒŒμΌμ€ μ„œλ²„μ˜ μž„μ‹œ μ €μž₯μ†Œ(temp)에 λ³΄κ΄€λ¨
                //    (2) transferTo()λ₯Ό ν˜ΈμΆœν•˜λ©΄ temp에 μžˆλŠ” νŒŒμΌ λ°μ΄ν„°λ₯Ό dest κ²½λ‘œλ‘œ λ³΅μ‚¬/이동
                //    (3) λ³΅μ‚¬ ν›„ dest κ²½λ‘œμ— μ‹€μ œ νŒŒμΌμ΄ μƒμ„±λ¨
                try {
                    file.transferTo(dest);
                } catch (IOException | IllegalStateException e) {
                    e.printStackTrace();
                    return 0;
                } // MultipartFile μ œκ³΅ν•΄μ£ΌλŠ” transferTo()λ©”μ„œλ“œλ‘œ In/OutputStream을 μ‚¬μš©ν•˜μ§€ μ•Šκ³  μ§€μ •ν•œ κ²½λ‘œμ— νŒŒμΌμ„ μ €μž₯
            
                ProductImageVO imageVO = new ProductImageVO();
                imageVO.setProductId(vo.getProductId());
                imageVO.setOriginalName(oFileName);
                imageVO.setStoredName(nFileName);
                imageVO.setSize(file.getSize());
                
                productDAO.insertProductImages(imageVO);
            }
        }
        return res;
    }
cs

Β 

λ‹€μŒκ²€μƒ‰
ν˜„μž¬ κ²Œμ‹œκΈ€ μΆ”κ°€ κΈ°λŠ₯ μ—΄κΈ°

λŒ“κΈ€

λŒ“κΈ€ 리슀트
λ§¨μœ„λ‘œ

카페 검색

카페 검색어 μž…λ ₯폼