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 package fr.paris.lutece.util.http;
35
36 import fr.paris.lutece.portal.service.html.EncodingService;
37 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
38 import fr.paris.lutece.portal.web.upload.NormalizeFileItem;
39
40 import org.apache.commons.fileupload.FileItem;
41 import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
42 import org.apache.commons.fileupload.FileUploadException;
43 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
44 import org.apache.commons.fileupload.servlet.ServletFileUpload;
45 import org.apache.commons.lang.StringUtils;
46
47 import java.io.UnsupportedEncodingException;
48
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53
54 import javax.servlet.http.HttpServletRequest;
55
56
57
58
59
60
61
62 public final class MultipartUtil
63 {
64
65
66
67 private MultipartUtil( )
68 {
69 }
70
71
72
73
74
75
76 public static boolean isMultipart( HttpServletRequest request )
77 {
78 return ServletFileUpload.isMultipartContent( request );
79 }
80
81
82
83
84
85
86
87
88
89
90
91 public static MultipartHttpServletRequest convert( int nSizeThreshold, long nRequestSizeMax,
92 boolean bActivateNormalizeFileName, HttpServletRequest request )
93 throws SizeLimitExceededException, FileUploadException
94 {
95 if ( isMultipart( request ) )
96 {
97
98 DiskFileItemFactory factory = new DiskFileItemFactory( );
99
100
101 factory.setSizeThreshold( nSizeThreshold );
102
103
104 ServletFileUpload upload = new ServletFileUpload( factory );
105
106
107 upload.setSizeMax( nRequestSizeMax );
108
109
110 String strEncoding = request.getCharacterEncoding( );
111
112 if ( strEncoding == null )
113 {
114 strEncoding = EncodingService.getEncoding( );
115 }
116
117 Map<String, List<FileItem>> mapFiles = new HashMap<String, List<FileItem>>( );
118 Map<String, String[]> mapParameters = new HashMap<String, String[]>( );
119
120 List<FileItem> listItems = upload.parseRequest( request );
121
122
123 for ( FileItem item : listItems )
124 {
125 if ( item.isFormField( ) )
126 {
127 String strValue = StringUtils.EMPTY;
128
129 try
130 {
131 if ( item.getSize( ) > 0 )
132 {
133 strValue = item.getString( strEncoding );
134 }
135 }
136 catch ( UnsupportedEncodingException ex )
137 {
138 if ( item.getSize( ) > 0 )
139 {
140
141 strValue = item.getString( );
142 }
143 }
144
145
146 String[] curParam = mapParameters.get( item.getFieldName( ) );
147
148 if ( curParam == null )
149 {
150
151 mapParameters.put( item.getFieldName( ), new String[] { strValue } );
152 }
153 else
154 {
155
156 String[] newArray = new String[curParam.length + 1];
157 System.arraycopy( curParam, 0, newArray, 0, curParam.length );
158 newArray[curParam.length] = strValue;
159 mapParameters.put( item.getFieldName( ), newArray );
160 }
161 }
162 else
163 {
164
165
166 FileItem fileItem = bActivateNormalizeFileName ? new NormalizeFileItem( item ) : item;
167 List<FileItem> listFileItem = mapFiles.get( fileItem.getFieldName( ) );
168
169 if ( listFileItem != null )
170 {
171 listFileItem.add( fileItem );
172 }
173 else
174 {
175 listFileItem = new ArrayList<FileItem>( 1 );
176 listFileItem.add( fileItem );
177 mapFiles.put( fileItem.getFieldName( ), listFileItem );
178 }
179 }
180 }
181
182 return new MultipartHttpServletRequest( request, mapFiles, mapParameters );
183 }
184
185 return null;
186 }
187 }