• Struts2多文件上传


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

    <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>

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

  • 相关阅读:
    chm文件生成
    java基础--集合
    java基础--多线程
    nexus
    java基础--IO流
    http与https
    java基础--数据结构
    mysql 优化
    maven依赖和传递
    java设计模式
  • 原文地址:https://www.cnblogs.com/xujianbo/p/4918568.html
Copyright © 2020-2023  润新知