• J2EE进阶(二)从零开始之Struts2_j2eestruts2


    1.引入相应的jar包(commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar)

    2.把form的enctype设置为"multipart/form-data",如下所示:

    文件1:

    3.在action类中添加如下代码中注释的几个属性。

    public class HelloWorldAction {  
        private File upload;//得到上传的文件  
       	private String uploadContentType;//得到上传文件的扩展名  
       	private String uploadFileName;//得到上传文件的名称   
        	public File getUpload() {  
            	return upload;  
        	}  
        	public void setUpload(File upload) {  
            	this.upload = upload;  
        	}  
        	public String getUploadContentType() {  
            	return uploadContentType;  
        	}  
        	public void setUploadContentType(String uploadContentType) {  
            	this.uploadContentType = uploadContentType;  
        	}  
        	public String getUploadFileName() {  
            	return uploadFileName;  
        	}  
        	public void setUploadFileName(String uploadFileName) {  
            	this.uploadFileName = uploadFileName;  
        	}  
        	public String upload() throws IOException {  
            	String realpath = ServletActionContext.getServletContext().getRealPath("/upload");  
            if(upload != null) {  
                File savefile = new File(realpath,uploadFileName);  
                if(!savefile.getParentFile().exists()) {  
                    savefile.getParentFile().mkdirs();  
                }  
                FileUtils.copyFile(upload, savefile);  
                ActionContext.getContext().put("msg", "文件上传成功!");  
            }  
            return "success";  
        }  
    }
    

    注意,如果在上传的过程中文件的大小超过了struts2默认的文件大小的话,就会上传失败,这时候,可以根据具体的情况设置struts.multipart.maxSize的值来满足上传的需求。

    多文件上传

    在实际的项目中,有时候可能会要求上传多个文件的情况,下面就来说说上传多个文件的情况。

    1.同上。

    2.form如下所示:

    文件1:
    文件2:
    文件3:

    3.action中添加的几个属性都是数组形式的。

    public class HelloWorldAction {  
        	private File[] upload;//得到上传的文件  
        private String[] uploadContentType;//得到上传文件的扩展名  
        private String[] uploadFileName;//得到上传文件的名称    
        public File[] getUpload() {  
            return upload;  
        }  
        public void setUpload(File[] upload) {  
            this.upload = upload;  
        }  
        public String[] getUploadContentType() {  
            return uploadContentType;  
        }  
        public void setUploadContentType(String[] uploadContentType) {  
            this.uploadContentType = uploadContentType;  
        }  
        public String[] getUploadFileName() {  
            return uploadFileName;  
        }  
        public void setUploadFileName(String[] uploadFileName) {  
            this.uploadFileName = uploadFileName;  
        }  
        public String upload() throws IOException {  
            String realpath = ServletActionContext.getServletContext().getRealPath("/upload");  
            if(upload != null) {  
                for(int i=0; i

    自定义拦截器

    自定义拦截器要实现com.opensymphony.xwork2.interceptor.Interceptor接口。下面是一个自定义拦截器的例子:

      
    
    总结

    =============================================================

    从转行到现在,差不多两年的时间,虽不能和大佬相比,但也是学了很多东西。我个人在学习的过程中,习惯简单做做笔记,方便自己复习的时候能够快速理解,现在将自己的笔记分享出来,和大家共同学习。

    个人将这段时间所学的知识,分为三个阶段:

    第一阶段:HTML&CSS&JavaScript基础

    第二阶段:移动端开发技术

    第三阶段:前端常用框架

    • 推荐学习方式:针对某个知识点,可以先简单过一下我的笔记,如果理解,那是最好,可以帮助快速解决问题;如果因为我的笔记太过简陋不理解,可以关注我以后我还会继续分享。

    • 大厂的面试难在,针对一个基础知识点,比如JS的事件循环机制,不会上来就问概念,而是换个角度,从题目入手,看你是否真正掌握。所以对于概念的理解真的很重要。

  • 相关阅读:
    [springboot专栏]使用redisson实现分布式锁
    深入浅出『汉诺塔』
    批量上传图片添加水印
    Mac终端出现 brew command not found 解决
    QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex
    给列起别名(关键字:as)
    SpringBoot+Vue+Redis实现验证码功能、一个小时只允许发三次验证码。一次验证码有效期二分钟。SpringBoot整合Redis
    vue小案列(hello world)
    android service基本介绍
    Electron之单例+多窗口
  • 原文地址:https://blog.csdn.net/2401_85124737/article/details/139625561