JavaWeb中struts2实现文件上传下载功能实例解析

yizhihongxing

JavaWeb中struts2实现文件上传下载功能实例解析

在JavaWeb应用中,实现文件的上传下载是很常见的需求。而在struts2框架中,实现文件的上传下载也并不难。本文将详细介绍struts2框架中如何实现文件上传下载的功能。

1. 文件上传实现

在struts2框架中,文件上传的实现需要用到Commons-FileuploadCommons-Io两个Jar包。以下是文件上传的实现步骤:

  1. pom.xml中导入以下两个依赖:
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.2</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
  1. 在struts.xml中配置文件上传的interceptor和action:
<interceptors>
     <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
     <interceptor-stack name="myStack">
         <interceptor-ref name="defaultStack"/>
         <interceptor-ref name="fileUpload"/>
     </interceptor-stack>
</interceptors>
<action name="fileUploadAction" class="com.example.action.FileUploadAction" method="upload">
     <interceptor-ref name="myStack"></interceptor-ref>
     <result name="success">/success.jsp</result>
     <result name="error">/error.jsp</result>
</action>
  1. 在action类中实现upload()方法并处理文件上传:
public String upload() throws Exception {    
    List<File> fileUpload = new ArrayList<File>();
    List<String> fileUploadContentType = new ArrayList<String>();
    List<String> fileUploadFileName = new ArrayList<String>();

    for(int i=0; i<upload.size(); i++) {
        fileUpload.add(upload.get(i));                    
        fileUploadContentType.add(uploadContentType.get(i));
        fileUploadFileName.add(uploadFileName.get(i));
    }

    if(fileUpload != null && fileUpload.size() > 0) {
        for(int i=0; i<fileUpload.size(); i++) {
            File file = fileUpload.get(i); // 得到上传的文件
            String fileName = fileUploadFileName.get(i); // 得到上传的文件名
            InputStream is = new FileInputStream(file);
            OutputStream os = new FileOutputStream(new File("C:\\fileuploads\\" + fileName));
            byte[] buffer = new byte[1024];
            int length = 0;
            while((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);                    
            }
            os.close();
            is.close();
        }
    }

    return SUCCESS;        
}

2. 文件下载实现

实现文件的下载同样需要用到Commons-Io Jar包。以下是文件下载的实现步骤:

  1. 在action类中实现download()方法:
public String download() throws Exception {
    File file = new File("C:\\fileuploads\\" + fileName);
    InputStream is = new FileInputStream(file);
    ServletActionContext.getResponse().setContentType("application/octet-stream");
    fileName = new String(fileName.getBytes(), "ISO8859-1"); // 解决中文文件名下载问题
    ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + fileName);
    ServletOutputStream os = ServletActionContext.getResponse().getOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    while((len = is.read(buffer)) > 0) {
        os.write(buffer, 0, len);
    }
    is.close();
    os.close();
    return null;
}    
  1. 在struts.xml中配置download()方法的action:
<action name="download" class="com.example.action.FileUploadAction" method="download">
    <result name="success" type="stream">
        <param name="contentType">application/octet-stream</param>
        <param name="inputName">inputStream</param>
        <param name="contentDisposition">attachment;filename=${fileName}</param>
        <param name="bufferSize">1024</param>
    </result>
</action>

3. 示例

以下是一个简单的示例,首先是上传文件:

<form action="fileUploadAction.action" method="post" enctype="multipart/form-data">
    <input id="upload" name="upload" type="file" multiple="multiple"/> 
    <input type="submit" value="上传"/>
</form> 

上传成功后,可以在页面上看到上传的文件名,点击文件名可以下载上传的文件:

<a href="/download.action?fileName=${fileName}">${fileName}</a>

以上就是struts2中实现文件上传下载功能的完整攻略。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JavaWeb中struts2实现文件上传下载功能实例解析 - Python技术站

(0)
上一篇 2023年5月20日
下一篇 2023年5月20日

相关文章

  • springboot-2.3.x最新版源码阅读环境搭建(基于gradle构建)

    下面我将介绍如何搭建springboot-2.3.x最新版源码阅读环境(基于gradle构建)。 1. 准备工作 首先需要安装以下工具: JDK8+ Git Gradle IntelliJ IDEA 2. 下载源码 在Github上下载最新版的springboot源码。 $ git clone https://github.com/spring-projec…

    Java 2023年5月19日
    00
  • 线程同步的作用是什么?

    以下是关于线程同步的完整使用攻略: 线程同步的作用是什么? 线程同步是指多个线程之间的协作同步,以避出现数据不一致或者数据污染的问题。在多线程编程中,如果多个线程同时访问共享资源,就会出数据不一致或者数据染的问题,这时就需要使用线程同步来解决这个问题。 线程同步的作用主要有几个方面: 1. 避免数据不一致 在多线程编程中,如果多个线程同时访问共享资源,就出现…

    Java 2023年5月12日
    00
  • java日志打印的完全使用指南

    Java日志打印的完全使用指南 为什么需要日志打印 在开发Java应用程序时,我们经常需要记录程序的运行状态,以便于调试、性能分析和错误排查。日志打印就是一种记录应用程序运行状态的方式,它可以用来记录各种类型的信息,如调试信息、异常信息、警告信息、错误信息等。 使用日志打印有以下几个好处: 记录程序的运行状态,便于排查问题。 通过日志分析来评估代码的性能表现…

    Java 2023年5月26日
    00
  • springboot 如何配置多个jndi数据源

    下面是详细讲解: 配置多个JNDI数据源 在Spring Boot中,可以使用application.properties或application.yml文件配置多个JNDI数据源。下面详细说明如何实现操作。 添加依赖 首先,需要在pom.xml文件中添加JNDI的支持依赖,例如: <dependency> <groupId>org.…

    Java 2023年5月20日
    00
  • springmvc mybatis集成配置示例

    下面是关于“SpringMVC MyBatis集成配置示例”的完整攻略,包含两个示例说明。 SpringMVC MyBatis集成配置示例 SpringMVC和MyBatis是Java Web应用程序开发中常用的框架。在本文中,将介绍如何使用这两个框架来构建一个Web应用程序。 步骤1:添加依赖 首先,我们需要在pom.xml文件中添加SpringMVC和M…

    Java 2023年5月17日
    00
  • Spring boot创建自定义starter的完整步骤

    Spring Boot创建自定义starter的完整步骤 在Spring Boot中,我们可以创建自定义starter来封装一些常用的功能,以便于在其他项目中重复使用。本文将详细讲解Spring Boot创建自定义starter的完整步骤,并提供两个示例。 1. 创建starter项目 以下是创建starter项目的基本流程: 在IDEA中创建一个Maven…

    Java 2023年5月15日
    00
  • JavaServlet的文件上传和下载实现方法

    下面是详细讲解“JavaServlet的文件上传和下载实现方法”的完整攻略。 一、文件上传的实现 1. HTML页面的制作 我们需要先准备一个用于上传文件的HTML页面。 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit…

    Java 2023年6月15日
    00
  • 详解Spring 中 Bean 对象的存储和取出

    下面是详解Spring中Bean对象的存储和取出的完整攻略: 目录 1. Spring中Bean对象的存储 1.1 BeanFactory 1.2 ApplicationContext 2. Spring中Bean对象的取出 2.1 通过名称获取Bean对象 2.2 通过类型获取Bean对象 3. 示例说明 3.1 示例1:通过名称获取Bean对象 3.2 …

    Java 2023年5月26日
    00
合作推广
合作推广
分享本页
返回顶部