Struts 2ラーニングシリーズ--Struts 2アップロードファイル

2962 ワード

プライマリインポートパッケージ:
struts2 2.3.4.1バージョン関連パッケージ
その他
 
 
受信ファイルアクションクラスの処理FileUploadAction
 
 
package com.dahuazwan.ihwsm.demo;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FileUploadAction extends ActionSupport {
	private File image; //     
    private String imageFileName; //    
    private String imageContentType; //    
    private String newName; //   
    
    public String execute() throws Exception {
    	System.out.println(image);
    	System.out.println(imageFileName);
    	System.out.println(imageContentType);
    	System.out.println(newName);
    	return SUCCESS;
    }
 
     // set/get      
}

 
strutsプロファイルstruts.xml:
 
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="false" />
	<!--   Post      -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    
    <constant name="struts.custom.i18n.resources" value="struts"/>
    
    <!--               。    2097152(2M) -->
    <constant name="struts.multipart.maxSize" value="20971520"/>
    <!--             ,    javax.servlet.context.tempdir -->
    <constant name="struts.multipart.saveDir " value="/" />
     	<package name="struts3" extends="struts-default,json-default" namespace="/">
   	    <action name="fileupload" class="com.dahuazwan.ihwsm.demo.FileUploadAction" method="execute">  
            <result name="success">/index.jsp</result> 
        </action> 
   	</package>
 	
</struts>
 
jspページの内容をアップロードします:
 
	<form action="fileupload" method="post" enctype="multipart/form-data"><!--             -->
   
  		<input type="text" name="newName" />
        <input type="file" name="image"><br>
         
        <input type="submit" value="submit">
 
    </form>
 
 
注意点:
 
  • formフォームのmethodはpostであり、enctypeはmultipart/form-data
  • である.
  • FileUploadActionの3つの属性は、File{1}、String{1}FileName、String{1}ContentType、
  • のルールを遵守します.
    フォームのfileと同じ名前でなければなりません.ここでimageの場合、対応する属性はimage、imageFileName、imageContentType
  • FileUploadActionは、フォームの他の属性名を受信する必要があります.ここでnewName
  • のように、フォームの対応する名前と等しい名前でなければなりません.

  •