Post
KO

멀티파트 이노옴!!!

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

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

package egovframework.com.cmm.web;

/*

 * Copyright 2001-2006 The Apache Software Foundation.

 *

 * Licensed under the Apache License, Version 2.0 (the ”;License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 * http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an ”AS IS”; BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.fileupload.FileItem;

import org.springframework.util.LinkedMultiValueMap;

import org.springframework.util.MultiValueMap;

import org.springframework.util.StringUtils;

import org.springframework.web.multipart.MultipartException;

import org.springframework.web.multipart.MultipartFile;

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

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

import egovframework.com.cmm.EgovWebUtil;

/**

 * 실행환경의 파일업로드 처리를 위한 기능 클래스

 * @author 공통서비스개발팀 이삼섭

 * @since 2009.06.01

 * @version 1.0

 * @see

 *

 * <pre>

 * « 개정이력(Modification Information) »

 *

 *   수정일      수정자           수정내용

 *  ——-    ——–    —————————

 *   2009.3.25  이삼섭          최초 생성

 *   2011.06.11 서준식          스프링 3.0 업그레이드 API변경으로인한 수정

 *

 * </pre>

 */

public class EgovMultipartResolver extends CommonsMultipartResolver {

    public EgovMultipartResolver() {

    }

    /**

     * 첨부파일 처리를 위한 multipart resolver를 생성한다.

     *

     * @param servletContext

     */

    public EgovMultipartResolver(ServletContext servletContext) {

        super(servletContext);

    }

    /**

     * multipart에 대한 parsing을 처리한다.

     */

    @SuppressWarnings(“unchecked”)

    @Override

    protected MultipartParsingResult parseFileItems(List fileItems, String encoding) {

    //스프링 3.0변경으로 수정한 부분

    MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<String, MultipartFile>();

    Map<String, String[]> multipartParameters = new HashMap<String, String[]>();

    // Extract multipart files and multipart parameters.

    for (Iterator it = fileItems.iterator(); it.hasNext();) {

        FileItem fileItem = (FileItem)it.next();

        if (fileItem.isFormField()) {

        String value = null;

        if (encoding != null) {

            try {

            value = fileItem.getString(encoding);

            } catch (UnsupportedEncodingException ex) {

            if (logger.isWarnEnabled()) {

                logger.warn(“Could not decode multipart item ’“ + fileItem.getFieldName() + ”‘ with encoding ’“ + encoding

                    + ”’: using platform default”);

            }

            value = fileItem.getString();

            }

        } else {

            value = fileItem.getString();

        }

        String[] curParam = (String[])multipartParameters.get(fileItem.getFieldName());

        if (curParam == null) {

            // simple form field

            //XSS필터 적용

            value = EgovWebUtil.clearXSSMinimum(value);

            multipartParameters.put(fileItem.getFieldName(), new String[] { value });

        } else {

            // array of simple form fields

            //XSS필터 적용

            value = EgovWebUtil.clearXSSMinimum(value);

            String[] newParam = StringUtils.addStringToArray(curParam, value);

            multipartParameters.put(fileItem.getFieldName(), newParam);

        }

        } else {

        if (fileItem.getSize() > 0) {

            // multipart file field

            CommonsMultipartFile file = new CommonsMultipartFile(fileItem);

            //스프링 3.0 업그레이드 API변경으로인한 수정

            List fileList = new ArrayList();

            fileList.add(file);

            if (multipartFiles.put(fileItem.getFieldName(), fileList) != null) { // CHANGED!!

            throw new MultipartException(“Multiple files for field name [“ + file.getName()

                + ”] found - not supported by MultipartResolver”);

            }

            if (logger.isDebugEnabled()) {

            logger.debug(“Found multipart file [“ + file.getName() + ”] of size ” + file.getSize() + ” bytes with original filename [”

                + file.getOriginalFilename() + ”], stored ” + file.getStorageDescription());

            }

        }

        }

    }

    return new MultipartParsingResult(multipartFiles, multipartParameters);

    }

}

This article is licensed under CC BY 4.0 by the author.