码迷,mamicode.com
首页 > Web开发 > 详细

Struts2多文件上传

时间:2015-10-28 21:02:51      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

第一步:首先创建一个多文件上传的页面

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>多文件上传</title>
</head>
<body>
    <h1>多文件上传</h1>
    
    <br><br>
    <form action="/Struts2-day02pm/upload/multi_upload.action" method="post" enctype="multipart/form-data">
        文件上传2:<br><br>
        <input type="file" name="oneFiles"><br><br>
        <input type="file" name="oneFiles"><br><br>
        <input type="file" name="oneFiles"><br><br>
        <input type="submit" value="开始多文件上传">
    </form>
</body>

第二步:创建多文件上传的请求处理类MultiUploadAction继承BaseAction(BaseAction代码见单文件上传)

public class MultiUploadAction extends BaseAction {

    private static final long serialVersionUID = -4445894434193884175L;
    
    private File[] oneFiles;
    private String[] oneFilesFileName;
    private String[] oneFilesContentType;
    
    private HttpServletRequest request;
    
    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public String execute() throws Exception {
        String uploadPath = request.getServletContext().getRealPath("upload");

        for (int i = 0; i < oneFiles.length; i++) {
            
            File file = new File(uploadPath, oneFilesFileName[i]);//拼接文件的存放路径和存放文件的真实名称
            FileUtils.copyFile(oneFiles[i], file);//将临时文件复制到上面这个路径
        }
        return SUCCESS;
    }

    public File[] getOneFiles() {
        return oneFiles;
    }
    public void setOneFiles(File[] oneFiles) {
        this.oneFiles = oneFiles;
    }
    public String[] getOneFilesFileName() {
        return oneFilesFileName;
    }
    public void setOneFilesFileName(String[] oneFilesFileName) {
        this.oneFilesFileName = oneFilesFileName;
    }
    public String[] getOneFilesContentType() {
        return oneFilesContentType;
    }
    public void setOneFilesContentType(String[] oneFilesContentType) {
        this.oneFilesContentType = oneFilesContentType;
    }
    public HttpServletRequest getRequest() {
        return request;
    }
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
}

第三步:配置struts.xml配置文件

<struts>

    <package name="upload-default" namespace="/upload" extends="struts-default">
        
        <action name="multi_upload" class="com.struts2.day02pm.action.MultiUploadAction">
            <result>/WEB-INF/jsp/multi_upload_ok.jsp</result>
        </action>
    </package>

</struts>

第四步:前台页面测试上传文件

 

Struts2多文件上传

标签:

原文地址:http://www.cnblogs.com/xujianbo/p/4918568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!