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 java.io.File;
019 import java.io.IOException;
020
021 import org.opengion.fukurou.util.FileUtil;
022
023 /**
024 * ファイルをサーバã?にアãƒ??ロードするå?合に使用されるファイル管ç�?‚¯ラスですã?
025 * HTML5 ファイルアãƒ??ロードã?è¤?•°選択ï¼?ultiple?‰対å¿?に伴ã�??ä¸?�¤のクラスとして public化しますã?
026 *
027 * @og.group そã?他機è?
028 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ロードã?è¤?•°選択ï¼?ultiple?‰対å¿?
029 *
030 * @version 4.0
031 * @author Kazuhiko Hasegawa
032 * @since JDK5.0,
033 */
034 public final class UploadedFile implements Comparable<UploadedFile> {
035
036 /** バッファの初期容量を通常より多い目に設定しますã? {@value} */
037 public static final int BUFFER_MIDDLE = 200;
038
039 /** シスãƒ?ƒ 依存ã?改行記号をセãƒ?ƒˆしますã? */
040 public static final String CR = System.getProperty("line.separator");
041
042 private File filename = null; // 現時点での置き換え後ファイルå�?
043
044 private final String uniqKey ; // アãƒ??ロードされたファイルå�?ユニã?クにしておきまã�?
045 private final String dir;
046 private final String name;
047 private final String original;
048 private final String type;
049
050 /**
051 * アãƒ??ロードファイルの管ç�?‚ªブジェクトを作æ?しますã?
052 *
053 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ロードã?è¤?•°選択ï¼?ultiple?‰対å¿?
054 *
055 * @param uniqKey ユニã?クキー(初期アãƒ??ロードファイルå�?
056 * @param dir ファイルを保管するフォルãƒ?
057 * @param name ファイルアãƒ??ロードされた時ã?name属æ?
058 * @param original ファイルå�?オリジナル)
059 * @param type コンãƒ?ƒ³トタイãƒ?
060 */
061 // UploadedFile( final String dir, final String name, final String filename, final String original, final String type) {
062 UploadedFile( final String uniqKey, final String dir, final String name, final String original, final String type ) {
063 this.uniqKey = uniqKey; // 5.7.1.1 (2013/12/13) uniqKey を確定させるã€?
064 this.dir = dir;
065 this.name = name;
066 // this.filename = filename;
067 this.original = original;
068 this.type = type;
069 }
070
071 /**
072 * ファイルアãƒ??ロードされた時ã?name属æ?を取得しますã?
073 *
074 * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルアãƒ??ロードã?è¤?•°選択ï¼?ultiple?‰対å¿?
075 *
076 * @return ファイルアãƒ??ロードされた時ã?name属æ?
077 */
078 public String getName() {
079 return name;
080 }
081
082 /**
083 * コンãƒ?ƒ³トタイプを取得しますã?
084 *
085 * @return コンãƒ?ƒ³トタイãƒ?
086 */
087 public String getContentType() {
088 return type;
089 }
090
091 /**
092 * ファイルå�?置き換えå¾?を取得しますã?
093 *
094 * @og.rev 5.7.1.2 (2013/12/20) zip 対応で、Fileオブジェクトを返すようにしますã?
095 *
096 * @return ファイルå�?置き換えå¾?
097 */
098 // public String getFilesystemName() {
099 public File getUploadFile() {
100 return filename;
101 }
102
103 /**
104 * ファイルå�?置き換えå¾?の置き換えを実行しますã?
105 * useBackup = true にすると、dir の直下にã€?_backup" フォルãƒ?‚’作æ?しますã?
106 * バックアãƒ??ファイル名ã?、å?のファイルå�?拡張子含ã‚? ??"_" + 現在時刻のlong値 + "." + å…??ファイルの拡張å?
107 *
108 * newName ã�?null の場合ã?、original のファイル名に、変換しますã?
109 *
110 * @og.rev 5.7.1.1 (2013/12/13) 新規追åŠ?
111 * @og.rev 5.7.2.1 (2014/01/17) FileUtil.renameTo の引数のé ?•ªを間違えてã�?�Ÿã€?
112 *
113 * @param newName ファイルå�?置き換えå¾?
114 * @param useBackup 置き換え後ファイルをバãƒ?‚¯アãƒ??するかどã�?�‹(true:バックアãƒ??する/false:しなã�?
115 * @return æœ?µ‚的に作æ?されたファイルオブジェクãƒ?
116 */
117 public File renameTo( final String newName , final boolean useBackup ) {
118
119 String newNm = newName ;
120 // 新規ファイル名を作æ?しますã?(拡張子ç?
121 if( newNm != null && newNm.length() > 0 ) {
122 // 新ファイル名から拡張子取å¾?
123 String newExt = FileUtil.getExtension( newNm );
124 if( newExt == null || newExt.length() == 0 ) {
125 String oldExt = FileUtil.getExtension( original );
126 newNm = newNm + "." + oldExt ;
127 }
128 }
129 else {
130 newNm = original;
131 }
132
133 File newFile = null ;
134 if( newNm != null && newNm.length() > 0 ) {
135 newFile = new File( dir,newNm );
136
137 File uniqFile = new File( dir , uniqKey ); // 5.7.1.1 (2013/12/13) アãƒ??ロードされたファイル
138
139 // 5.7.2.1 (2014/01/17) FileUtil.renameTo の引数のé ?•ªを間違えてã�?�Ÿã€?
140 // FileUtil.renameTo( newFile, uniqFile , useBackup );
141 FileUtil.renameTo( uniqFile , newFile, useBackup ); // from â‡?to のé ?•ªにæŒ?®šã?
142
143 // // 置き換えファイルの存在チェãƒ?‚¯ã€?
144 // if( newFile.exists() ) {
145 // if( useBackup ) {
146 // // newNm にフォルãƒ?šŽ層を含ã‚??合に、そなえてã€?
147 // File parent = newFile.getParentFile(); // バックアãƒ??すべきファイルのフォルãƒ?
148 // File backup = new File( parent , "_backup" ); // そã?直下にã€?_backup" フォルãƒ?‚’作æ?
149 // if( !backup.exists() && !backup.mkdirs() ) {
150 // String errMsg = "バックアãƒ??処ç�?�§backupフォルãƒ??作æ?に失敗しましたã€?" + backup + "]";
151 // throw new RuntimeException( errMsg );
152 // }
153 // // バックアãƒ??ファイル名ã?、å?のファイルå�?拡張子含ã‚? ??"_" + 現在時刻のlong値 + "." + å…??ファイルの拡張å?
154 // String bkupName = newFile.getName() + "_" + System.currentTimeMillis() + "." + FileUtil.getExtension( newNm ) ;
155 // File fromFile = new File( dir,newNm ); // オリジナルの newFile をrename するとまずいので、同名ã?Fileオブジェクトを作æ?
156 // File bkupFile = new File( backup,bkupName );
157 //
158 // if( !fromFile.renameTo( bkupFile ) ) {
159 // String errMsg = "バックアãƒ??処ç�?�§バックアãƒ??ファイルをリネã?ãƒ?�§きませんでしたã€? +CR
160 // + " [" + fromFile + "] �[" + bkupFile + "]" ;
161 // throw new RuntimeException( errMsg );
162 // }
163 // }
164 // else if( !newFile.delete() ) {
165 // String errMsg = "既存ã?ファイル[" + newNm + "]が削除できませんでしたã€?;
166 // throw new RuntimeException( errMsg );
167 // }
168 // }
169 //
170 // File uniqFile = new File( dir , uniqKey ); // 5.7.1.1 (2013/12/13) アãƒ??ロードされたファイル
171 // if( !uniqFile.renameTo( newFile ) ) {
172 // String errMsg = "æ‰?®šã?ファイルをリネã?ãƒ?�§きませんでしたã€? + CR
173 // + " [" + uniqFile + "] �[" + newFile + "]" ;
174 // throw new RuntimeException( errMsg );
175 // }
176 }
177 // 5.7.1.1 (2013/12/13) ここの処ç�?�Œ走ることは無ã�??ずã?
178 else {
179 String errMsg = "新ファイル名が存在しませんã€?" + newNm + "]" ;
180 throw new RuntimeException( errMsg );
181 }
182 // 新ファイル名ã?セãƒ?ƒˆは、すべての処ç�?�Œ完äº?�—てから、設定するã?
183 filename = newFile ;
184 return filename;
185 }
186
187 /**
188 * ファイルå�?置き換えå¾?をセãƒ?ƒˆしますã?
189 *
190 * @og.rev 5.7.1.1 (2013/12/13) å»?¢
191 *
192 * @param name ファイルå�?置き換えå¾?
193 */
194 // public void setFilesystemName( final String name ) {
195 // filename = name;
196 // }
197
198 /**
199 * ファイルå�?オリジナル)を取得しますã?
200 *
201 * @return ファイルå�?オリジナル)
202 */
203 public String getOriginalFileName() {
204 return original;
205 }
206
207 /**
208 * ファイルå�?置き換えå¾?の File オブジェクトを取得しますã?
209 *
210 * @og.rev 5.7.1.1 (2013/12/13) å»?¢ã€?
211 *
212 * @return Fileオブジェクãƒ?
213 */
214 // public File getFile() {
215 // if(dir == null || filename == null) {
216 // return null;
217 // }
218 // else {
219 // return new File(dir + File.separator + filename);
220 // }
221 // }
222
223 /**
224 * ファイル名かã‚?拡張子を取得しますã?
225 *
226 * @og.rev 5.7.1.1 (2013/12/13) ローカルに移動ã?若干のロジãƒ?‚¯変更
227 *
228 * @param fileName ファイルå�?
229 * @return 拡張å?
230 */
231 // private String getExtension( final String fileName ) {
232 // int index = fileName.lastIndexOf('.');
233 //// if(index!=-1) {
234 //// return fileName.substring(index + 1, fileName.length());
235 //// }
236 // if( index >= 0 ) {
237 // return fileName.substring( index + 1 );
238 // }
239 // return "";
240 // }
241
242 /**
243 * 自然比è¼?ƒ¡ソãƒ?ƒ‰
244 * インタフェース Comparable の 実è£?�«関連して、å?定義してã�?�¾すã?
245 * 登録されたシーケンス(画面の表示é ?で比è¼?�—ますã?
246 * equals メソãƒ?ƒ‰では、キーの同ä¸??のみにç�?›®して判定してã�?�¾すã?
247 * こã?比è¼?�§はã€?運用上同ä¸?‚ーは発生しませんã�?たとえ同ä¸?‚ーが存在した
248 * としてもã?そã?比è¼??が同じになることを保証してã�?�¾せんã€?
249 *
250 * @param other 比è¼?¯¾象のObject
251 *
252 * @return こã?オブジェクトがæŒ?®šされたオブジェクトより小さã�??合ã?è²??整数、等しã�??合ã?ゼロ、大きい場合ã?正の整数
253 * @throws ClassCastException 引数ã�?UploadedFile ではなã�??å�?
254 * @throws IllegalArgumentException 引数ã�?null の場å�?
255 */
256 @Override
257 public int compareTo( final UploadedFile other ) {
258 if( other == null ) {
259 String errMsg = "引数がã?null ですã?" ;
260 throw new IllegalArgumentException( errMsg );
261 }
262
263 return ( uniqKey ).compareTo( other.uniqKey );
264 }
265
266 /**
267 * こã?オブジェクトと他ã?オブジェクトが等しã�?�‹どã�?�‹を示しますã?
268 * 画面は、画面IDが等しければ、è¨?ªžや表示é ?�«関係なく同ä¸?�¨みなされますã?
269 * GUIInfo は、ユーザー個別に扱われ、そのグループには、key は唯ä¸?�§、かつ
270 * 同ä¸?¨?ªžå?で扱われるオブジェクトã?為、同ä¸?�¨みなしますã?
271 *
272 * @param object 比è¼?¯¾象の参ç?オブジェクãƒ?
273 *
274 * @return 引数にæŒ?®šされたオブジェクトとこã?オブジェクトが等しã�??合ã? true、そã�?�§なã�??合ã? false
275 */
276 @Override
277 public boolean equals( final Object object ) {
278 if( object instanceof UploadedFile ) {
279 return uniqKey.equals( ((UploadedFile)object).uniqKey );
280 }
281
282 return false ;
283 }
284
285 /**
286 * オブジェクトã?ハッシュコードå?を返しますã?
287 * こã?メソãƒ?ƒ‰は、java.util.Hashtable によって提供されるような
288 * ハッシュãƒ??ブルで使用するために用意されてã�?�¾すã?
289 * equals( Object ) メソãƒ?ƒ‰をオーバã?ライトしたå?合ã?、hashCode() メソãƒ?ƒ‰ã‚?
290 * å¿?�š 記述するå¿?¦�がありますã?
291 * こã?実è£?�§は、getKey().hashCode() と同å?を返しますã?
292 *
293 * @return こã?オブジェクトã?ハッシュコードå?
294 */
295 @Override
296 public int hashCode() {
297 return uniqKey.hashCode() ;
298 }
299
300 /**
301 * オブジェクトã?識別子として?Œ詳細な画面æƒ??を返しますã?
302 *
303 * @return 詳細な画面æƒ??
304 */
305 @Override
306 public String toString() {
307 StringBuilder rtn = new StringBuilder( BUFFER_MIDDLE );
308 rtn.append( this.getClass().getName() ).append( CR );
309 rtn.append( " uniqKey :").append( uniqKey ).append( CR );
310 rtn.append( " filename :").append( filename ).append( CR );
311 rtn.append( " name :").append( name ).append( CR );
312 rtn.append( " dir :").append( dir ).append( CR );
313 rtn.append( " original :").append( original ).append( CR );
314 rtn.append( " type :").append( type ).append( CR );
315 return rtn.toString();
316 }
317 }