开发环境JDK1.8 eclipse struts2-2.3.31
1.创建web项目
2.导入struts2核心jar包
3.更改web.xml配置文件(只要配置好struts2的Filter就好)
4.创建src/struts.xml文件
				?
			
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
						 | <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 --><constantname="struts.action.extension"value="do"/><!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 --><constantname="struts.serve.static.browserCache"value="false"/><!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --><constantname="struts.configuration.xml.reload"value="true"/><!-- 开发模式下使用,这样可以打印出更详细的错误信息 --><constantname="struts.devMode"value="true"/><!-- 默认的视图主题 --><constantname="struts.ui.theme"value="simple"/><!--<constant name="struts.objectFactory" value="spring" />--><!--解决乱码 --><constantname="struts.i18n.encoding"value="UTF-8"/><!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) --><constantname="struts.multipart.maxSize"value="10701096"/><!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir --><constantname="struts.multipart.saveDir "value="d:/tmp"/><packagename="upload"extends="struts-default"><actionname="fileUpload"class="com.ifan.action.FileUpload"><!-- 动态设置savePath的属性值 --><paramname="savePath">WEB-INF/images</param><resultname="success">/success.jsp</result><resultname="input">/error.jsp</result><interceptor-refname="fileUpload"><!-- 文件过滤 --><paramname="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param><!-- 文件大小, 以字节为单位 --><paramname="maximumSize">1025956</param></interceptor-ref><!-- 默认拦截器必须放在fileUpload之后,否则无效 --><interceptor-refname="defaultStack"/></action></package></struts> | 
5.创建src/com.ifan.action.FileUpload.Java
				?
			
                	
    
	
	
		
		
	
 
	
		
			
	
	 
     
	
			
                 
			
		
		
			
			
			
    
        
        
	
			
						
			
            			
    		
    		
		
	    
    	
    	
        
    	
    
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
 
								43
 
								44
 
								45
 
								46
 
								47
 
								48
 
								49
 
								50
 
								51
 
								52
 
								53
 
								54
 
								55
 
								56
 
								57
						 | packagecom.ifan.action;importjava.io.File;importorg.apache.commons.io.FileUtils;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;publicclassFileUpload extendsActionSupport{privateFile[] image; //上传的文件privateString[] imageFileName; //文件名称privateString[] imageContentType; //文件类型publicString execute() throwsException {ServletActionContext.getRequest().setCharacterEncoding("UTF-8");String realpath = ServletActionContext.getServletContext().getRealPath("/images");System.out.println(realpath);if(image != null) {File savedir=newFile(realpath);if(!savedir.getParentFile().exists())savedir.getParentFile().mkdirs();for(inti=0;i<image.length;i++){File savefile = newFile(savedir, imageFileName[i]);FileUtils.copyFile(image[i], savefile);}ActionContext.getContext().put("message", "文件上传成功");}return"success";}publicFile[] getImage() {returnimage;}publicvoidsetImage(File[] image) {this.image = image;}publicString[] getImageContentType() {returnimageContentType;}publicvoidsetImageContentType(String[] imageContentType) {this.imageContentType = imageContentType;}publicString[] getImageFileName() {returnimageFileName;}publicvoidsetImageFileName(String[] imageFileName) {this.imageFileName = imageFileName;}} | 
6.创建WebContent/index.jsp ,作为上传文件的页面
				?
			
	
						
						
						
						
						
						
						
																		
    
        
    
        
                        
                
                    
                
                
                
                    
                
                
                
                    
                
                
                
                    
                
                        
    
 																		
						
																		
    
        
 												
						
																		
	
	
		
				
			
																		
						
						
					
				
				                | 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
						 | <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%><%@ taglib prefix="s"uri="/struts-tags"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":"+ request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"rel="external nofollow"><title>My JSP 'hello.jsp'starting page</title><meta http-equiv="pragma"content="no-cache"><meta http-equiv="cache-control"content="no-cache"><meta http-equiv="expires"content="0"><meta http-equiv="keywords"content="keyword1,keyword2,keyword3"><meta http-equiv="description"content="This is my page"><!--<link rel="stylesheet"type="text/css"href="styles.css"rel="external nofollow">--></head><body><!-- Struts2的文件上传标签 --><s:form action="fileUpload"namespace="/"method="POST"enctype="multipart/form-data"><!-- 该name需要和后台的File类型的名字对应起来,否则将得不到该文件 size 上传文件的大小 --><s:file name="image"label="Select a File to upload"size="40"/><s:file name="image"label="Select a File to upload"size="40"/><s:submit value="submit"name="submit"/></s:form></body></html> | 
7.创建WebContent/success.jsp 作为文件上传成功跳转的页面,创建WebContent/error.jsp 作为文件上传失败的页面 , 创建WebContent/images文件夹,作为上传文件的存储位置
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/ifan138/article/details/61206275
相关文章
             猜你喜欢
        
        - 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
			TA的动态
			
		
				- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
				您的支持,是我们最大的动力!				
			
		
        热门文章
        
    
    - 
            2025-05-25 22
- 
            2025-06-04 30
- 
            2025-05-27 37
- 
            2025-05-29 61
- 
            2025-06-04 69
		热门评论
	
	 
        
 
    		 
            	 
															 
         
        
 
                        