Struts2单文件上传和多文件上传

单文件上传:

一、在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。

二、编写register.jsp提交页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
    </head>
    <body>
        <s:form action="register" method="post" enctype="multipart/form-data">
            username:<s:textfield name="username"></s:textfield>
            <br />
            <s:file name="uploadFile"></s:file>
            <br />
            <s:submit value="提交"></s:submit>
        </s:form>
    </body>
</html>

三、编写Action

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package controller;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {

    private String username;

    private File uploadFile;

    private String uploadFileFileName;

    @Override
    public void validate() {

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public File getUploadFile() {
        return uploadFile;
    }

    public void setUploadFile(File uploadFile) {
        this.uploadFile = uploadFile;
    }

    public String getUploadFileFileName() {
        return uploadFileFileName;
    }

    public void setUploadFileFileName(String uploadFileFileName) {
        this.uploadFileFileName = uploadFileFileName;
    }

    public String execute() throws IOException {
        // 输出username的值
        System.out.println("username的值是:" + username);
        // 取得上传后文件要存放的路径
        String targetDirectory = ServletActionContext.getRequest().getRealPath(
                "/upload");
        // 生成上传的File对象
        File target = new File(targetDirectory, uploadFileFileName);
        // 复制File对象,从而实现上传文件
        FileUtils.copyFile(uploadFile, target);

        return "register";
    }

}

多文件上传:

和单文件上传差不多,只是把属性改成数组:

四、编写上传界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
    </head>
    <body>
        <s:form action="register" method="post" enctype="multipart/form-data">
            username:<s:textfield name="username"></s:textfield>
            <br />
            <s:file name="uploadFile"></s:file>
            <br />
            <s:file name="uploadFile"></s:file>
            <br />
            <s:file name="uploadFile"></s:file>
            <br />
            <s:submit value="提交"></s:submit>
        </s:form>
    </body>
</html>

五、编写处理请求的Action

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {

    private String username;

    private File uploadFile[];// 是File的数组类型

    private String uploadFileFileName[];// 由于上传多文件,所以文件名也是数组类型

    @Override
    public void validate() {

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public File[] getUploadFile() {
        return uploadFile;
    }

    public void setUploadFile(File[] uploadFile) {
        this.uploadFile = uploadFile;
    }

    public String[] getUploadFileFileName() {
        return uploadFileFileName;
    }

    public void setUploadFileFileName(String[] uploadFileFileName) {
        this.uploadFileFileName = uploadFileFileName;
    }

    public String execute() throws IOException {
        System.out.println("username的值是:" + username);
        // 取得上传后文件所存放的路径
        String targetDirectory = ServletActionContext.getRequest().getRealPath(
                "/upload");
        // 循环得到File[]数组中的每一个对象,然后分别复制File对象,实现上传文件
        for (int i = 0; i < uploadFile.length; i++) {
            // 在文件名上加上时间来标识每一个上传的文件
            File target = new File(targetDirectory, new SimpleDateFormat(
                    "yyyy_MM_dd_HH_mm_ss").format(new Date()).toString()
                    + System.nanoTime() + uploadFileFileName[i]);

            FileUtils.copyFile(uploadFile[i], target);
        }

        return "register";
    }

}

 

除非注明,Coder文章均为原创,转载请以链接形式标明本文地址

本文地址:http://www.alonemonkey.com/struts2-file-upload.html

本文链接:http://www.alonemonkey.com/struts2-file-upload.html