首页 > 代码库 > Struts2文件的下载

Struts2文件的下载

1、下载登录页面download.jsp

  1: <%@ page language="java" contentType="text/html; charset=UTF-8"
  2:     pageEncoding="UTF-8"%>
  3: <%@ taglib prefix="s" uri="/struts-tags" %>
  4: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5: <html>
  6:   <head>
  7:     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8:     <title>下载页面</title>
  9:   </head>
 10:   <body>
 11:     下载前的登录页面
 12:     <s:form action="download1">
 13:       <s:textfield name="user" label="用户名"/>
 14:       <s:textfield name="pass" label="密    码"/>
 15:       <s:submit value="http://www.mamicode.com/下载"/>
 16:     </s:form>
 17:   </body>
 18: </html>

2、FileDownAction.java

  1: package cc.openhome;
  2: 
  3: import java.io.File;
  4: import java.io.FileInputStream;
  5: import java.io.InputStream;
  6: import com.opensymphony.xwork2.Action;
  7: import com.opensymphony.xwork2.ActionSupport;
  8: public class FileDownAction extends ActionSupport implements Action {
  9:   private InputStream fileInputStream;
 10:   private String fileName;
 11:   private String user;
 12:   private String pass;
 13:   
 14:   public String getUser() {
 15:     return user;
 16:   }
 17:   public void setUser(String user) {
 18:     this.user = user;
 19:   }
 20:   public String getPass() {
 21:     return pass;
 22:   }
 23:   public void setPass(String pass) {
 24:     this.pass = pass;
 25:   }
 26:   public InputStream getFileInputStream() {
 27:     return fileInputStream;
 28:   }
 29:   public void setFileInputStream(InputStream fileInputStream) {
 30:     this.fileInputStream = fileInputStream;
 31:   }
 32:   public String getFileName() {
 33:     return fileName;
 34:   }
 35:   public void setFileName(String fileName) {
 36:     this.fileName = fileName;
 37:   }
 38:   @Override
 39:   public String execute() throws Exception {
 40:     // TODO Auto-generated method stub
 41:     if(getUser().equals("nihao") && getPass().equals("nihao")){
 42:       File file = new File("E:/java web/workspace/struts-fileupload/WebContent/images/struts-gif.rar");
 43:       fileName=file.getName();
 44:       fileInputStream=new FileInputStream(file);
 45:       return SUCCESS;
 46:     }else{
 47:       return ERROR;
 48:     }
 49:   }
 50:   public void validate(){
 51:     if(getUser()==null || getUser().trim().equals("")){
 52:       addFieldError("user", getText("user.required"));
 53:     }
 54:     if(getPass()==null || getPass().trim().equals("")){
 55:       addFieldError("pass", getText("pass.required"));
 56:     }
 57:   }
 58:   
 59: }
 60: 

3、struts.xml

  1: <?xml version="1.0" encoding="UTF-8" ?>
  2: <!DOCTYPE struts PUBLIC
  3:   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4:   "http://struts.apache.org/dtds/struts-2.3.dtd">
  5: 
  6: <struts>   
  7:   <constant name="struts.custom.i18n.resources" value="http://www.mamicode.com/message"/>
  8:   <package name="struts2" extends="struts-default">
  9:     <action name="download1" class="cc.openhome.FileDownAction">
 10:       <param name="inputPath">\images\struts-gif.rar</param>
 11:       <result name="success" type="stream">
 12:         <param name="contentType">application/octet-stream</param>
 13:         <param name="inputName">fileInputStream</param>
 14:         <param name="contentDisposition">acttachment;filename="${ fileName}"</param>
 15:         <param name="bufferSize">4096</param>
 16:       </result>
 17:       <result name="input">/input.jsp</result>
 18:     </action>
 19:    </package>
 20: </struts>  

4、运行结果

             技术分享    技术分享

Struts2文件的下载