Struts 2ファイルのアップロード


Struts 2は自分の要求の解像度を提供していません。つまり、Struts 2は自分でmultiipad/form-dataの要求を処理しません。他の要求の解像器を呼び出して、HTTP要求のフォーム領域を解析します。しかし、Struts 2は既存のアップロード解像器をもとに、一歩進んでカプセル化しました。さらにファイルのアップロードを簡略化しました。
以下はファイルアップロードの具体的なコードです。
まず一つのupload.jspを定義します。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'upload.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <s:form action="uploadAction" enctype="multipart/form-data" method="POST">
     	<s:file label="    " name="upload"></s:file>
         <s:submit/>
     </s:form>
  </body>
</html>
次にUploadActionを定義する:

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{
	
	//          
	private File upload;
	//           
	private String uploadContentType;
	//          
	private String uploadFileName;
	//         
	private String savePath;
	//         
	public void setSavePath(String value)
	{
		this.savePath=value;
	}
	//           
	private String getSavePath()throws Exception
	{
		return ServletActionContext.getRequest().getRealPath(savePath);
	}
	
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String execute()throws Exception
	{		
		FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
		FileInputStream fs = new FileInputStream(getUpload());
		byte[]buffer = new byte[1024];
		int len=0;
		while((len=fs.read(buffer))>0)
		{
			fos.write(buffer,0,len);
		}
		fs.close();
		fos.close(); 	
		return SUCCESS;
	}
}
フォームにname属性がxxxのファイル領域が含まれている場合、対応するアクションは、ファイル領域の情報を3つの属性でカプセル化する必要がある。
1.タイプはFileのxxx属性で、このファイル領域に対応するファイル内容をカプセル化しました。
2.StringタイプのxxFileName属性は、このファイル領域に対応するファイルのファイル名をカプセル化しています。
3.SteringタイプのxxContentType属性はこのファイル領域対応ファイルのファイルタイプをカプセル化しました。
他の2つの属性がフォームに使用されていない理由は、これらはすべてfileUpload傍受の功労であり、具体的にはstruts-default.xmlの構成を確認することができます。
最終配置struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<package name="struts2demo" extends="struts-default">
			<action name="uploadAction" class="action.UploadAction">
			<param name="savePath">/upload</param>
			<result>/success.jsp</result>
			</action>		
	</package>
</struts>
以上の過程で簡単なファイルアップロードができます。