Struts2入门(七)——Struts2的文件上传和下载

一、前言

在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载。

注意:文件上传时,我们需要将表单提交方式设置为"POST"方式,并且将enctype属性设置为"multipart/form-data",该属性的默认值为"application/x-www-form-urlencoded",就是说,表单要写成以下这种形式:

<form action="" method="post" enctype="multipart/form-data"></form>

而且Struts2中并没有提供自己的文件上传解析器,默认使用的是Jakarta的Common-FileUpload的文件上传组件,所以我们还需要在添加两个包:

commons-io

commons-fileupload

至于版本根据自己需要选择(笔者在第一篇已经搭建好环境了。地址)

注意点如下:

1.1、文件上传的前提是表单属性method="post" enctype="multipart/form-data";

1.2、web应用中必须包含common-fileupload.jar和common-io.jar,因为struts2默认上传解析器使用的是jakarta;

1.3、可以在struts.xml中配置最大允许上传的文件大小:<constant name="struts.multipart.maxSize" value="....."/>,默认为2M;

二、文件上传案例

2.1、在Action中定义属性:

private File upload;                //包含文件内容

private String uploadFileName;       //上传文件的名称;

private String uploadContentType;    //上传文件的MIME类型;

这些属性都会随着文件的上传自动赋值;

2.2、上传图片的例子

新建视图界面:Upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts2 FileUpload</title>
</head>
<body>
    <form action="fileupload" method="post" enctype="multipart/form-data">
        文件标题:<input type="text" name="title"/><br/>
        选择文件:<input type="file" name="upload"/><br/>
        <input type="submit" value="上传"/>
    </form>
</body>
</html>

新建UploadAction继承ActionSupport

package com.Struts2.load;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

//上传单个文件
public class UploadAction extends ActionSupport {
    //文件标题请求参数的属性
    private String title;
    //上传文件域的属性
    private File upload;
    //上传文件类型
    private String uploadContentType;
    //上传文件名
    private String uploadFileName;
    //接受依赖注入的属性
    private String savePath;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    //返回上传文件的保存位置
    public String getSavePath() {
        return ServletActionContext.getRequest().getRealPath(savePath);
    }

    //接受依赖注入的方法
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public String execute() throws Exception{
        System.out.println(getSavePath());
        System.out.println(getUploadFileName());
        //以服务器的文件保存地址和原文件名建立上传文件输出流
        FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadFileName());

        //以上传文件建立一个文件上传流
        FileInputStream fis = new FileInputStream(getUpload());

        //将上传文件的内容写入服务器
        byte[] buffer = new byte[1024];
        int leng = 0;
        while((leng=fis.read(buffer))>0){
            fos.write(buffer,0,leng);
            buffer = new byte[1024];
        }
        return SUCCESS;
    }
}

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="default" extends="struts-default">
        <action name="fileupload" class="com.Struts2.load.UploadAction">
            <!--使用拦截器过滤:1、配置默认拦截器,2、配置input的逻辑视图-->
            <interceptor-ref name="fileUpload">
                <param name="allowedTypes">image/jpeg,image/jpg,/image/gif,image/png</param>
            </interceptor-ref>
            <!-- 必须显示配置defaultStack拦截器的引用 -->
            <interceptor-ref name="defaultStack"/>
            <param name="savePath">/upload</param>
            <result name="success">succ.jsp</result>
            <!--必须配置input的逻辑视图  -->
            <result name="input">Error.jsp</result>
        </action>
<struts>

注意:web.xml中需要配置struts2的拦截器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>LearStruts2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <!--为Struts2定义一个过滤器  -->
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
          org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

web.xml

succ.jsp视图代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传成功</title>
</head>
<body>
    上传成功<br/>
    文件标题:<s:property value="title"/><br/>
    <s:property value="uploadFileName"/>
    文件为:<img src="<s:property value="‘/LearStruts2/upload/‘+uploadFileName"/>"/><br/>
</body>
</html>

Error.jsp视图代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error 界面</title>
</head>
<body>
    <s:fielderror/>
</body>
</html>

Error.jsp

代码效果如下:

注意:在struts.xml配置文件中,已经限制只能上传图片格式,如果上传别的文件的话,则会报错。

2.3、多个文件上传

