Struts上传、下载功能结合
/20171105_shiyan_upanddown/src/nuc/sw/action/DocDownloadAction.java
package nuc.sw.action;import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class DocDownloadAction extends ActionSupport { private String downPath;public InputStream getInputStream () throws Exception{ downPath = new String(downPath.getBytes("ISO8859-1" ),"utf-8" );return ServletActionContext.getServletContext().getResourceAsStream(downPath);}public String getDownPath (){return downPath;}public void setDownPath (String downPath){this .downPath=downPath;}public String getDownloadFileName (){String downFileName=downPath.substring(7 );try {downFileName = new String(downFileName.getBytes("iso8859-1" ),"utf-8" );}catch (Exception e){e.printStackTrace();}return downFileName;}@Override public String execute () throws Exception { return SUCCESS;} }
/20171105_shiyan_upanddown/src/nuc/sw/action/DocUploadAction.java
package nuc.sw.action;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class DocUploadAction extends ActionSupport {private String name;private File[] upload;private String[] uploadContentType;private String[] uploadFileName;private String savePath;private Date createTime;public String getName () {return name;}public void setName (String name) {this .name = name;}public File[] getUpload () {return upload;}public void setUpload (File[] upload) {this .upload = upload;}public String[] getUploadContentType () {return uploadContentType;}public void setUploadContentType (String[] uploadContentType) {this .uploadContentType = uploadContentType;}public String[] getUploadFileName () {return uploadFileName;}public void setUploadFileName (String[] uploadFileName) {this .uploadFileName = uploadFileName;}public String getSavePath () {return savePath;}public void setSavePath (String savePath) {this .savePath = savePath;}public Date getCreateTime (){ createTime=new Date();return createTime;}public static void copy (File source,File target){ InputStream inputStream=null ;OutputStream outputStream=null ;try {inputStream=new BufferedInputStream(new FileInputStream(source));outputStream=new BufferedOutputStream(new FileOutputStream(target));byte [] buffer=new byte [1024 ];int length=0 ;while ((length=inputStream.read(buffer))>0 ){outputStream.write(buffer, 0 , length);} }catch (Exception e){e.printStackTrace();}finally {if (null !=inputStream){try {inputStream.close();} catch (IOException e2) {e2.printStackTrace();} }if (null !=outputStream){try {outputStream.close();}catch (Exception e2){e2.printStackTrace();} }} }@Override public String execute () throws Exception { for (int i=0 ;i<upload.length;i++){ String path=ServletActionContext.getServletContext().getRealPath(this .getSavePath())+"\\" +this .uploadFileName[i];File target=new File(path);copy(this .upload[i],target);}return SUCCESS;} }
/20171105_shiyan_upanddown/src/nuc/sw/action/LoginAction.java
package nuc.sw.action;import java.util.regex.Pattern;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {private String username;private String password;public String getUsername () {return username;}public void setUsername (String username) {this .username = username;}public String getPassword () {return password;}public void setPassword (String password) {this .password = password;}@Override public void validate () {if (username.trim().length()<6 ||username.trim().length()>15 ||username==null ) {this .addFieldError("username" , "用户名长度不合法!" );}if (password.trim().length()<6 ||password.trim().length()>15 ||password==null ) {this .addFieldError("password" , "密码长度不合法!" );} }public String loginMethod () {if (username.equals("chenghaoran" )&&password.equals("12345678" )) {ActionContext.getContext().getSession().put("user" , username);return "loginOK" ;}else {this .addFieldError("err" ,"用户名或密码不正确!" );return "loginFail" ;} }public void validateLoginMethod () {if (username==null ||username.trim().equals("" )) {this .addFieldError("username" ,"用户名不能为空!" );}else {if (!Pattern.matches("[a-zA-Z]{6,15}" , username.trim())) {this .addFieldError("username" , "用户名格式错误!" );} }if (password==null ||password.trim().equals("" )) {this .addFieldError("password" ,"密码不能为空!" );}else {if (!Pattern.matches("\\d{6,15}" , password.trim())) {this .addFieldError("password" , "密码格式错误!" );} }}
}
/20171105_shiyan_upanddown/src/nuc/sw/interceptor/LoginInterceptor.java
package nuc.sw.interceptor;import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class LoginInterceptor extends AbstractInterceptor {@Override public String intercept (ActionInvocation arg0) throws Exception {ActionContext ac=arg0.getInvocationContext();String username=(String)ac.getSession().get("user" );if (username!=null &&username.equals("chenghaoran" )) {return arg0.invoke();}else {((ActionSupport)arg0.getAction()).addActionError("请先登录!" );return Action.LOGIN;} }}
/20171105_shiyan_upanddown/src/struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts > <constant name ="struts.i18n.encoding" value ="utf-8" /> <package name ="default" extends ="struts-default" > <interceptors > <interceptor name ="login" class ="nuc.sw.interceptor.LoginInterceptor" > </interceptor > </interceptors > <action name ="docUpload" class ="nuc.sw.action.DocUploadAction" > <interceptor-ref name ="fileUpload" > <param name ="maximumSize" > 50000</param > </interceptor-ref > <interceptor-ref name ="defaultStack" /> <param name ="savePath" > /upload</param > <result > /showFile.jsp</result > <result name ="input" > /uploadFile.jsp</result > </action > <action name ="docDownload" class ="nuc.sw.action.DocDownloadAction" > <result type ="stream" > <param name ="contentType" > application/msword,text/plain</param > <param name ="inputName" > inputStream</param > <param name ="contentDisposition" > attachment;filename="${downloadFileName}"</param > <param name ="bufferSize" > 40960</param > </result > </action > <action name ="loginAction" class ="nuc.sw.action.LoginAction" method ="loginMethod" > <result name ="loginOK" > /uploadFile.jsp</result > <result name ="loginFail" > /login.jsp</result > <result name ="input" > /login.jsp</result > </action > </package >
</struts >
/20171105_shiyan_upanddown/WebContent/login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html >
<head >
<meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
<title > 登录页</title >
<s:head />
</head >
<body > <s:actionerror /> <s:fielderror fieldName ="err" > </s:fielderror > <s:form action ="loginAction" method ="post" > <s:textfield label ="用户名" name ="username" > </s:textfield > <s:password label ="密码" name ="password" > </s:password > <s:submit value ="登陆" > </s:submit > </s:form >
</body >
</html >
/20171105_shiyan_upanddown/WebContent/showFile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html >
<head >
<meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
<title > 显示上传文档</title >
</head >
<body >
<center > <font style ="font-size:18px;color:red" > 上传者:<s:property value ="name" /> </font > <table width ="45%" cellpadding ="0" cellspacing ="0" border ="1" > <tr > <th > 文件名称</th > <th > 上传者</th > <th > 上传时间</th > </tr > <s:iterator value ="uploadFileName" status ="st" var ="doc" > <tr > <td align ="center" > <a href ="docDownload.action?downPath=upload/<s:property value=" #doc" /> "><s:property value ="#doc" /> </a > </td > <td align ="center" > <s:property value ="name" /> </td > <td align ="center" > <s:date name ="createTime" format ="yyyy-MM-dd HH:mm:ss" /> </td > </tr > </s:iterator > </table >
</center >
</body >
</html >
/20171105_shiyan_upanddown/WebContent/uploadFile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html >
<head >
<meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
<title > 多文件上传 </title >
</head >
<body >
<center >
<s:form action ="docUpload" method ="post" enctype ="multipart/form-data" > <s:textfield name ="name" label ="姓名" size ="20" /> <s:file name ="upload" label ="选择文档" size ="20" /> <s:file name ="upload" label ="选择文档" size ="20" /> <s:file name ="upload" label ="选择文档" size ="20" /> <s:submit value ="确认上传" align ="center" />
</s:form >
</center >
</body >
</html >