struts2完美实现文件上传和下载

                                  ## struts2完美实现文件上传和下载 ##

第一步:在webx项目lib目录下加入struts.jar包
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
freemarker-2.3.22.jar
javassist-3.11.0.GA.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
ognl-3.0.6.jar
struts2-core-2.3.24.1.jar
struts2-dojo-plugin-2.3.24.1.jar
xwork-core-2.3.24.1.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_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Struts08</display-name>
    <!-- struts2.配置-->
    <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>*.action</url-pattern>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    <!-- struts2配置-->

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>`

第三步配置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.i18n.encoding" value="utf-8" />
    <!-- 设置自动装载国际化 -->
    <constant name="struts.i18n.reload" value="true" />
    <!-- 设置动态方法调用 -->
    <constant name="struts.enable.DnamicMethodInvocation" value="true" />
    <!-- 设置开发模式 -->
    <constant name="struts.devMode" value="true" />
    <!-- 指定每次配置文件更改后,自动重新加载 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 设置访问方式 -->
    <constant name="struts.action.extension" value="action,," />
    <!-- 去除多余的格式 -->
    <constant name="struts.ui.theme" value="simple" />
    <!--设置上传文件大小20MB,默认为2MB大小 -->
    <constant name="struts.multipart.maxSize" value="20971520"></constant>

    <package name="default" extends="struts-default" namespace="/">
       <!-- 文件上传配置 -->
        <action name="m" class="com.action.UploadManyFile">
            <!-- 文件上传到user目录下-->
            <param name="path">/user</param>
            <param name="size">10240000</param>
        </action>
        <!-- 文件下载配置 -->
        <action name="down" class="com.action.FileDownloadAction">
        <param name="path">/user</param>
        <result name="success" type="stream">
        <param name="contentType">application/octet-stream</param>
        <param name="inputName">inputStream</param> 
        <param name="inputName">inputStream</param>
        <param name="contentDisposition">filename=${fn}</param>
        <param name="bufferSize">4096</param>
        </result>
        </action>
    </package>

</struts>

第四步jsp写入代码

<%@ page language="java" pageEncoding="UTF8"%>
<!Doctype html>
<html >
<head>
    <meta charset="UTF-8" />
    <title>struts2 多文件上传</title>
</head>
<body>
    <h1>struts-多文件 上传</h1>
    <form action="m.action" method="post" enctype="multipart/form-data">
    姓名:<input type="text" name="name" value="张三"><br>
    文件:<input type="file" name="fs"  multiple ><br>
    文件1:<input type="file" name="fs"  multiple ><br>
    文件2:<input type="file" name="fs"  multiple ><br>
    文件3:<input type="file" name="fs"  multiple ><br>
            <input type="submit"   value="上传"><br>
    </form>
    <hr>
    <h1>文件下载</h1>
     <a href="user/abc.jpg">下载</a>
     <a href="user/abc.rar">下载zip</a>
     <a href="down.action?fn=abc.rar">struts2文件下载</a>
</body>
</html>

第四步在src目录下建立com.action包建立UploadManyFile

package com.action;

import java.io.File;

import javax.servlet.ServletContext;

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

import com.opensymphony.xwork2.ActionSupport;

public class UploadManyFile  extends ActionSupport implements ServletContextAware{
    private String name;// 
    private File[] fs; //文件名
    private String [] fsFileName;
    private String[] fsContentType;//文件类型

    private String path;//文件路径
    private  int   size; //文件大小
    private ServletContext  ctx;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public File[] getFs() {
        return fs;
    }

    public void setFs(File[] fs) {
        this.fs = fs;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String[] getFsFileName() {
        return fsFileName;
    }

    public void setFsFileName(String[] fsFileName) {
        this.fsFileName = fsFileName;
    }

    public String[] getFsContentType() {
        return fsContentType;
    }

    public void setFsContentType(String[] fsContentType) {
        this.fsContentType = fsContentType;
    }

    @Override
    public void setServletContext(ServletContext  ctx) {
               this.ctx=ctx;    
    }

    @Override
    public String execute() throws Exception {
          String p=ctx.getRealPath(path);
           File f=new File(p);
           if(!f.exists()){
               f.mkdirs();
           }
           for(int i=0;i<fs.length;i++){
            System.out.println(path);
            System.out.println(p);
            System.out.println(size);
            System.out.println(fs[i].length());
            System.out.println("------------");

            /* 
             * 如果文件大于上传限定值
             */
            if(fs[i].length()<size){
                FileUtils.copyFile(fs[i],new File(f,fsFileName[i]));
            }
           }
        return null;
    }

}

第五步建立FileDownloadAction

package com.action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class FileDownloadAction implements Action {
    private String fn;  //文件名
    private String path;  //路径

    public String getFn() {
        return fn;
    }

    public void setFn(String fn) {
        this.fn = fn;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    public InputStream getInputStream() throws Exception{
        //获得路径和文件名
        String ip=ServletActionContext.getServletContext().getRealPath(path)+"/"+fn;
        InputStream is=new FileInputStream(ip);
        return is;
    }
}

第六步输入项目名称运行

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值