新建Upliads.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传多个文件</title>
</head>
<body>
    <form action="filesupload" method="post" enctype="multipart/form-data">
        文件标题:<input type="text" name="title"><br/>
        选择第一个文件:<input type="file" name="uploads"><br/>
        选择第二个文件:<input type="file" name="uploads"><br/>
        选择第三个文件:<input type="file" name="uploads"><br/>
        <input type="submit" value="上传"/>
    </form>
</body>
</html>

Upliads.jsp

新建UploadsAction类继承ActionSupport

package com.Struts2.load;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

//上传多个文件
public class UploadsAction extends ActionSupport {
    private String title;    //对应jsp的title
    private File[] uploads;    //对应jsp的uploads
    private String[] uploadsContentType;
    private String[] uploadsFileName;
    //savePath:通过配置文件进行赋值"‘\‘upload",
    //其中的‘\‘表示项目的根目录D:\\tomcat-8.0\\wtpwebapps\\LearStruts2\\upload
    private String savePath;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public File[] getUploads() {
        return uploads;
    }
    public void setUploads(File[] upload) {
        this.uploads = upload;
    }
    public String[] getUploadsContentType() {
        return uploadsContentType;
    }
    public void setUploadsContentType(String[] uploadsContentType) {
        this.uploadsContentType = uploadsContentType;
    }
    public String[] getUploadsFileName() {
        return uploadsFileName;
    }
    public void setUploadsFileName(String[] uploadsFileName) {
        this.uploadsFileName = uploadsFileName;
    }
    public String getSavePath() {
        return ServletActionContext.getRequest().getRealPath(savePath);
    }
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
    public String execute() throws Exception{
        File[] files = getUploads();
        for(int i = 0;i<files.length;i++){
            System.out.println(getSavePath());
            System.out.println(getUploadsFileName()[i]);
            //getSavePath : 获得根目录
            //getUploadsFileName() : 获得文件名
            FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadsFileName()[i]);

            FileInputStream fis = new FileInputStream(files[i]);

            byte[] buffer = new byte[1024];

            int len = 0;

            while((len=fis.read(buffer))>0){
                fos.write(buffer,0,len);
                buffer = new byte[1024];
            }
        }
        return SUCCESS;
    }
}

struts.xml中配置信息

        <!--多个文件上传  -->
        <action name="filesupload" class="com.Struts2.load.UploadsAction">
            <!--该属性是依赖注入:通过配置文件给savePath赋值 ,是必须的 -->
            <param name="savePath">/upload</param>
            <result name="success">succ1.jsp</result>
        </action>
        

succ1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>多个文件上传</title>
</head>
<body>
    上传成功
</body>
</html>

succ1.jsp

代码效果如下:

2.3、Struts2的文件下载

视图下载界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts 2实现的文件下载</title>
</head>
<body>
    <a href="down.action">图片下载</a>
</body>
</html>

fuledown.jsp

Action类

package com.Struts2.load;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownAction extends ActionSupport {
    private String inputPath;
    private String contentType;
    private String filename;

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getInputPath() {
        return inputPath;
    }

    public void setInputPath(String inputPath) {
        this.inputPath = inputPath;
    }

     //下载用的Action应该返回一个InputStream实例
     //该方法对应在result里的inputName属性值为targetFile
    //第二步
    public InputStream getTargetFile() throws Exception{
            System.out.println("1");
            InputStream in=ServletActionContext.getServletContext().getResourceAsStream(inputPath);
           return in;
    }
    //第一步
    public String execute(){
         System.out.println("???");
         inputPath="/upload/1.jpg";//要下载的文件名称
         filename="1.jpg"; //保存文件时的名称
         contentType="image/jpg";//保存文件的类型
        return SUCCESS;
    }
}

Struts.xml配置文件

     <!-- 下载文件的Action -->
        <action name="down" class="com.Struts2.load.DownAction">
          <!-- 指定被下载资源的位置 -->
          <param name="inputPath">${inputPath}</param>

          <!-- 配置结果类型为stream的结果 -->
          <result name="success" type="stream">
             <!--
                 contentType:指定下载文件的类型 ,和互联网MIME标准中的规定类型一致,
                 例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片
             -->
             <param name="contentType">${contentType}</param>
             <!-- 指定下载文件的位置 -->
             <!--
             inputName:下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,
                                       例如取值为inputStream的属性需要编写getInputStream()方法
             -->
             <param name="inputName">targetFile</param>
             <!--
             contentDisposition
                 文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,
               而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。
              默认情况是代表inline,浏览器会尝试自动打开它
               -->
             <param name="contentDisposition">attachement;filename="${filename}"</param>
             <!-- 指定下载文件的缓冲大小 -->
             <param name="bufferSize">50000000</param>
          </result>
       </action>

