Struts2学习(三)上传下载


今天记录一下利用struts2实现上传下载,借此案例说明一下struts2的开发流程。

须要注意的是struts2版本号不同非常多地方的写法是不同的。本例使用struts2.3.15 。有差别的地方文中会提到。下一篇具体说明下不同版本号差别 的地方。


  1. 建立web项目,导入必要jar包。替换web.xml。
    不同版本号标签内容有所不同
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

2.在src根文件夹手动建立struts.xml。加入头文件标签及action配置。
这里我们主要须要两个action:upoad和download。

<?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" /> <!-- 开发模式 --> <package name="up" namespace="/" extends="struts-default"> <default-action-ref name="index"></default-action-ref> <action name="index"> <result>/index.jsp</result> </action> <action name="upload" class="com.etoak.action.UploadAction"> <result> /index.jsp </result> </action> <action name="download" class="com.etoak.action.DownLoadAction"> <!-- 注意下载返回的不是一个字符串或跳转action,而是返回 一个流到本地。这里结果集调用stream流 --> <result type="stream"> <!-- action运行返回流的指定方法,注意此方法有我们自己书 写,不在使用返回String的execute方法处理业务逻辑了。 --> <param name="inputName">etoak</param> <!-- 文件保存方式 attachmen:使用附件形式 filename=:表示下载之后叫什么名字 ${filename}:表示下载后保持原名 --> <param name="contentDisposition"> attachment;filename=${filename} </param> </result> </action> </package> </struts>

3.建立action类。
UploadAction.java

package com.etoak.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
    private File myfile;
    private String myfileFileName;

    public String getMyfileFileName() {
        return myfileFileName;
    }

    public void setMyfileFileName(String myfileFileName) {
        this.myfileFileName = myfileFileName;
    }

    public File getMyfile() {
        return myfile;
    }

    public void setMyfile(File myfile) {
        this.myfile = myfile;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("hhh");
        String path = ServletActionContext.getServletContext().getRealPath("image")+"/"
                +this.getMyfileFileName();
        System.out.println(path);
        File desFile = new File(path);
        FileUtils.copyFile(this.getMyfile(), desFile);
        return this.SUCCESS;
    }

}

Download.java

package com.etoak.action;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
    private String filename;

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }
    public InputStream getEtoak() throws Exception{
        String path = ServletActionContext.getServletContext().getRealPath("image")
                +"/"+this.getFilename();
        return new FileInputStream(path);

    }
}

4.改动index.jsp页面
注意要引入struts标签库

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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></title>
  </head>
  <body>
    <s:form action="upload" enctype="multipart/form-data" method="post" >
        <s:file name="myfile" tooltip="" label="上传文件" ></s:file>
        <s:submit value="上传"></s:submit>
    </s:form>
    <hr>
    <s:form action="download" enctype="mutipart/form-data">
        <s:textfield name="filename" label="请输入文件名称"></s:textfield>
        <s:submit value="下载"></s:submit>
    </s:form>
  </body>
</html>
原文地址:https://www.cnblogs.com/gccbuaa/p/7060496.html