Struts 2多文件上传

很多应用都要求在一个界面中可以上传多个文件,Struts 2框架也可以方便地支持多个文件同时上传,一般可以使用数组和List来实现。

使用数组实现上传多个文件:

1upload.jsp文件,代码11.5所示。

<%@ page 

    language="java" 

    contentType="text/html; charset=UTF-8" 

    pageEncoding="UTF-8"%> 

<%@ taglib prefix="s" uri="/struts-tags" %> 

<html> 

<head> 

<title>使用数组实现多个文件上传</title> 

</head> 

<body> 

<s:form action="doMultipleUploadUsingArray" method="POST" enctype="multipart/form-data"> 

    <s:file label="文件1" name="upload" /> 

    <s:file label="文件2" name="upload" /> 

    <s:file label="文件3" name="upload" /> 

    <s:submit value="上传"/>   

</s:form> 

</body> 

</html> 

 (2)在该form中,定义了3个具有相同名称的文件域,所以需要在Action实现类中使用数组来封装这3个文件域。使用数组多个上传文件的业务控制器如代码

package ch11; 

import java.io.File; 

import java.text.DateFormat; 

import java.text.SimpleDateFormat; 

import java.util.Date; 

import java.util.Random; 

import org.apache.commons.io.FileUtils; 

import org.apache.struts2.ServletActionContext; 

import com.opensymphony.xwork2.ActionSupport; 

public class MultipleFileUploadUsingArrayAction extends ActionSupport { 

    private File[] uploads; 

    private String[] uploadFileNames; 

    private String[] uploadContentTypes; 

    private String[] dir; 

    private String[] targetFileName; 

    //属性的getter和setter方法 

    public File[] getUpload() { 

        return this.uploads; 

    } 

    public void setUpload(File[] upload) { 

        this.uploads = upload; 

    } 

    public String[] getUploadFileName() { 

        return this.uploadFileNames; 

    } 

    public void setUploadFileName(String[] uploadFileName) { 

        this.uploadFileNames = uploadFileName; 

    } 

    public String[] getUploadContentType() { 

        return this.uploadContentTypes; 

    } 

    public void setUploadContentType(String[] uploadContentType) { 

        this.uploadContentTypes = uploadContentType; 

    } 

    //文件上传 

    public String upload() throws Exception { 

        // 获得upload路径的实际目录 

        String realPath = ServletActionContext.getRequest().getRealPath( 

                "/upload"); 

        //获得实际目录 

        String targetDirectory = realPath; 

        String[] mydir = new String[uploads.length]; 

        String[] tname = new String[uploads.length]; 

        for (int i = 0; i < uploads.length; i++) { 

            // 生成保存文件的文件名称 

            tname[i] = generateFileName(uploadFileNames[i]); 

            // 保存文件的路径 

            mydir[i] = targetDirectory + "\\" + tname[i]; 

            // 建立一个目标文件 

            File target = new File(targetDirectory, tname[i]); 

            // 将临时文件复制到目标文件 

            FileUtils.copyFile(uploads[i], target); 

        } 

        setDir(mydir); 

        setTargetFileName(tname); 

        return SUCCESS; 

    } 

    // 为上传文件自动分配文件名称,避免重复 

    private String generateFileName(String fileName) { 

        // 获得当前时间 

        DateFormat format = new SimpleDateFormat("yyMMddHHmmss"); 

        // 转换为字符串 

        String formatDate = format.format(new Date()); 

        // 随机生成文件编号 

        int random = new Random().nextInt(10000); 

        // 获得文件后缀名称 

        int position = fileName.lastIndexOf("."); 

        String extension = fileName.substring(position); 

        // 组成一个新的文件名称 

        return formatDate + random + extension; 

    } 

    //属性的getter和setter方法 

    public String[] getDir() { 

        return dir; 

    } 

    public void setDir(String[] dir) { 

        this.dir = dir; 

    } 

    public String[] getTargetFileName() { 

        return targetFileName; 

    } 

    public void setTargetFileName(String[] targetFileName) { 

        this.targetFileName = targetFileName; 

    } 

} 

 

使用数组上传多个文件同上传单个文件非常类似,并不复杂。

3)在配置文件中增加如下配置:

<action name="doMultipleUploadUsingArray" 

            class="ch11.MultipleFileUploadUsingArrayAction" 

            method="upload"> 

            <result>/ch11/multipleUploadUsingArray-success.jsp</result> 

            <result name="input">/ch11/multipleUploadUsingArray.jsp</result> 

</action> 

 

 4multipleUploadUsingArray-success.jsp的内容如代码

<%@ page 

    language="java" 

    contentType="text/html; charset=UTF-8" 

    pageEncoding="UTF-8"%> 

<%@ taglib prefix="s" uri="/struts-tags" %> 

<html> 

<head> 

<title>多个文件上传示例</title> 

</head> 

<body> 

<table border="1"> 

<!--  使用迭代标签--> 

<s:iterator value="dir" status="stat"> 

<tr> 

    <td>文件名称<s:property value="%{#stat.index}" /></td> 

    <td><s:property value="%{dir[#stat.index]}" /></td> 

</tr>   

</s:iterator> 

</table> 

<table border="1"> 

<!--  使用迭代标签--> 

<s:iterator value="uploadContentType" status="stat"> 

<tr> 

    <td>文件类型<s:property value="%{#stat.index}" /></td> 

    <td><s:property value="%{uploadContentType[#stat.index]}" /></td> 

</tr> 

</s:iterator> 

</table> 

<table border="1"> 

<!--  使用迭代标签--> 

<s:iterator value="+targetFileName" status="stat"> 

<tr> 

    <td>图片<s:property value="%{#stat.index}" /></td> 

    <td><img src="<s:property value="'/bookcode/upload/'+targetFileName[#stat.index]"/>"/></td> 

</tr> 

</s:iterator> 

</table> 

</body> 

</html> 

 运行该示例,在浏览器中输入http://localhost:8080/TestS/multipleUpload UsingArray.jsp,界面如图

 

 

 

 

 



 

 

 6)在这里,选择3gif文件,单击“上传”按钮,结果如图



 

使用List上传多个文件:

使用List来上传多个文件同数组基本相同,上传JSP文件和success逻辑视图的JSP文件均不用修改,只是需要相应的Action实现类即可。

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值