Storage¶
Storageとは¶
Storageは分散システムで intra-mart Accel Platform を利用しているときに、アップロードされたファイルやシステムで共有したいファイル(主にデータファイル)を一元管理します。コラム
分散システムを構築する場合、各 Web Application Server で共有するディレクトリを設定しておく必要があります。詳細はセットアップガイドを参照してください。
Storageの種類¶
SystemStorage
システムで利用するファイルを保存する領域です。主に、 intra-mart Accel Platform の基盤APIやアプリケーション内の処理で利用されます。PublicStorage
アップロードされたファイルや利用者間で共有したいファイルを保存する領域です。Storageにファイルを保存する場合は、基本的にPublicStorageに保存します。SessionScopeStorage
一時的にファイルを保存する領域です。処理の途中でアップロードされたファイルや保存されたデータを一時的に保存したい場合に利用します。SessionScopeStorageに保存されたファイルは、セッションの有効期間が切れたタイミングで自動的に削除されます。
ストリーミング¶
intra-mart Accel Platform ではStorageの保存されているデータをストリームで扱うことができるようになりました。ストリームを利用することで、従来のintra-mart WebPaltformのように一度 Web Application Server のメモリ上にファイルデータを持つ必要がなくなり、サイズの大きいファイルのアップロードやダウンロードが行えるようになります。コラム
Storage APIのload(),read(),save(),write()メソッドを使用するとファイルデータがAPサーバのメモリ上に展開されるため、メモリを圧迫します。その為、これらのメソッドの利用は推奨しません。
プログラミング方法¶
ファイルアップロード¶
ここでは、PublicStorage内の”sample”ディレクトリにファイルをアップロードする例を示します。
- Actionクラス
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.struts.upload.FormFile; import org.seasar.framework.container.annotation.tiger.Binding; import org.seasar.framework.container.annotation.tiger.BindingType; import org.seasar.framework.exception.IORuntimeException; import org.seasar.struts.annotation.Execute; import jp.co.intra_mart.common.aid.jdk.java.io.IOUtil; import jp.co.intra_mart.foundation.service.client.file.PublicStorage; public class UploadAction { @Binding(bindingType = BindingType.NONE) public FormFile formFile; /** * クライアントよりアップロードされたファイルをPublicStorageに保存します。 */ @Execute(validator = false) public String upload() { // ファイル保存先を指定したPublicStorageのインスタンスを生成します。 final PublicStorage storage = new PublicStorage("sample", formFile.getFileName()); try { final InputStream istr = formFile.getInputStream(); try { final OutputStream ostr = storage.create(); try { // アップロードされたファイルをPublicStorageに書き込みます。 IOUtil.transfer(istr, ostr); } finally { IOUtil.closeCloseableQuietly(ostr); } } finally { IOUtil.closeCloseableQuietly(istr); } } catch (final IOException e) { throw new IORuntimeException(e); } return "index.jsp"; } }
ファイルダウンロード¶
ここでは、PublicStorage内のファイル”sample.txt”をダウンロードする例を示します。import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.seasar.framework.exception.IORuntimeException; import org.seasar.struts.annotation.Execute; import jp.co.intra_mart.common.aid.jdk.java.io.IOUtil; import jp.co.intra_mart.foundation.service.client.file.PublicStorage; import jp.co.intra_mart.framework.extension.seasar.struts.util.IMResponseUtil; public class DownloadAction { /** * PublicStorageに保存されているファイル「sample.txt」をクラアントへ送信します。 */ @Execute(validator = false) public String download() { try { // sample.txtを指定したPublicStorageのインスタンスを生成します。 final PublicStorage storage = new PublicStorage("sample.txt"); if (!storage.isFile()) { // ファイルが存在しない場合は例外をスローします。 throw new FileNotFoundException(); } final InputStream istr = storage.open(); try { // クライアントへsample.txtを送信します。 IMResponseUtil.download(storage.getName(), istr, storage.length()); } finally { IOUtil.closeCloseableQuietly(istr); } return null; } catch (final IOException e) { throw new IORuntimeException(e); } } }