欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2上传文件以及拦截器配置

程序员文章站 2022-05-28 15:50:40
...

struts.xml文件:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>

<!--设置Struts应用的解码集-->
    <constant name="struts.i18n.encoding" value="GBK"/>
    
    
<package name="helloworld" extends="struts-default" namespace="/">

		<interceptors>
            <!--配置拦截器栈(在拦截器章节有讲述)-->
            <interceptor-stack name="myStack">
               <!--配置fileUpload拦截器-->
                <interceptor-ref name="fileUpload">
                    <!--配置允许上传文件的类型(此处要注意的是png图片在ie浏览器中是image/x-png类型)-->
                    <param name="allowedTypes">image/png,image/bmp,image/gif,image/jpeg,image/jpg,text/plain</param>
                    <!--配置允许上传文件大小拦截器,单位是字节(2的16次幂=65536(64k))-->
                    <param name="maximumSize">65536555</param>
                </interceptor-ref>
                <!-- 这个是系统默认的Stack,如果你只调用上面自己写的Stack,则会只执行login拦截器的方法,很多地方会发生错误接收不到值-->
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>
        
       <!-- 文件上传action -->
	<action name="upload" class="cn.mxr.test01.UploadFile">
			<!-- 调用上面配置的拦截器 -->
           <interceptor-ref name="myStack"></interceptor-ref>
		<result name="success">/success.jsp</result>
		<result name="error">/error.jsp</result>
	</action>
	
	
	 
	<action name="helloAction" class="cn.mxr.test01.HelloWorldAction" 
		method="checkLogin">
		<interceptor-ref name="params"></interceptor-ref>
		<interceptor-ref name="timer"></interceptor-ref>
		<!-- 
		<result name="success" type="freemarker">/hello.fm</result>
		
		 -->
		 <result name="success">/helloworld.jsp</result>
	</action>
	
	
	
</package>
</struts>

UploadFile.java类文件:

package cn.mxr.test01;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadFile extends ActionSupport{

	private File myFile;//表单上提供的字段,上传的文件
	//struts2在文件上传时提供的属性
	private String myFileContentType;//上传的文件名。上传字段名称+FileName  注意大小写
	private String myFileFileName; //上传文件的MIME类型。上传字段名称+ContentType 注意大小写
	private String destPath; //文件保存的路径
	
	public String execute() {
		
		this.destPath="d:"+File.separator+"test";
		
		try {
			System.out.println("Src File name: " + myFile);
	     	 System.out.println("Dst File name: " + myFileFileName);
	     	    	 
	     	 File destFile  = new File(destPath,myFileFileName);	//文件保存的路径
	     	 System.out.println("destFile:"+destFile);
	    	 FileUtils.copyFile(myFile, destFile);	//拷贝:把文件的临时文件复制到指定的位置。注意:临时文件还在
	     	 
	     	
		} catch (Exception e) {
			// TODO: handle exception
	         e.printStackTrace();

			return "error";
		}
		return "success";
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	
	
}

upload.jsp文件

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>File Upload</title>
</head>
<body>
   <form action="upload" method="post" enctype="multipart/form-data">
      <label for="myFile">Upload your file</label>
      <input type="file" name="myFile" />
      <input type="submit" value="Upload"/>
   </form>
</body>
</html>

 

转载于:https://my.oschina.net/u/3801890/blog/1645107