Storage¶
About Storage¶
When intra-mart Accel Platform is used in the distributed system, Storage provides central control of uploaded filesand files (mainly data files) to be shared by the system.Note
In order to build the distributed system, it is necessary to set the directory that is shared by each Web Application Server.Please refer to the Setup Guide for detail.
Storage types¶
SystemStorage
It is the region to store the files used by the system.It is used mainly by the platform API of intra-mart Accel Platform and the process in the applications.PublicStorage
It is the region to store the uploaded files and the files to be shared among users.If files are stored into Storage, they will be basically stored into PublicStorage.SessionScopeStorage
It is the region to store the files temporarily.It is used to temporarily store the uploaded files and saved data in the middle of processing.Files that are stored in SessionScopeStorage will be automatically deleted when the session validity period is expired.
Streaming¶
intra-mart Accel Platform allows the data stored in Storage to be handled as stream.By using the stream, it is no longer necessary to keep the file data on the memory of Web Application Server,which was necessary in the intra-mart Webplatform. This would allow the upload or download of large volume data.Note
When load(),read(),save(),or write() method of Storage API is used, file data is expanded onto the memory of AP server and would cause memory constraint.Therefore, we do not recommend the use of these methods.
Programming Method¶
File Upload¶
Here, an example of file uploading to the “sample” directory in PublicStorage is shown.
- Controller Class
import java.io.IOException; import java.io.OutputStream; import jp.co.intra_mart.common.aid.jdk.java.io.IOUtil; import jp.co.intra_mart.foundation.service.client.file.PublicStorage; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import org.terasoluna.gfw.common.exception.SystemException; @Controller @RequestMapping("upload") public class UploadController { @RequestMapping public String index() { return "index.jsp"; } @RequestMapping(value = "store", method = RequestMethod.POST) public String upload(final MultipartFile uploadFile) { if (!uploadFile.isEmpty()) { // Create public storage instance of file saving destination. final PublicStorage storage = new PublicStorage("sample", "samplefile"); OutputStream os = null; try { os = storage.create(); IOUtil.transfer(uploadFile.getInputStream(), os); } catch (final IOException e) { throw new SystemException("io error code", e); } finally { try { os.close(); } catch (final IOException e) { throw new SystemException("io error code", e); } } return "redirect:/upload"; } return "redirect:/upload"; } }
File Download¶
Here, an example of downloading the “sample.txt” file in PublicStorage is shown.For the details of AbstractFileDownloadView, please refer to Topic about arbitrary file download of TERASOLUNA Global Framework Development Guideline.
- Controller Class
import static org.springframework.web.bind.annotation.RequestMethod.GET; import java.io.FileNotFoundException; import java.io.IOException; import jp.co.intra_mart.foundation.service.client.file.PublicStorage; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.terasoluna.gfw.common.exception.SystemException; import org.terasoluna.gfw.common.message.ResultMessage; import org.terasoluna.gfw.common.message.ResultMessages; @Controller @RequestMapping("sample/tgfw/download") public class DownloadController { @RequestMapping(value = "fetch", method = GET) public String donwload(final Model model) { // Create PublicStorage instance which specifies sample.txt. final PublicStorage storage = new PublicStorage("sample.txt"); try { if (!storage.isFile()) { // If the file does not exist, it is returned to index.jsp as an error. final ResultMessages rs = ResultMessages.warn().add(ResultMessage.fromText("file not found.")); model.addAttribute(rs); return "sample/tgfw/download/index.jsp"; // Returned to index.jsp. } model.addAttribute("storage", storage); // view for download is specified. return "sample.tgfw.app.download.DownloadView"; } catch (final IOException e) { throw new SystemException("io error code", e); } } @RequestMapping public String index() { return "sample/tgfw/download/index.jsp"; } }
- View Class
import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jp.co.intra_mart.foundation.http.ResponseUtil; import jp.co.intra_mart.foundation.service.client.file.Storage; import org.springframework.stereotype.Component; import org.terasoluna.gfw.common.exception.SystemException; import org.terasoluna.gfw.web.download.AbstractFileDownloadView; // view name = sample.tgfw.app.download.DownloadView is registered. @Component("sample.tgfw.app.download.DownloadView") public class DownloadView extends AbstractFileDownloadView { @Override protected void addResponseHeader(final Map<String, Object> model, final HttpServletRequest request, final HttpServletResponse response) { // Header for text file is set. final String disposition; try { // Please set character encoding of server to second argument. disposition = ResponseUtil.encodeFileName(request, "UTF-8", "sample.txt"); } catch (final UnsupportedEncodingException e) { throw new SystemException("download view error code", e); } response.setHeader("Content-Disposition", "attachment;" + disposition); response.setContentType("text/plain"); } @Override protected InputStream getInputStream(final Map<String, Object> model, final HttpServletRequest request) throws IOException { // Get storage which is set in model, and InputStream is returned. final Storage<?> storage = (Storage<?>) model.get("storage"); return storage.open(); } }