Struts2上传多个文件例子
精华
小牛编辑
183浏览
2023-03-14
在上章节Struts2 文件上传示例, 用户允许选择一个文件并上传到服务器。在本教程中,您将学习如何允许用户将多个文件上传到服务器。
这里创建一个Web工程:strut2uploadfiles,来演示在多个复选框如何设置的默认值,整个项目的结构如下图所示:
1. 动作类
在Action类,可以使用列表或数组以存储上传的文件。
FileUploadAction.java
package com.yiibai.common.action; import java.io.File; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class MultipleFileUploadAction extends ActionSupport{ private List<File> fileUpload = new ArrayList<File>(); private List<String> fileUploadContentType = new ArrayList<String>(); private List<String> fileUploadFileName = new ArrayList<String>(); public List<File> getFileUpload() { return fileUpload; } public void setFileUpload(List<File> fileUpload) { this.fileUpload = fileUpload; } public List<String> getFileUploadContentType() { return fileUploadContentType; } public void setFileUploadContentType(List<String> fileUploadContentType) { this.fileUploadContentType = fileUploadContentType; } public List<String> getFileUploadFileName() { return fileUploadFileName; } public void setFileUploadFileName(List<String> fileUploadFileName) { this.fileUploadFileName = fileUploadFileName; } public String upload() throws Exception{ for (File file: fileUpload) { System.out.println("File :" + file); } for (String fileName: fileUploadFileName) { System.out.println("Filename : " + fileName); } for (String fileContentType: fileUploadContentType) { System.out.println("File type : " + fileContentType); } return SUCCESS; } public String display() { return NONE; } }
2. 结果页面
使用<s:file>标签来渲染多文件上传组件,并设置表单 enctype类型为“multipart/form-data”.
fileupload.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <s:head /> </head> <body> <h1>Struts2上传多个文件示例</h1> <s:form action="resultAction" namespace="/" method="POST" enctype="multipart/form-data"> <s:file label="File 1" name="fileUpload" size="40" /> <s:file label="File 2" name="fileUpload" size="40" /> <s:file label="FIle 2" name="fileUpload" size="40" /> <s:submit value="submit" name="submit" /> </s:form> </body> </html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts2上传多个文件示例</h1> <div><div class="ads-in-post hide_if_width_less_800"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- 728x90 - After2ndH4 --> <ins class="adsbygoogle hide_if_width_less_800" data-ad-client="ca-pub-2836379775501347" data-ad-slot="3642936086" data-ad-region="yiibairegion"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></div><h2> File Name : <s:property value="fileUploadFileName"/> </h2> <h2> Content Type : <s:property value="fileUploadContentType"/> </h2> <h2> File : <s:property value="fileUpload"/> </h2> </body> </html>
3. struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="multipleFileUploadAction" class="com.yiibai.common.action.MultipleFileUploadAction" method="display"> <result name="none">pages/multiplefileupload.jsp</result> </action> <action name="resultAction" class="com.yiibai.common.action.MultipleFileUploadAction" method="upload"> <result name="success">pages/result.jsp</result> </action> </package> </struts>
4. 示例
http://localhost:8080/struts2uploafiles/multipleFileUploadAction.action
参考
- Struts2文件上传示例
- Struts2文件上传文档
- http://struts.apache.org/2.0.14/docs/file-upload.html
- http://struts.apache.org/2.0.14/docs/how-do-we-upload-files.html
下载代码 -