struts2 多个文件上传



01<!---------------------文件名:upload.jsp----------------->
02<%@taglib prefix="s" uri="/struts-tags"%>
03<html>
04    <head>
05        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
06        <title>上传文件</title>
07    </head>
08    <body>
09    <!-- 上传文件表单定义 -->
10    <s:form action="upload" method="post" enctype="multipart/form-data">
11        <tr>
12    <!-- 上传文件标签定义 -->
13    <td>上传文件:<s:file name="file"></s:file></td>
14    </tr>
15    <tr>
16    <td>再次上传文件:<s:file name="file"></s:file></td>
17    </tr>
18    <tr>
19    <td align="left"><s:submit name="submit" value="提交"></s:submit></td>
20    </tr>
21    </s:form>
22    </body>
23</html>

2. [代码][Java]代码                                      

01<!-------------------上传文件成功后结果页面文件名:result.jsp ----------------->
02<%@taglib prefix="s" uri="/struts-tags"%>
03<html>
04    <head>
05        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
06        <title>上传结果</title>
07    </head>
08    <body>
09        上传文件:
10        <!-- 显示上传成功文件名 -->
11        <s:property value="fileFileName" />
12    </body>
13</html>

3. [代码][Java]代码             

01<!------------------文件名:UploadAction.java ------------------>
02import java.io.File;
03import java.io.FileInputStream;
04import java.io.FileNotFoundException;
05import java.io.FileOutputStream;
06import java.io.IOException;
07import java.io.InputStream;
08import java.io.OutputStream;
09import java.util.List;
10 
11import org.apache.struts2.ServletActionContext;
12import com.opensymphony.xwork2.ActionSupport;
13 
14//文件上传Action
15public class UploadAction extends ActionSupport {
16    //上传文件存放路径
17    private final static String UPLOADDIR = "/upload";
18    //上传文件集合
19    private List<File> file;
20    //上传文件名集合
21    private List<String> fileFileName;
22    //上传文件内容类型集合
23    private List<String> fileContentType;
24 
25    public List<File> getFile() {
26        return file;
27    }
28 
29    public void setFile(List<File> file) {
30        this.file = file;
31    }
32 
33    public List<String> getFileFileName() {
34        return fileFileName;
35    }
36 
37    public void setFileFileName(List<String> fileFileName) {
38        this.fileFileName = fileFileName;
39    }
40 
41    public List<String> getFileContentType() {
42        return fileContentType;
43    }
44 
45    public void setFileContentType(List<String> fileContentType) {
46        this.fileContentType = fileContentType;
47    }
48 
49    public String execute() throws Exception {
50        for (int i = 0; i < file.size(); i++) {
51            //循环上传每个文件
52            uploadFile(i);
53        }
54        return "success";
55    }
56 
57    //执行上传功能
58    private void uploadFile(int i) throws FileNotFoundException, IOException {
59        try {
60            InputStream in = new FileInputStream(file.get(i));
61            String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);
62            File uploadFile = new File(dir, this.getFileFileName().get(i));
63            OutputStream out = new FileOutputStream(uploadFile);
64            byte[] buffer = new byte[1024 * 1024];
65            int length;
66            while ((length = in.read(buffer)) > 0) {
67                out.write(buffer, 0, length);
68            }
69 
70            in.close();
71            out.close();
72        } catch (FileNotFoundException ex) {
73            ex.printStackTrace();
74        } catch (IOException ex) {
75            ex.printStackTrace();
76        }
77    }
78}

4. [代码][Java]代码               

01struts.xml配置文件中有关文件上传的配置:
02<!--------------------文件名:struts.xml------------------->
03<struts>
04    <!-- 系统常量定义,定义上传文件字符集编码 -->
05    <constant name="struts.i18n.encoding" value="gb2312"></constant>
06    <!-- 系统常量定义,定义上传文件临时存放路径 -->
07    <constant name="struts.multipart.saveDir" value="c:\"></constant>
08    <!-- Action所在包定义 -->
09    <package name="C04.4" extends="struts-default">
10        <!-- Action名字,类以及导航页面定义 -->
11        <!-- 通过Action类处理才导航的的Action定义 -->
12        <action name="upload" class="action.UploadAction">
13            <result name="input">/jsp/upload.jsp</result>
14            <result name="success">/jsp/result.jsp</result>
15        </action>
16    </package>
17</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值