代码都有笔者测试过,至于解析,笔者会在后期理解好之后再重新写。

时间: 2024-08-06 03:21:57

Struts2入门(七)——Struts2的文件上传和下载的相关文章

Struts2学习(八)—文件上传和下载

在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来比较繁琐,而且不灵活,在学习了struts2后,struts2为文件上传下载提供了更好的实现机制,在这里我分别就单文件上传和多文件上传的实现进行一下讲解,这里 我们使用的struts2 web项目所导入的jar包中的**commons-fileupload-1.3.1.jar commons-io-

七牛云的文件上传和下载

1.本篇博客参考网址 https://www.cnblogs.com/xiaoBlog2016/p/9041308.html https://blog.csdn.net/peaceful000/article/details/53171578 https://blog.csdn.net/albertfly/article/details/51499812 2.在pom.xml中添加需要的jar <!--七牛云上传图片服务--> <!-- https://mvnrepository.com

struts2中的文件上传和下载

天下大事,必做于细.天下难事,必作于易. 曾经见过某些人,基础的知识还不扎实就去学习更难的事,这样必然在学习新的知识会很迷惑结果 再回来重新学习一下没有搞懂的知识,这必然会导致学习效率的下降!我写的这篇上传和下载都很基础. 十分适合初学者! jsp:页面 <!--在进行文件上传时,表单提交方式一定要是post的方式,因为文件上传时二进制文件可能会很大,还有就是enctype属性,这个属性一定要写成multipart/form-data, 不然就会以二进制文本上传到服务器端--> <for

Struts2学习总结——文件上传与下载

Struts2文件上传与下载 1.1.1新建一个Maven项目(demo02) 在此添加Web构面以及 struts2 构面 1.2.1配置Maven依赖(pom.xml 文件) <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20

struts2学习(14)struts2文件上传和下载(4)多个文件上传和下载

四.多个文件上传: 五.struts2文件下载: 多个文件上传action com.cy.action.FilesUploadAction.java: package com.cy.action; import java.io.File; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class FilesUploadAction extends Actio

Struts2(二)文件上传和下载

struts2文件上传和下载 1.创建一个index.jsp界面 和success.jsp上传成功界面 index.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <body> <s:form action="files/add" method="post" enctype="multipart/form-data"> &l

Spring Boot入门——文件上传与下载

Spring Boot入门--文件上传与下载https://www.cnblogs.com/studyDetail/p/7003253.html 1.在pom.xml文件中添加依赖 复制代码 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:/

Python入门学习-DAY32-链接循环与通信循环,粘包问题,远程控制,文件上传与下载

链接循环与通信循环 服务端 from socket import * IP = '127.0.0.1' PORT = 8181 ADDRESS = (IP, PORT) BUFSIZE = 1024 server = socket(AF_INET, SOCK_STREAM) server.bind(ADDRESS) server.listen(5) tag=True while tag: conn, addr = server.accept() while tag: try: data = co

springmvc和servlet下的文件上传和下载(存文件目录和存数据库Blob两种方式)

项目中涉及了文件的上传和下载,以前在struts2下做过,今天又用springmvc做了一遍,发现springmvc封装的特别好,基本不用几行代码就完成了,下面把代码贴出来: FileUpAndDown.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"%> <html> <head> <title>using commons Uplo

5文件上传与下载

java领域有两个常用的文件上传项目:Common-FileUpload和COS.struts2则在原来的文件上传的项目基础上,进行进一步的封装,从而进一步地简化了文件上传.除此之外,struts2对文件下载支持stream的结果类型,通过借助于struts2提供的文件下载支持,应用可以实现非西欧字符文件名的文件下载,并可以在文件下载前检查用户的权限,从而通过授权控制来控制文件的下载. 文件上传原理 表单元素的enctype属性 大部分时候,无需设置表单元素的enctype属性,我们只设置表单的