001 /*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016 package org.opengion.hayabusa.servlet;
017
018 import org.opengion.hayabusa.common.HybsSystem;
019 import org.opengion.hayabusa.servlet.multipart.MultipartParser;
020 import org.opengion.hayabusa.servlet.multipart.Part;
021 import org.opengion.hayabusa.servlet.multipart.FilePart;
022 import org.opengion.hayabusa.servlet.multipart.ParamPart;
023 // import org.opengion.fukurou.util.ZipArchive; // 5.7.1.2 (2013/12/20) zip 対�
024
025 import java.io.File;
026 import java.io.IOException;
027 import java.util.Map;
028 // import java.util.HashMap;
029 import java.util.TreeMap;
030 import java.util.List;
031 import java.util.ArrayList;
032 import java.util.Set;
033 import java.util.Random ;
034 import java.util.concurrent.atomic.AtomicInteger; // 5.5.2.6 (2012/05/25) findbugs対�
035 import javax.servlet.http.HttpServletRequest;
036
037 /**
038 * ファイルをサーãƒã?ã«ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã™ã‚‹å?åˆã«ä½¿ç”¨ã•れるã?ルãƒãƒ‘ートå?ç?‚µãƒ¼ãƒ–レãƒ?ƒˆã§ã™ã?
039 *
040 * 通常ã®ãƒ•ァイルアãƒ??ãƒãƒ¼ãƒ‰æ™‚ã®ã€form ã§ä½¿ç”¨ã™ã‚‹ã€enctype="multipart/form-data"
041 * を指定ã—ãŸå?åˆã?ã€ä»–ã?ãƒªã‚¯ã‚¨ã‚¹ãƒˆæƒ…å ±ã‚‚ã?å–り出ã™ã“ã¨ãŒå¯èƒ½ã§ã™ã?
042 *
043 * ファイルをアãƒ??ãƒãƒ¼ãƒ‰å¾Œã«ã€æŒ‡å®šã?ファイルåã«å¤‰æ›´ã™ã‚‹æ©Ÿè?ãŒã‚りã¾ã™ã?
044 * file 登録ãƒ?‚¤ã‚¢ãƒã‚°ã§æŒ?®šã—ã?name ã«ã€?_NEW" ã¨ã?†å称を付ã‘ãŸãƒªã‚¯ã‚¨ã‚¹ãƒˆå?ã‚?
045 * ファイルã®ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã¨åŒæ™‚ã«é€ä¿¡ã™ã‚‹ã“ã¨ã§ã€ã“ã®åå‰ã«ãƒ•ã‚¡ã‚¤ãƒ«ã‚’ä»˜ã‘æ›¿ãˆã¾ã™ã?
046 * ã¾ãŸã?ã‚¢ãƒ??ãƒãƒ¼ãƒ‰å¾Œã?ファイルåã?ã€name æŒ?®šã?åç§°ã§ã€å–りå?ã›ã¾ã™ã?
047 * クライアントã‹ã‚‰ç™»éŒ²ã—ãŸã‚ªãƒªã‚¸ãƒŠãƒ«ã®ãƒ•ァイルåã?ã€name ã«ã€?_ORG" ã¨ã?†åç§°
048 * ã§å–り出ã™ã“ã¨ãŒå¯èƒ½ã§ã™ã?
049 *
050 * maxPostSize : æœ?¤§è»¢é€ã‚µã‚¤ã‚º(Byte)を指定ã—ã¾ã™ã? 0,ã¾ãŸã?マイナスã§ç„¡åˆ¶é™ã§ã™ã?
051 * useBackup : ファイルアãƒ??ãƒãƒ¼ãƒ‰æ™‚ã«ã€ã™ã§ã«åŒåã®ãƒ•ァイルãŒå˜åœ¨ã—ãŸå ´åˆã«ã€?
052 * ãƒãƒƒã‚¯ã‚¢ãƒ??処ç?renameTo)ã™ã‚‹ã‹ã©ã?‹[true/false]を指定ã—ã¾ã?åˆæœŸå€¤:false)
053 *
054 * ファイルアãƒ??ãƒãƒ¼ãƒ‰æ™‚ã«ã€ã‚¢ãƒ??ãƒãƒ¼ãƒ‰å?ã«ã€åŒåã?ファイルãŒå˜åœ¨ã—ãŸå ´åˆã?ã€æ—¢å˜æ©Ÿè?ã¯ã€ãã®ã¾ã¾
055 * ç½®ãæ›ãˆã¦ã?¾ã—ãŸãŒã?簡易ãƒãƒ¼ã‚¸ãƒ§ãƒ³ã‚¢ãƒ??機è?ã¨ã—ã¦ã€useBackup="true" を指定ã™ã‚‹ã¨ã€æ—¢å˜ã?ファイルã‚?
056 * リãƒã?ãƒ?—ã¦ã€ãƒãƒ?‚¯ã‚¢ãƒ??ファイルを作æ?ã—ã¾ã™ã?
057 * ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルã¯ã€ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ãƒ•ォルãƒ?‚’基準ã¨ã—ã¦ã€_backup/ファイルå?æ‹¡å¼µå_処ç?™‚刻ã®long値.æ‹¡å¼µå?ã«ãªã‚Šã¾ã™ã?
058 * オリジナルã®ãƒ•ァイルå(拡張å付)を残ã—ãŸã¾ã¾ã€?_処ç?™‚刻ã®long値" を追åŠ?—ã€ã•らã«ã€ã‚ªãƒªã‚¸ãƒŠãƒ«ã®æ‹¡å¼µåを追åŠ?—ã¾ã™ã?
059 * ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルã®å½¢å¼ã?æŒ?®šã§ãã¾ã›ã‚“ã€?
060 *
061 * 5.7.1.2 (2013/12/20) zip 対�
062 * filename 属æ?ã«ã€?.zip" ã®æ‹¡å¼µåã?ファイルåã‚’æŒ?®šã—ãŸå?åˆã?ã€ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã•れãŸä¸??ã®ãƒ•ァイルã‚?
063 * ZIP圧縮ã—ã¾ã™ã?ã“れã¯ã€ã‚¢ãƒ??ãƒãƒ¼ãƒ‰å¾Œã?処ç?«ãªã‚Šã¾ã™ã?
064 * ZIP圧縮ã®ã‚ªãƒªã‚¸ãƒŠãƒ«ãƒ•ァイルã¯ã€ãã®ã¾ã¾æ®‹ã‚Šã¾ã™ã?
065 * ãªãŠã?ZIPファイルã¯ã€useBackup属æ?ã‚?true ã«è¨å®šã—ã¦ã‚‚ã?無関係ã«ã€ä¸Šæ›¸ãã•れã¾ã™ã?
066 *
067 * @og.group ãã?他機è?
068 *
069 * @version 4.0
070 * @author Kazuhiko Hasegawa
071 * @since JDK5.0,
072 */
073 public final class MultipartRequest {
074 // private static volatile int dumyNewFileCnt = 1 ; // 3.8.1.4 (2006/03/17)
075 private static AtomicInteger dumyNewFileCnt = new AtomicInteger(1); // 5.5.2.6 (2012/05/25) findbugs対�
076
077 private static String RANDOM_KEY = new Random().nextInt( Integer.MAX_VALUE ) + "_" ; // 5.6.5.3 (2013/06/28) ã‚¢ãƒ??ãƒãƒ¼ãƒ‰æ™‚ã®ãƒ?ƒŸãƒ¼ãƒ•ァイルåã‚’ã‚‚ã†å°‘ã—ã?‘ランãƒ?ƒ ã«ã™ã‚‹ã€?
078
079 // private final Map<String,List<String>> parameters = new HashMap<String,List<String>>();
080 // private final Map<String,UploadedFile> files = new HashMap<String,UploadedFile>();
081 private final Map<String,List<String>> parameters = new TreeMap<String,List<String>>(); // 5.6.5.2 (2013/06/21) ソートã—ã¾ã™ã?
082
083 // 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
084 // private final Map<String,UploadedFile> files = new TreeMap<String,UploadedFile>(); // 5.6.5.2 (2013/06/21) ソートã—ã¾ã™ã?
085 private final List<UploadedFile> files = new ArrayList<UploadedFile>(); // 5.7.1.1 (2013/12/13) HTML5対�
086
087 /**
088 * MultipartRequest オブジェクトを構築ã—ã¾ã™ã?
089 *
090 * 引数ã¨ã—ã¦ã€ãƒ•ァイルアãƒ??ãƒãƒ¼ãƒ‰æ™‚ã®ä¿å˜ãƒ•ォルãƒ??æœ?¤§ã‚µã‚¤ã‚ºã€ã‚¨ãƒ³ã‚³ãƒ¼ãƒ‰ã?
091 * æ–°ã—ã„ファイルåãªã©ã‚’指定ã§ãã¾ã™ã?æ–°ã—ã„ファイルåã?ã€ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã•れる
092 * ファイルãŒä¸?¤ã?‘ã®å ´åˆã«ä½¿ç”¨ã§ãã¾ã™ã?è¤?•°ã®ãƒ•ã‚¡ã‚¤ãƒ«ã‚’åŒæ™‚ã«å¤‰æ›´ã—ãŸã?
093 * å ´åˆã?ã€ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ãƒ«ãƒ¼ãƒ«ã«ã®ã£ã¨ã‚Šã?ãƒªã‚¯ã‚¨ã‚¹ãƒˆãƒ‘ãƒ©ãƒ¡ãƒ¼ã‚¿ã§æŒ?®šã—ã¦ãã ã•ã„ã€?
094 *
095 * HTML5 ã§ã¯ã€ãƒ•ァイルアãƒ??ãƒãƒ¼ãƒ‰æ™‚ã«ã€multiple 属æ???nputã‚¿ã‚°ã®type="file"?‰ã‚’
096 * 付ã‘る事ã§ã€ãƒ•ァイルをè¤?•°é¸æŠžã§ãã¾ã™ã?
097 * ãã?å ´åˆã?ã€inputã®name属æ?ã¯ã€ä¸?¤ãªã®ã§ã€_NEW ã«ã‚ˆã‚‹åå‰ã®æ›¸ãæ›ãˆã?ã§ãã¾ã›ã‚“ã€?
098 *
099 * @og.rev 3.8.1.3A (2006/01/30) 新ファイルåã«ã‚ªãƒªã‚¸ãƒŠãƒ«ãƒ•ァイルåã?æ‹¡å¼µåã‚’ã‚»ãƒ?ƒˆã—ã¾ã?
100 * @og.rev 4.0.0.0 (2007/11/28) メソãƒ?ƒ‰ã®æˆ»ã‚Šå?ã‚’ãƒã‚§ãƒ?‚¯ã—ã¾ã™ã?
101 * @og.rev 5.5.2.6 (2012/05/25) findbugs対応ã?staticフィールドã¸ã®æ›¸ãè¾¼ã¿ã«ã€AtomicInteger を利用ã—ã¾ã™ã?
102 * @og.rev 5.6.5.3 (2013/06/28) useBackup引数追�
103 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
104 * @og.rev 5.7.1.2 (2013/12/20) zip 対�
105 *
106 * @param request HttpServletRequestオブジェク�
107 * @param saveDirectory ファイルアãƒ??ãƒãƒ¼ãƒ‰ãŒã‚ã£ãŸå?åˆã?ä¿å˜ãƒ•ォルãƒ?
108 * @param maxPostSize ファイルアãƒ??ãƒãƒ¼ãƒ‰æ™‚ã®æœ?¤§ãƒ•ァイルサイズ(Byte)0,ã¾ãŸã?マイナスã§ç„¡åˆ¶é™?
109 * @param encoding ファイルã®ã‚¨ãƒ³ã‚³ãƒ¼ãƒ?
110 * @param inputFilename ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã•れãŸãƒ•ã‚¡ã‚¤ãƒ«ã®æ–°ã—ã„åå‰
111 * @param useBackup ファイルアãƒ??ãƒãƒ¼ãƒ‰æ™‚ã«ã€ãƒãƒ?‚¯ã‚¢ãƒ??処ç?™ã‚‹ã‹ã©ã?‹[true/false]を指å®?
112 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
113 */
114 public MultipartRequest(final HttpServletRequest request,
115 final String saveDirectory,
116 final int maxPostSize,
117 final String encoding,
118 // final String inputFilename ) throws IOException {
119 final String inputFilename,
120 final boolean useBackup ) throws IOException { // 5.6.5.3 (2013/06/28) 追�
121
122 if(request == null) {
123 throw new IllegalArgumentException("request cannot be null");
124 }
125 if(saveDirectory == null) {
126 throw new IllegalArgumentException("saveDirectory cannot be null");
127 }
128 // 5.5.2.6 (2012/05/25) 0,ã¾ãŸã?マイナスã§ç„¡åˆ¶é™?
129 // if(maxPostSize <= 0) {
130 // throw new IllegalArgumentException("maxPostSize must be positive");
131 // }
132
133 // Save the dir
134 File dir = new File(saveDirectory);
135
136 // Check saveDirectory is truly a directory
137 if(!dir.isDirectory()) {
138 throw new IllegalArgumentException("Not a directory: " + saveDirectory);
139 }
140
141 // Check saveDirectory is writable
142 if(!dir.canWrite()) {
143 throw new IllegalArgumentException("Not writable: " + saveDirectory);
144 }
145
146 // Parse the incoming multipart, storing files in the dir provided,
147 // and populate the meta objects which describe what we found
148 MultipartParser parser = new MultipartParser(request, maxPostSize);
149 if(encoding != null) {
150 parser.setEncoding(encoding);
151 }
152
153 // 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
154 // List<String> list = new ArrayList<String>();
155
156 Part part;
157 while ((part = parser.readNextPart()) != null) {
158 String name = part.getName();
159 if( part.isParam() && part instanceof ParamPart ) {
160 ParamPart paramPart = (ParamPart)part;
161 String value = paramPart.getStringValue();
162 List<String> existingValues = parameters.get(name);
163 if(existingValues == null) {
164 existingValues = new ArrayList<String>();
165 parameters.put(name, existingValues);
166 }
167 existingValues.add(value);
168 }
169 else if( part.isFile() && part instanceof FilePart ) {
170 FilePart filePart = (FilePart)part;
171 // String fileName = filePart.getFilename();
172 String orgName = filePart.getFilename(); // 5.7.1.1 (2013/12/13) 判りやã™ã„よã†ã«å¤‰æ•°å変更
173 // if(fileName != null) {
174 if(orgName != null) {
175 // 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
176 // åŒä¸?name ã§ã€è¤?•°ãƒ•ァイルを扱ã?¿?¦ãŒã‚りã¾ã™ã?
177 // list.add( name ); // 3.5.6.5 (2004/08/09) æŒ?®šã? name 属æ?
178 // 3.8.1.2 (2005/12/19) 仮ファイルã§ã‚»ãƒ¼ãƒ–ã™ã‚‹ã?
179 // String newName = String.valueOf( dumyNewFileCnt++ ) ; // 3.8.1.4 (2006/03/17)
180 // String newName = String.valueOf( dumyNewFileCnt.getAndIncrement() ) ; // 5.5.2.6 (2012/05/25) findbugs対�
181 String uniqKey = RANDOM_KEY + dumyNewFileCnt.getAndIncrement() ; // 5.6.5.3 (2013/06/28) ã‚¢ãƒ??ãƒãƒ¼ãƒ‰æ™‚ã®ãƒ?ƒŸãƒ¼ãƒ•ァイルåã‚’ã‚‚ã†å°‘ã—ã?‘ランãƒ?ƒ ã«ã™ã‚‹ã€?
182 // filePart.setFilename( newName ); // 5.6.5.3 (2013/06/28) newName ã¯ã‚?‚„ã“ã—ã??ã§ã€å¤‰æ›´
183 filePart.setFilename( uniqKey );
184 filePart.writeTo(dir);
185
186 // 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
187 files.add( new UploadedFile(
188 uniqKey, // 5.7.1.1 (2013/12/13) é ?•ªå¤‰æ›´
189 dir.toString(),
190 name, // 5.7.1.1 (2013/12/13) é ?›®è¿½åŠ?
191 // fileName,
192 orgName,
193 filePart.getContentType()));
194
195 // files.put(name,
196 // new UploadedFile( dir.toString(),
197 //// newName, // 3.8.1.2 (2005/12/19)
198 // tempName, // 3.8.1.2 (2005/12/19)
199 // fileName,
200 // filePart.getContentType()));
201 }
202 // else {
203 // files.put(name, new UploadedFile(null, null, null, null));
204 // }
205 }
206 else {
207 String errMsg = "PartオブジェクトãŒã€ParamPartã§ã‚?ilePartã§ã‚‚ã‚りã¾ã›ã‚“ã€?
208 + " class=[" + part.getClass() + "]";
209 throw new RuntimeException( errMsg );
210 }
211 }
212
213 // 3.5.6.5 (2004/08/09) 登録後ã«ãƒ•ァイルをリãƒã?ãƒ?—ã¾ã™ã?
214 // 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
215 // int size = list.size();
216 int size = files.size();
217
218 // 5.7.1.2 (2013/12/20) zip 対�
219 File[] tgtFiles = new File[size];
220 boolean isZip = ( inputFilename != null && inputFilename.endsWith( ".zip" ) );
221
222 for( int i=0; i<size; i++ ) {
223 // String name = list.get(i);
224 // File file = getFile( name );
225 UploadedFile upFile = files.get(i);
226 String name = upFile.getName(); // 5.7.1.1 (2013/12/13)
227
228 String newName = (isZip) ? null : inputFilename ;
229 if( newName == null && name != null ) {
230 int adrs = name.lastIndexOf( HybsSystem.JOINT_STRING ); // カラãƒ?_è¡Œç•ªå· ã® __ ã®ä½ç½®
231 if( adrs < 0 ) {
232 newName = getParameter( name + "_NEW" );
233 }
234 else {
235 newName = getParameter( name.substring( 0,adrs ) + "_NEW" + name.substring( adrs ) );
236 }
237 }
238
239 // 5.7.1.1 (2013/12/13) UploadedFile å†?§å‡¦ç?™ã‚‹ã‚ˆã?«å¤‰æ›´
240 tgtFiles[i] = upFile.renameTo( newName,useBackup );
241
242 // // 3.8.1.3 (2006/02/06) 新ファイルåã«æ‹¡å¼µåãŒãªã?¨ã?
243 // // 旧ファイルåã‹ã‚‰æ‹¡å¼µåå–å¾—ã—æ–°ãƒ•ァイルåã«æ–?—å?連çµ?
244 // if( newName != null && newName.length() > 0 ) {
245 // // 新ファイルåã‹ã‚‰æ‹¡å¼µåå–å¾?
246 // String newExt = getExtension( newName );
247 // if( newExt == null || newExt.length() == 0 ) {
248 //// String oldExt = getExtension( getOriginalFileName( name ) ); // 5.7.1.1 (2013/12/13)
249 // String oldExt = getExtension( upFile.getOriginalFileName() );
250 //// newName = new StringBuilder().append( newName ).append( "." ).append( oldExt ).toString();
251 // newName = newName + "." + oldExt ;
252 // }
253 // }
254 // else {
255 //// newName = getOriginalFileName( name ); // 5.7.1.1 (2013/12/13)
256 // newName = upFile.getOriginalFileName();
257 // }
258 //
259 // // 3.8.1.2 (2005/12/19) 基本çš?«ã¯ã™ã¹ã¦ã®ã‚±ãƒ¼ã‚¹ã§ãƒ•ァイルå変更ãŒç™ºç”Ÿã™ã‚‹ã?
260 // File file = upFile.getFile(); // 5.7.1.1 (2013/12/13)
261 // if( file != null && newName != null && newName.length() > 0 ) {
262 // File newFile = new File( dir,newName );
263 //
264 // // 5.6.5.3 (2013/06/28) useBackup ファイルアãƒ??ãƒãƒ¼ãƒ‰æ™‚ã«ã€ãƒãƒ?‚¯ã‚¢ãƒ??処ç?™ã‚‹ã‹ã©ã?‹[true/false]を指å®?
265 //// if( newFile.exists() && !newFile.delete() ) {
266 //// String errMsg = "æ—¢å˜ã?ファイル[" + newName + "]ãŒå‰Šé™¤ã§ãã¾ã›ã‚“ã§ã—ãŸã€?;
267 //// throw new RuntimeException( errMsg );
268 //// }
269 // if( newFile.exists() ) {
270 // if( useBackup ) {
271 // // newName ã«ãƒ•ォルãƒ?šŽå±¤ã‚’å«ã‚??åˆã«ã€ããªãˆã¦ã€?
272 // File parent = newFile.getParentFile(); // ãƒãƒƒã‚¯ã‚¢ãƒ??ã™ã¹ãファイルã®ãƒ•ォルãƒ?
273 // File backup = new File( parent , "_backup" ); // ãã?直下ã«ã€?_backup" フォルãƒ?‚’作æ?
274 // if( backup != null && !backup.exists() && !backup.mkdirs() ) {
275 // String errMsg = "ãƒãƒƒã‚¯ã‚¢ãƒ??処ç?§backupフォルãƒ??作æ?ã«å¤±æ•—ã—ã¾ã—ãŸã€?" + backup + "]";
276 // throw new RuntimeException( errMsg );
277 // }
278 // // ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルåã?ã€å?ã®ãƒ•ァイルå?æ‹¡å¼µåå«ã‚? ??"_" + ç¾åœ¨æ™‚刻ã®long値 + "." + å…??ãƒ•ã‚¡ã‚¤ãƒ«ã®æ‹¡å¼µå?
279 // String bkupName = newFile.getName() + "_" + System.currentTimeMillis() + "." + getExtension( newName ) ;
280 // File fromFile = new File( dir,newName ); // オリジナル㮠newFile ã‚’rename ã™ã‚‹ã¨ã¾ãšã„ã®ã§ã€åŒåã?Fileオブジェクトを作æ?
281 // File bkupFile = new File( backup,bkupName );
282 //
283 // if( !fromFile.renameTo( bkupFile ) ) {
284 // String errMsg = "ãƒãƒƒã‚¯ã‚¢ãƒ??処ç?§ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルをリãƒã?ãƒ?§ãã¾ã›ã‚“ã§ã—ãŸã€?" + bkupFile + "]" ;
285 // throw new RuntimeException( errMsg );
286 // }
287 // }
288 // else if( !newFile.delete() ) {
289 // String errMsg = "æ—¢å˜ã?ファイル[" + newName + "]ãŒå‰Šé™¤ã§ãã¾ã›ã‚“ã§ã—ãŸã€?;
290 // throw new RuntimeException( errMsg );
291 // }
292 // }
293 //
294 //// file.renameTo( newFile );
295 // if( !file.renameTo( newFile ) ) {
296 // String errMsg = "æ‰?®šã?ファイルをリãƒã?ãƒ?§ãã¾ã›ã‚“ã§ã—ãŸã€?" + file + "]" ;
297 // throw new RuntimeException( errMsg );
298 // }
299 //// UploadedFile fup = files.get( name );
300 //// fup.setFilesystemName( newName );
301 // upFile.setFilesystemName( newName );
302 // }
303 }
304 // 5.7.1.2 (2013/12/20) zip 対�
305 // ä¸?—¦ä¿ç•™ã«ã—ã¾ã™ã?
306 // if( isZip ) {
307 // File zipFile = new File( saveDirectory,inputFilename );
308 // ZipArchive.compress( zipFile,tgtFiles );
309 // }
310 }
311
312 /**
313 * リクエストパラメータã®åå‰é…å?ã‚’å–å¾—ã—ã¾ã™ã?
314 *
315 * @return リクエストパラメータã®åå‰é…å?
316 */
317 public String[] getParameterNames() {
318 Set<String> keyset = parameters.keySet();
319 return keyset.toArray( new String[keyset.size()] );
320 }
321
322 /**
323 * ファイルアãƒ??ãƒãƒ¼ãƒ‰ã•れãŸãƒ•ァイル群ã®ãƒ•ァイルåé?列をå–å¾—ã—ã¾ã™ã?
324 *
325 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿œã?為ã€å»?¢
326 *
327 * @return ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã•れãŸãƒ•ァイルåé?åˆ?
328 */
329 // public String[] getFilenames() {
330 // Set<String> keyset = files.keySet();
331 // return keyset.toArray( new String[keyset.size()] );
332 // }
333
334 /**
335 * ファイルアãƒ??ãƒãƒ¼ãƒ‰ã•れãŸãƒ•ァイル群ã®ãƒ•ァイルé…å?ã‚’å–å¾—ã—ã¾ã™ã?
336 *
337 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
338 *
339 * @return ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã•れãŸãƒ•ァイル群
340 */
341 public UploadedFile[] getUploadedFile() {
342 return files.toArray( new UploadedFile[files.size()] );
343 }
344
345 /**
346 * æŒ?®šã?åå‰ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆãƒ‘ラメータã®å€¤ã‚’å–å¾—ã—ã¾ã™ã?
347 *
348 * è¤?•°å˜åœ¨ã™ã‚‹å ´åˆã?ã€ä¸?•ªæœ?¾Œã?値を返ã—ã¾ã™ã?
349 *
350 * @param name リクエストパラメータå?
351 *
352 * @return パラメータã®å€¤
353 */
354 public String getParameter( final String name ) {
355 List<String> values = parameters.get(name);
356 if( values == null || values.isEmpty() ) {
357 return null;
358 }
359 return values.get(values.size() - 1);
360 }
361
362 /**
363 * æŒ?®šã?åå‰ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆãƒ‘ラメータã®å€¤ã‚’é?列型å¼ã§å–å¾—ã—ã¾ã™ã?
364 *
365 * @og.rev 5.3.2.0 (2011/02/01) æ–°è¦ä½œæ?
366 *
367 * @param name リクエストパラメータå?
368 *
369 * @return パラメータã®å€¤é…å?
370 */
371 public String[] getParameters( final String name ) {
372 List<String> values = parameters.get(name);
373 if( values == null || values.isEmpty() ) {
374 return null;
375 }
376 // return values.toArray( new String[0] );
377 return values.toArray( new String[values.size()] );
378 }
379
380 /**
381 * æŒ?®šã?åå‰ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆãƒ‘ラメータã®å€¤ã‚’é?åˆ?int)åž‹å¼ã§å–å¾—ã—ã¾ã™ã?
382 *
383 * @og.rev 5.3.2.0 (2011/02/01) æ–°è¦ä½œæ?
384 * @og.rev 5.3.6.0 (2011/06/01) é…å?値ã?"ã®å ´åˆã«NumberFormatExceptionãŒç™ºç”Ÿã™ã‚‹ãƒã‚°ã‚’ä¿®æ£
385 *
386 * @param name リクエストパラメータå?
387 *
388 * @return パラメータã®å€¤é…å?
389 */
390 public int[] getIntParameters( final String name ) {
391 List<String> values = parameters.get(name);
392 if( values == null || values.isEmpty() ) {
393 return null;
394 }
395
396 // int[] rtn = new int[values.size()];
397 // for( int i=0; i<values.size(); i++ ) {
398 // rtn[i] = Integer.valueOf( values.get(i) );
399 // }
400
401 // 5.3.6.0 (2011/06/01) ゼãƒã‚¹ãƒˆãƒªãƒ³ã‚°("")ã¯int変æ›å¯¾è±¡ã‹ã‚‰äºˆã‚除外ã™ã‚?
402 List<Integer> intVals = new ArrayList<Integer>();
403 for( int i=0; i<values.size(); i++ ) {
404 String str = values.get(i);
405 if( str != null && str.length() > 0 ) {
406 intVals.add( Integer.parseInt( str ) );
407 }
408 }
409 if( intVals.isEmpty() ) {
410 return null;
411 }
412
413 int[] rtn = new int[intVals.size()];
414 for( int i=0; i<intVals.size(); i++ ) {
415 rtn[i] = intVals.get(i).intValue();
416 }
417
418 return rtn;
419 }
420
421 /**
422 * æŒ?®šã?åå‰ã® UploadedFile オブジェクトã‹ã‚?登録ã•れるファイルåã‚’å–å¾—ã—ã¾ã™ã?
423 *
424 * 登録ã•れるファイルåã¨ã¯ã€æ–°ãŸã«æ›¸ãæ›ãˆã‚‰ã‚ŒãŸãƒ•ァイルåã?ã“ã¨ã§ã™ã?
425 *
426 * @og.rev 5.6.6.1 (2013/07/12) null 対ç?
427 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿œã?ãŸã‚å»?¢
428 *
429 * @param name ã‚ーæƒ??
430 *
431 * @return æ–°ãŸã«æ›¸ãæ›ãˆã‚‰ã‚ŒãŸãƒ•ァイルå?
432 */
433 // public String getFilesystemName( final String name ) {
434 // UploadedFile file = files.get(name);
435 //// return file.getFilesystemName(); // may be null
436 // return (file == null) ? null : file.getFilesystemName(); // may be null
437 // }
438
439 /**
440 * æŒ?®šã?åå‰ã® UploadedFile オブジェクトã‹ã‚?ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã•れãŸãƒ•ァイルåã‚’å–å¾—ã—ã¾ã™ã?
441 *
442 * ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã•れãŸãƒ•ァイルåã¨ã¯ã€ã‚ªãƒªã‚¸ãƒŠãƒ«ã®ãƒ•ァイルåã?ã“ã¨ã§ã™ã?
443 *
444 * @og.rev 5.6.6.1 (2013/07/12) null 対ç?
445 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿œã?ãŸã‚å»?¢
446 *
447 * @param name ã‚ーæƒ??
448 *
449 * @return オリジナルã®ãƒ•ァイルå?
450 */
451 // public String getOriginalFileName( final String name ) {
452 // UploadedFile file = files.get(name);
453 //// return file.getOriginalFileName(); // may be null
454 // return (file == null) ? null : file.getOriginalFileName(); // may be null
455 // }
456
457 /**
458 * æŒ?®šã?åå‰ã® UploadedFile オブジェクトã‹ã‚?File オブジェクトをå–å¾—ã—ã¾ã™ã?
459 *
460 * @og.rev 5.6.6.1 (2013/07/12) null 対ç?
461 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
462 *
463 * @param name ã‚ーæƒ??
464 *
465 * @return Fileオブジェク�
466 */
467 // public File getFile( final String name ) {
468 // UploadedFile file = files.get(name);
469 //// return file.getFile(); // may be null
470 // return (file == null) ? null : file.getFile(); // may be null
471 // }
472
473 /**
474 * ファイルåã‹ã‚?æ‹¡å¼µåã‚’å–å¾—ã—ã¾ã™ã?
475 *
476 * @og.rev 5.7.1.1 (2013/12/13) UploadedFileクラスã«ç§»å‹?
477 *
478 * @param fileName ファイルå?
479 * @return æ‹¡å¼µå?
480 */
481 // private String getExtension( final String fileName ) {
482 // int index = fileName.lastIndexOf('.');
483 // if(index!=-1) {
484 // return fileName.substring(index + 1, fileName.length());
485 // }
486 // return "";
487 // }
488 }
489
490 /**
491 * ファイルをサーãƒã?ã«ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ã™ã‚‹å?åˆã«ä½¿ç”¨ã•れるファイル管ç??部クラスã§ã™ã?
492 *
493 * @og.group ãã?他機è?
494 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿œã?ãŸã‚ã€publicåŒ?
495 *
496 * @version 4.0
497 * @author Kazuhiko Hasegawa
498 * @since JDK5.0,
499 */
500 //final class UploadedFile {
501 //
502 // private String filename;
503 // private final String name;
504 // private final String dir;
505 // private final String original;
506 // private final String type;
507 //
508 // /**
509 // * ã‚¢ãƒ??ãƒãƒ¼ãƒ‰ãƒ•ァイルã®ç®¡ç?‚ªãƒ–ジェクトを作æ?ã—ã¾ã™ã?
510 // *
511 // * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
512 // *
513 // * @param dir ファイルをä¿ç®¡ã™ã‚‹ãƒ•ォルãƒ?
514 // * @param name ファイルアãƒ??ãƒãƒ¼ãƒ‰ã•ã‚ŒãŸæ™‚ã?name属æ?
515 // * @param filename ファイルå?ç½®ãæ›ãˆå¾?
516 // * @param original ファイルå?オリジナル)
517 // * @param type コンãƒ?ƒ³ãƒˆã‚¿ã‚¤ãƒ?
518 // */
519 // UploadedFile( final String dir, final String name, final String filename, final String original, final String type) {
520 // this.dir = dir;
521 // this.name = name;
522 // this.filename = filename;
523 // this.original = original;
524 // this.type = type;
525 // }
526 //
527 // /**
528 // * ファイルアãƒ??ãƒãƒ¼ãƒ‰ã•ã‚ŒãŸæ™‚ã?name属æ?ã‚’å–å¾—ã—ã¾ã™ã?
529 // *
530 // * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ãƒãƒ¼ãƒ‰ã?è¤?•°é¸æŠžï¼?ultiple?‰å¯¾å¿?
531 // *
532 // * @return ファイルアãƒ??ãƒãƒ¼ãƒ‰ã•ã‚ŒãŸæ™‚ã?name属æ?
533 // */
534 // public String getName() {
535 // return name;
536 // }
537 //
538 // /**
539 // * コンãƒ?ƒ³ãƒˆã‚¿ã‚¤ãƒ—ã‚’å–å¾—ã—ã¾ã™ã?
540 // *
541 // * @return コンãƒ?ƒ³ãƒˆã‚¿ã‚¤ãƒ?
542 // */
543 // public String getContentType() {
544 // return type;
545 // }
546 //
547 // /**
548 // * ファイルå?ç½®ãæ›ãˆå¾?ã‚’å–å¾—ã—ã¾ã™ã?
549 // *
550 // * @return ファイルå?ç½®ãæ›ãˆå¾?
551 // */
552 // public String getFilesystemName() {
553 // return filename;
554 // }
555 //
556 // /**
557 // * ファイルå?ç½®ãæ›ãˆå¾?ã‚’ã‚»ãƒ?ƒˆã—ã¾ã™ã?
558 // *
559 // * @param name ファイルå?ç½®ãæ›ãˆå¾?
560 // */
561 // public void setFilesystemName( final String name ) {
562 // filename = name;
563 // }
564 //
565 // /**
566 // * ファイルå?オリジナル)ã‚’å–å¾—ã—ã¾ã™ã?
567 // *
568 // * @return ファイルå?オリジナル)
569 // */
570 // public String getOriginalFileName() {
571 // return original;
572 // }
573 //
574 // /**
575 // * ファイルå?ç½®ãæ›ãˆå¾?ã® File オブジェクトをå–å¾—ã—ã¾ã™ã?
576 // *
577 // * @return File File オブジェク�
578 // */
579 // public File getFile() {
580 // if(dir == null || filename == null) {
581 // return null;
582 // }
583 // else {
584 // return new File(dir + File.separator + filename);
585 // }
586 // }
587 //}