---
title: "《struts2权威指南》学习笔记之struts2文件上传--使用fileUpload拦截器"
author: "esffor"
date: "2008-02-28T11:58:31Z"
tags: []
slug: "struts2-struts2-fileupload"
draft: false
lastmod: "2008-02-28T11:58:31Z"
id: 168175
url: "https://esffor.iteye.com/blog/168175"
---

上传页面：

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ page language="java" contentType="text/html; charset=GBK"%&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@taglib prefix="s" uri="/struts-tags"%&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ page isELIgnored="false" %&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;meta http-equiv="Content-Type" content="text/html; charset=GBK" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;title&gt;&lt;/title&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;body&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;s:fielderror/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;form action="upload.action" method="post" enctype="multipart/form-data"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;input type="text" name="title" /&gt;&lt;br&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;input type="file" name="upload" /&gt;&lt;br&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;input value="upload" type="submit" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/form&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/body&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 上传action:

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package lee;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.opensymphony.xwork2.Action;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.opensymphony.xwork2.ActionContext;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.struts2.ServletActionContext;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.opensymphony.xwork2.ActionSupport;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public class UploadAction extends ActionSupport  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String title;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private File upload;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String uploadContentType;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String uploadFileName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    //接受依赖注入的属性  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String savePath;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    //接受依赖注入的方法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public void setSavePath(String value)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.savePath = value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String getSavePath() throws Exception   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return ServletActionContext.getRequest().getRealPath(savePath);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public void setTitle(String title) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.title = title;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public void setUpload(File upload) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.upload = upload;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public void setUploadContentType(String uploadContentType) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.uploadContentType = uploadContentType;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public void setUploadFileName(String uploadFileName) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.uploadFileName = uploadFileName;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String getTitle() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return (this.title);   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public File getUpload() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return (this.upload);   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String getUploadContentType() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return (this.uploadContentType);   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String getUploadFileName() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return (this.uploadFileName);   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    @Override  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public String execute() throws Exception  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println("开始上传单个文件-----------------------");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println(getSavePath());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println("==========" + getUploadFileName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println("==========" + getUploadContentType());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println("==========" + getUpload());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //以服务器的文件保存地址和原文件名建立上传文件输出流  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FileInputStream fis = new FileInputStream(getUpload());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        byte\[] buffer = new byte\[1024];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int len = 0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        while ((len = fis.read(buffer)) &gt; 0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            fos.write(buffer , 0 , len);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return SUCCESS;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

 

struts.xml

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!DOCTYPE struts PUBLIC  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;struts&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;constant name="struts.custom.i18n.resources" value="globalMessages"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;constant name="struts.i18n.encoding" value="GBK"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;package name="lee" extends="struts-default"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;action name="upload" class="lee.UploadAction"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt;interceptor-ref name="fileUpload"&gt;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;param name="allowedTypes"&gt;image/bmp,image/png,image/gif,image/jpeg,image/jpg&lt;/param&gt;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;param name="maximumSize"&gt;2000&lt;/param&gt;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;/interceptor-ref&gt;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;interceptor-ref name="defaultStack"/&gt;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;param name="savePath"&gt;/upload&lt;/param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;result&gt;/succ.jsp&lt;/result&gt;      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;result name="input"&gt;/upload.jsp&lt;/result&gt;      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;/action&gt;          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/package&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/struts&gt;      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

如果需要使用文件上传拦截器来过滤文件大小，或者过滤文件内容，则必须显示配置引用Struts默认的拦截器栈：defaultStack

资源文件中配置文件类型不正确和大小不正确的提示：

struts.messages.error.content.type.not.allowed = 文件类型不正确  
struts.messages.error.file.too.large= 文件大小不正确