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.multipart;
017
018 import org.opengion.fukurou.util.Closer ;
019
020 import java.io.File;
021 import java.io.InputStream;
022 import java.io.OutputStream;
023 import java.io.BufferedOutputStream;
024 import java.io.FileOutputStream;
025 import java.io.IOException;
026 import javax.servlet.ServletInputStream;
027
028 /**
029 * ファイルア??ロード時のマルチパート???ファイルパ?ト部品です?
030 *
031 * ファイル??を取り扱?す?
032 *
033 * @og.group そ?他機?
034 *
035 * @version 4.0
036 * @author Kazuhiko Hasegawa
037 * @since JDK5.0,
038 */
039 public class FilePart extends Part {
040
041 private String filename;
042 private final String filePath;
043 private final String contentType;
044 private final PartInputStream partInput;
045
046 /**
047 * ファイルパ?ト部?オブジェクトを構築する?コンストラクター
048 *
049 * @param name Part名称
050 * @param in ServletInputStreamオブジェク?
051 * @param boundary ???
052 * @param contentType コン??イ?
053 * @param filename ファイル?
054 * @param filePath ファイルパス
055 * @throws IOException 入出力エラーが発生したと?
056 */
057 FilePart( final String name, final ServletInputStream in, final String boundary,
058 final String contentType, final String filename, final String filePath)
059 throws IOException {
060 super(name);
061 this.filename = filename;
062 this.filePath = filePath;
063 this.contentType = contentType;
064 partInput = new PartInputStream(in, boundary);
065 }
066
067 /**
068 * ファイル名を取得します?
069 *
070 * @return ファイル?
071 */
072 public String getFilename() {
073 return filename;
074 }
075
076 /**
077 * ファイル名をセ?します?
078 *
079 * @param fname ファイル?
080 */
081 public void setFilename( final String fname ) {
082 filename = fname ;
083 }
084
085 /**
086 * ファイルパスを取得します?
087 *
088 * @return ファイルパス
089 */
090 public String getFilePath() {
091 return filePath;
092 }
093
094 /**
095 * コン??イプを取得します?
096 *
097 * @return コン??イ?
098 */
099 public String getContentType() {
100 return contentType;
101 }
102
103 /**
104 * 入力ストリー?取得します?
105 *
106 * @return 入力ストリー?
107 */
108 public InputStream getInputStream() {
109 return partInput;
110 }
111
112 /**
113 * ??ファイルに書き?します?
114 *
115 * @param fileOrDirectory 出力?ファイル??レクトリ?
116 *
117 * @return ストリー?書き?したバイト数
118 * @throws IOException 入出力エラーが発生したと?
119 */
120 public long writeTo( final File fileOrDirectory ) throws IOException {
121 long written = 0;
122
123 OutputStream fileOut = null;
124 try {
125 // Only do something if this part contains a file
126 if(filename != null) {
127 // Check if user supplied directory
128 File file;
129 if(fileOrDirectory.isDirectory()) {
130 // Write it to that dir the user supplied,
131 // with the filename it arrived with
132 file = new File(fileOrDirectory, filename);
133 }
134 else {
135 // Write it to the file the user supplied,
136 // ignoring the filename it arrived with
137 file = fileOrDirectory;
138 }
139 fileOut = new BufferedOutputStream(new FileOutputStream(file));
140 written = write(fileOut);
141 }
142 }
143 finally {
144 Closer.ioClose( fileOut ); // 4.0.0 (2006/01/31) close 処?の IOException を無?
145 }
146 return written;
147 }
148
149 /**
150 * ??ストリー?書き?します?
151 *
152 * @param out OutputStreamオブジェク?
153 *
154 * @return ストリー?書き?したバイト数
155 * @throws IOException 入出力エラーが発生したと?
156 */
157 long write( final OutputStream out ) throws IOException {
158 // decode macbinary if this was sent
159 long size=0;
160 int read;
161 byte[] buf = new byte[8 * 1024];
162 while((read = partInput.read(buf)) != -1) {
163 out.write(buf, 0, read);
164 size += read;
165 }
166 return size;
167 }
168
169 /**
170 * ファイルかど?
171 *
172 * @return (常に true)
173 */
174 @Override
175 public boolean isFile() {
176 return true;
177 }
178 }