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