---
title: "《struts2权威指南》学习笔记之struts2文件上传"
author: "esffor"
date: "2008-02-28T11:03:00Z"
tags: []
slug: "struts2-struts2"
draft: false
lastmod: "2008-02-28T11:03:00Z"
id: 168176
url: "https://esffor.iteye.com/blog/168176"
---

struts2没有提供自己的请求解析器，也就是说，struts2不会自己区处理multipart/form-data的请求，它需要调用其他请求解析器，将HTTP请求中的表单域解析出来，但struts2在原有的上传解析器上作了进一步封装，更进一步简化了文件上传

 Struts2的struts.properties配置文件中，配置struts2的上传文件解析器

struts.multipart.parser=jakarta  (srtuts2默认)，也可以设置为常用的cos,pell等

 

配置上传页面：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;head&gt;  
![](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;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="上传" 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 org.apache.struts2.ServletActionContext;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](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)    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)        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/ExpandedBlockEnd.gif)}

 

struts配置文件

 

![](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;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;/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)

 

web.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)&lt;web-app version="2.5"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    xmlns="http://java.sun.com/xml/ns/javaee"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;filter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/filter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;filter-mapping&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/filter-mapping&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;filter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;filter-name&gt;struts-cleanup&lt;/filter-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;filter-class&gt;org.apache.struts2.dispatcher.ActionContextCleanUp&lt;/filter-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/filter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;filter-mapping&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;filter-name&gt;struts-cleanup&lt;/filter-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/filter-mapping&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/web-app&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

这里web.xml多配置了一个ActionContextCleanUp的配置，这个类是一个filter,他的作用是方便strut2与sitemesh整合，与文件上传本没有关系，但不加载这个filter,可能在上传出出现莫名的异常，加入后就稳定了，可能是strut2的bug吧

文件上传工程页面：

 

![](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/None.gif)&lt;html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;head&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;br&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        文件标题:&lt;s:property value=" + title"/&gt;&lt;br&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        文件为：&lt;img src="&lt;s:property value="'upload/' + uploadFileName"/&gt;"/&gt;&lt;br&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)

 

运行，选择一个jpg图片，提交，就可以看到上传的页面显示出来了

通常，我们希望限定上传文件的类型，比如说只能上传图片，还需要进一步改造

首先改造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