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如下所示:

<form action="<%=basePath%>upload/upload" method="post" name="form" enctype="multipart/form-data">  
    	文件1:<input type="file" name="upload"/><br/>  
    	文件2:<input type="file" name="upload"/><br/>  
    	文件3:<input type="file" name="upload"/><br/>  
    	<input type="submit" value="上传" />  
</form>

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<upload.length; i++) {  
                File savefile = new File(realpath,uploadFileName[i]);  
                if(!savefile.getParentFile().exists()) {  
                    savefile.getParentFile().mkdirs();  
                }  
                FileUtils.copyFile(upload[i], savefile);  
            }  
            ActionContext.getContext().put("msg", "文件上传成功!");  
        }  
        return "success";  
    }  
}  

自定义拦截器

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

  
总结

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

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

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

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

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

第三阶段:前端常用框架

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

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

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值