struts2學習-文件上傳和文件下載

文件上傳

                      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,只限小白,大神勿擾。在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章