struts2学习-文件上传和文件下载

这篇博客介绍了使用Struts2进行文件上传和下载的操作。在文件上传部分,展示了页面设计和struts2.xml的配置;在文件下载部分,强调了视图类型必须为stream,并给出了struts2.xml中相应的action配置。
摘要由CSDN通过智能技术生成

文件上传

                      1)三个条件:
                                        表单有file
                                        post提交
                                        enctype="multipart/form-data"

                      2)在Action中接收文件内容
                                        File attach;   (attach是file表单的name属性)
                                        String attachContentType;  文件类型
                                        String attachFileName;   文件名称
                      细节:
                                        修改上传大小

页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h2>request</h2>
<table border="1">
<c:forEach items="${requestScope.request_list}" var="user">
<tr>
    <td>${user}</td>
</tr>
</c:forEach>
</table>

<h2>session</h2>
<table border="1">
    <c:forEach items="${sessionScope.session_list}" var="user">
        <tr>
            <td>${user}</td>
        </tr>
    </c:forEach>
</table>

<h2>context</h2>
<table border="1">
    <c:forEach items="${applicationScope.context_list}" var="user">
        <tr>
            <td>${user}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

struts2.xml的配置

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="upload" extends="struts-default" namespace="/">
        <action name="upload" class="com.bin.action.UploadAction" method="upload">
            <param name="savePath">e:/images/</param>
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>

action:

package com.bin.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class UploadAction extends ActionSupport {
    //接收上传文件,名字来源于页面的File的name属性
    public File attach;
    //接收文件的类型
    public String attachContentType;
    //接收文件的名称
    public String attachFileName;
    //接收描述
    public String info;

    public String savePath;

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public File getAttach() {
        return attach;
    }

    public void setAttach(File attach) {
        this.attach = attach;
    }

    public String getAttachContentType() {
        return attachContentType;
    }

    public void setAttachContentType(String attachContentType) {
        this.attachContentType = attachContentType;
    }

    public String getAttachFileName() {
        return attachFileName;
    }

    public void setAttachFileName(String attachFileName) {
        this.attachFileName = attachFileName;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public  String upload() throws IOException {
        System.out.println(attach);
        System.out.println(attachContentType);
        System.out.println(attachFileName);
        System.out.println(info);

        FileUtils.copyFile(attach,new File(savePath+attachFileName));
        return SUCCESS;
    }
}

文件的下载

视图类型一定是stream类型
页面

<%--
  Created by IntelliJ IDEA.
  User: bin
  Date: 2020-07-02
  Time: 9:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"  isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table border="1" width="300xp">

<tr>
    <td>编号</td>
    <td>文件名称</td>
    <td>操作</td>
</tr>
<c:forEach items="${requestScope.list}" var="list" varStatus="downStu">
    <tr>
        <td>${downStu.count}</td>
        <td>${list}</td>
        <td><a href="${pageContext.request.contextPath}/down?name=${list}">下载</a></td>
    </tr>
</c:forEach>

</table>

</body>
</html>

struts。xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="down" extends="struts-default" namespace="/">
        <action name="downlist" class="com.bin.action.DownLoad" method="downLoad">
            <param name="downPath">e:/images/</param>
            <result name="list">downlist.jsp</result>
        </action>

        <action name="down" class="com.bin.action.DownLoad" method="down">
            <param name="downPath">e:/images/</param>
            <result name="down" type="stream">
                <!--  往StreamResult类中的属性注入内容 -->
                <!-- 返回给浏览器的文件类型。返回通用的二进制 -->
                <param name="contentType">application/octet-stream</param>
                <!-- 返回给浏览器的输入流 -->
                <param name="inputName">inputStream</param>
                <!--  告诉浏览器的方式下载资源
                  ${name}: 获取Action中的getName()方法的数据
                  -->
                <param name="contentDisposition">attachment;filename=${name}</param>
                <!-- 缓存大小 -->
                <param name="bufferSize">1024</param>
            </result>
        </action>
    </package>
</struts>

action:

package com.bin.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

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

public class DownLoad extends ActionSupport {
    private String downPath;
    private String name;

    public String getName() {
        return name;
    }

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

    public String getDownPath() {
        return downPath;
    }

    public void setDownPath(String downPath) {
        this.downPath = downPath;
    }

    public String downLoad(){

        File file =new File(downPath);
        String[] list = file.list();
        ActionContext requestMap = ActionContext.getContext();
        requestMap.put("list",list);
        return "list";
    }

    //给struts提供写出数据的输入流
    public InputStream getInputStream(){
        try {
            FileInputStream inputStream = new FileInputStream(new File(downPath + name));
            return inputStream;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public String down(){
        return "down";
    }



}

这是我自己写的一个小案例,如果又什么地方看不懂的话,欢迎随时提问,也可以加我的微信13283840906,只限小白,大神勿扰。在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值