zoukankan      html  css  js  c++  java
  • struts2的单文件上传

    第一步:首先写个上传文件的页面(简单的一个form表单)

    <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/one_upload.action" method="post" enctype="multipart/form-data">
            文件上传1:<br><br>
            <input type="file" name="oneFile"><br><br>
            <input type="submit" value="开始上传">
        </form>
        
    </body>

    第二步:创建一个基类BaseAction.java,继承ActionSupport,并实现ServletRequestAware,ServletResponseAware,ServletContextAware三个接口,重写三个接口的set方法

    public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
        /**
         * 编写一个基类,继承ActionSupport并实现相应的接口
         * 以后的Action直接继承该类,就可以简单获取到Servlet API
         * 这是一个典型的适配设计模式
         * @author Owen
         */
    
        private static final long serialVersionUID = 7267018575222346353L;
        
        
        @Override
        public void setServletContext(ServletContext servletContext) {
        }
    
        @Override
        public void setServletResponse(HttpServletResponse response) {
        }
    
        @Override
        public void setServletRequest(HttpServletRequest request) {
        }
    
    }

    第三步:创建OneUploadAction请求处理类,继承BaseAction

    public class OneUploadAction extends BaseAction {
    
        private static final long serialVersionUID = -4445894434193884175L;
    //    该属性名必须和<input type="file" name="oneFile">中name值一致
        private File oneFile;
    //    真实名称
        private String oneFileFileName;
    //    文件类型
        private String oneFileContentType;
        
        private HttpServletRequest request;
        
        @Override
        public void setServletRequest(HttpServletRequest request) {
            this.request = request;
        }
    
        @Override
        public String execute() throws Exception {
    //        获取保存上传文件在服务器的真是路径
            String uploadPath = request.getServletContext().getRealPath("upload");
            System.out.println(uploadPath);//目录真是路径
            System.out.println("oneFile--"+oneFile.getName());//文件临时名称
            System.out.println("oneFileFileName--"+oneFileFileName);//文件原始名称
            System.out.println("oneFileContentType--"+oneFileContentType);//文件类型
    //        第一种:该步骤适合上传小文件
    //        将临时文件复制到硬盘上的真是路径
            /*
            File file = new File(uploadPath, oneFileFileName);//拼接文件的存放路径和存放文件的真实名称
            FileUtils.copyFile(oneFile, file);//将临时文件复制到上面这个路径
            */
            
    //        第二种:适合大文件上传操作
            /*InputStream is = null;
            OutputStream os = null;
            
            is = new FileInputStream(oneFile);
            os = new FileOutputStream(new File(uploadPath,oneFileFileName));
            
            byte[] buffer = new byte[500];
            int length = 0;
            while((length=is.read(buffer,0,buffer.length)) != -1){
                os.write(buffer, 0,length);
            }
            os.close();
            is.close();*/
            copyFile(uploadPath);
            
            return SUCCESS;
        }
        public void copyFile(String uploadPath){
            InputStream is = null;
            OutputStream os = null;
            
            try {
                is = new FileInputStream(oneFile);
                os = new FileOutputStream(new File(uploadPath,oneFileFileName));
                
                byte[] buffer = new byte[500];
                int len = 0;
                while((len=is.read(buffer,0,buffer.length)) != -1){
                    os.write(buffer, 0,len);
                }
                os.close();
                is.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    
        public File getOneFile() {
            return oneFile;
        }
    
        public void setOneFile(File oneFile) {
            this.oneFile = oneFile;
        }
    
        public String getOneFileFileName() {
            return oneFileFileName;
        }
    
        public void setOneFileFileName(String oneFileFileName) {
            this.oneFileFileName = oneFileFileName;
        }
    
        public String getOneFileContentType() {
            return oneFileContentType;
        }
    
        public void setOneFileContentType(String oneFileContentType) {
            this.oneFileContentType = oneFileContentType;
        }
        
    }

    第三步:配置struts.xml文件

    <struts>
    
        <package name="upload-default" namespace="/upload" extends="struts-default">
            
            <action name="one_upload" class="com.struts2.day02pm.action.OneUploadAction">
                <result>/WEB-INF/jsp/one_upload_ok.jsp</result>
            </action>
            
        </package>
    
    </struts>
  • 相关阅读:
    自动登录跳板机->开发机
    关于写代码的一下规范
    vscode 配置 GOPATH
    thinkphp6.0 nginx 配置
    vue-cli 3.x 构建项目,webpack没有了?
    Laravel6.0 使用 Jwt-auth 实现多用户接口认证
    怎么在 localhost 下访问多个 Laravel 项目,通过一个IP访问多个项目(不仅仅是改变端口哦)
    laravel 5.8 实现消息推送
    vs code 设置 保存自动格式化vue代码
    项目开发规范(编码规范、命名规范、安全规范、前端优化、源码提交规范、代码维护规范、产品发布规范)
  • 原文地址:https://www.cnblogs.com/myjavalife/p/4923093.html
Copyright © 2011-2022 走看看