「Struts1」ファイルアップロードサンプル(サイズチェック有)

Struts 1 系でファイルアップロードを行なうサンプル。取り急ぎメモだけ・・・

controller 要素の maxFileSize 属性でファイルサイズの上限設定、ActionForm の validate でサイズチェックしている感じです。

1. 設定ファイル


struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
 <struts-config>
  <form-beans>
    <form-bean name="UploadForm" type="upload.form.UploadForm" />
  </form-beans>

  <action-mappings>  
    <action path="/upload" type="upload.action.UploadAction" name="UploadForm" scope="request" input="/uploadFail.jsp">
      <forward name="success" path="/uploadSuccess.jsp"/>
    </action>
  </action-mappings>
  
  <!-- ファイルサイズの上限を1メガに設定 -->
  <controller maxFileSize="1M"/>
  
  <!-- メッセージリソースの設定 (MessageResources.properties は upload.form パッケージに配置)-->
  <message-resources parameter="upload/form/MessageResources"/>
</struts-config>

■ web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

■ MessageResources.properties

size.over=Upload FileSize Over !!


2. 画面


■ upload.jsp ( アップロード画面 )

<%@ page language="java" contentType="text/html; charset=Windows-31J" pageEncoding="Windows-31J"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-31J">
</head>
<body>
<html:form action="/upload" enctype="multipart/form-data">
<html:file property="file"/>
<html:submit value="send"/>
</html:form>
</body>
</html>

■ uploadSuccess.jsp ( アップロード成功時の画面 )

<%@ page language="java" contentType="text/html; charset=Windows-31J" pageEncoding="Windows-31J"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-31J">
<title>Insert title here</title>
</head>
<body>
Name:<bean:write name="name"/><br>
Size:<bean:write name="size" format="#"/>
</body>
</html>

■ uploadFail.jsp ( アップロード失敗時の画面 )

<%@ page language="java" contentType="text/html; charset=Windows-31J" pageEncoding="Windows-31J"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-31J">
<title>Insert title here</title>
</head>
<body>
<html:errors/>
</body>
</html>


3. ActionForm / Action クラス


■ UploadForm.java

package upload.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;

public class UploadForm extends ActionForm {
  private FormFile file;

  public FormFile getFile() {
    return file;
  }

  public void setFile(FormFile file) {
    this.file = file;
  }

  //ファイルサイズが上限超えたかチェックする
  public ActionErrors validate(ActionMapping mapping, HttpServletRequest req) {
    ActionErrors errors = new ActionErrors();

    //上限超えた場合 true (超えない場合は null になる)
    Boolean sizeChk = (Boolean) req
        .getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
    if (sizeChk != null && sizeChk) {
      errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("size.over"));
    }
    return errors;
  }
}

■ UploadAction.java

package upload.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import upload.form.UploadForm;

public class UploadAction extends Action {
  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest req, HttpServletResponse res) {

    UploadForm f = (UploadForm) form;
    FormFile file = f.getFile();
    req.setAttribute("name", file.getFileName());
    req.setAttribute("size", file.getFileSize());

    return (mapping.findForward("success"));
  }
}


[ 環境情報 ]
Windows 7 SP1
Apache Tomcat 8.0.21
Java SE 8 Update 25
Struts 1.3.10