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