<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
<property name="dist" value="classes"/>
<property name="src" value="src"/>
<path id="classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${dist}"/>
</path>
<target name="compile" description="Compile all source code">
<delete dir="${dist}"/>
<mkdir dir="${dist}"/>
<copy todir="${dist}">
<fileset dir="${src}">
<exclude name="**/*.java"/>
</fileset>
</copy>
<javac destdir="classes" debug="true" includeantruntime="yes"
deprecation="false" optimize="false" failonerror="true">
<src path="${src}"/>
<classpath refid="classpath"/>
</javac>
</target>
</project>
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<!-- 定义Struts 2的核心Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 让Struts 2的核心Filter拦截所有请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author yeeku.H.lee kongyeeku@163.com
version 1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date:
--%>
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br/>
文件标题:<s:property value=" + title"/><br/>
文件为:<img src="<s:property value="'uploadFiles/'
+ uploadFileName"/>"/><br/>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author yeeku.H.lee kongyeeku@163.com
version 1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date:
--%>
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>利用拦截器实现文件过滤</title>
<s:head/>
</head>
<body>
<s:form action="upload"
enctype="multipart/form-data">
<s:textfield name="title" label="文件标题"/>
<s:file name="upload" label="选择文件"/>
<s:submit value="上传"/>
</s:form>
</body>
</html>
#改变文件类型不允许的提示信息 struts.messages.error.content.type.not.allowed=您上传的文件类型只能是图片文件! 请重新选择! #改变上传文件太大的提示信息 struts.messages.error.file.too.large=您要上传的文件太大,请重新选择!
#\u6539\u53d8\u6587\u4ef6\u7c7b\u578b\u4e0d\u5141\u8bb8\u7684\u63d0\u793a\u4fe1\u606f struts.messages.error.content.type.not.allowed=\u60a8\u4e0a\u4f20\u7684\u6587\u4ef6\u7c7b\u578b\u53ea\u80fd\u662f\u56fe\u7247\u6587\u4ef6\uff01 \u8bf7\u91cd\u65b0\u9009\u62e9\uff01 #\u6539\u53d8\u4e0a\u4f20\u6587\u4ef6\u592a\u5927\u7684\u63d0\u793a\u4fe1\u606f struts.messages.error.file.too.large=\u60a8\u8981\u4e0a\u4f20\u7684\u6587\u4ef6\u592a\u5927\uff0c\u8bf7\u91cd\u65b0\u9009\u62e9\uff01
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="mess"/>
<!-- 设置该应用使用的字符集 -->
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<!-- 配置处理文件上传的Action -->
<action name="upload" class="org.crazyit.app.action.UploadAction">
<!-- 配置fileUpload的拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型 -->
<param name="allowedTypes">image/png
,image/gif,image/jpeg</param>
<!-- 配置允许上传的文件大小 -->
<param name="maximumSize">2000</param>
</interceptor-ref>
<!-- 配置系统默认的拦截器 -->
<interceptor-ref name="defaultStack"/>
<!-- 动态设置Action的属性值 -->
<param name="savePath">/uploadFiles</param>
<!-- 配置Struts 2默认的视图页面 -->
<result>/WEB-INF/content/succ.jsp</result>
<result name="input">/WEB-INF/content/uploadForm.jsp</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
package org.crazyit.app.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class UploadAction extends ActionSupport
{
//封装文件标题请求参数的属性
private String title;
//封装上传文件域的属性
private File upload;
//封装上传文件类型的属性
private String uploadContentType;
//封装上传文件名的属性
private String uploadFileName;
//直接在struts.xml文件中配置的属性
private String savePath;
//接受struts.xml文件配置值的方法
public void setSavePath(String value)
{
this.savePath = value;
}
//返回上传文件的保存位置
private String getSavePath() throws Exception
{
return ServletActionContext.getServletContext()
.getRealPath(savePath);
}
//文件标题的setter和getter方法
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return (this.title);
}
//上传文件对应文件内容的setter和getter方法
public void setUpload(File upload)
{
this.upload = upload;
}
public File getUpload()
{
return (this.upload);
}
//上传文件的文件类型的setter和getter方法
public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public String getUploadContentType()
{
return (this.uploadContentType);
}
//上传文件的文件名的setter和getter方法
public void setUploadFileName(String uploadFileName)
{
this.uploadFileName = uploadFileName;
}
public String getUploadFileName()
{
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
{
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath()
+ "\\" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
fos.close();
return SUCCESS;
}
}
来源:https://www.cnblogs.com/tszr/p/12366797.html