Struts2_拦截器_文件上传
###一、
1.jar包:
(同上)
2.web.xml配置
(同上)
3.action编写
package cn.itcast.web.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import cn.itcast.utils.BaseAction;
public class UploadAction extends BaseAction{
private String paFileName;//页面传输过来的文件名
private String paContentType;//页面传输过来的文件类型
private String pa;//页面传输过来的文件内容
@Override
public String execute() throws Exception {
ServletContext application = ServletActionContext.getServletContext();
String realPath = application.getRealPath("upload");//根据开发逻辑路径,获取实际upload在服务器的部署路径
String uploadPath = realPath+"/"+paFileName;
InputStream in = new FileInputStream(pa);//读取数据
OutputStream out = new FileOutputStream(new File(uploadPath));//写入数据
//文件的复制:方法一
//IOUtils.copy(in, out);
//文件的复制:方法二
byte[] b = new byte[1024*5];
int count;
while((count=in.read(b))!=-1){
out.write(b, 0, count);//从0开始,到count结束
}
in.close();//流资源关闭
out.close();//
return SUCCESS;
}
public String getPaFileName() {
return paFileName;
}
public void setPaFileName(String paFileName) {
this.paFileName = paFileName;
}
public String getPaContentType() {
return paContentType;
}
public void setPaContentType(String paContentType) {
this.paContentType = paContentType;
}
public String getPa() {
return pa;
}
public void setPa(String pa) {
this.pa = pa;
}
}
4.struts.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!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.devMode" value="true"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="default" extends="struts-default" namespace="/">
<!-- 拦截器 -->
<interceptors>
<interceptor name="LoginInterceptor" class="cn.itcast.web.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="myInterceptor">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="LoginInterceptor"></interceptor-ref>
<interceptor-ref name="fileUpload">
<param name="maximumSize">2000000</param><!-- 设置文件上传的最大值 -->
<param name="allowedTypes">image/jpeg,image/png,image/gif</param><!-- 设置文件上传的类型 -->
<!-- <param name="allowedExtensions">.jpg,.png,.gif</param>设置文件上传的扩展后缀 -->
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 全局 -->
<global-results>
<result name="login">/login.jsp</result>
<result></result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<!-- ########################################################################### -->
<action name="userLogin" class="cn.itcast.web.action.UserLoginAction" method="execute">
<result type="redirectAction">list</result>
<result name="fail">/login.jsp</result>
</action>
<action name="list" class="cn.itcast.web.action.UserAction" method="execute">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<result>/list.jsp</result>
</action>
<action name="upload" class="cn.itcast.web.action.UploadAction" method="execute">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<result>/index.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
5.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'upload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<fieldset>
<form action="${pageContext.request.contextPath}/upload.action" enctype="multipart/form-data" method="post">
<s:debug></s:debug>
文件上传:<input type="file" name="pa"/><br>
<input type="submit" value="上传">
</form>
</fieldset>
</body>
</html>