一、 搭建struts2环境
在myeclipse下,右击项目->MyEclipse->Project Facets->install Apache Struts2。
如要自己搭建,需下载struts2包,写struts.xml配置文件。
web.xml文件配置如下:
1
2
3
4
5
6
7
8
|
<filter> <filter-name>struts2</filter-name> <filter- class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter- class > </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> |
二、 文件上传
1.前台页面:
上传页面:
1
2
3
4
5
6
7
8
|
<body> <form action= "upload.action" method= "post" enctype= "multipart/form-data" > <input type= "file" name= "upload" /> <input type= "submit" value= "提交" /> <br> ${result} </form> </body> |
input name属性与后台命名一致。
上传失败页面:
1
2
3
4
|
<body> <h2>上传失败</h2> <s:fielderror></s:fielderror> </body> |
需:
1
|
<%@ taglib uri= "/struts-tags" prefix= "s" %> |
2.后台Action
主要属性upload,uploadContentType,uploadFileName。
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
|
package com.yf.action; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport{ private File upload; private String uploadContentType; private String uploadFileName; private String result; public File getUoload() { 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 getResult() { return result; } public void setResult(String result) { this .result = result; } @Override public String execute() throws Exception { String path = ServletActionContext.getServletContext().getRealPath( "/images" ); File file = new File(path); if (!file.exists()){ file.mkdir(); } System.out.println(upload); FileUtils.copyFile(upload, new File(file,uploadFileName)); result = "上传成功" ; return SUCCESS; } } |
3.struts.xml文件配置
配置action及配置拦截器限制上传文件的类型及大小。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" > <struts> <constant name= "struts.enable.DynamicMethodInvocation" value= "false" /> <constant name= "struts.devMode" value= "true" /> <constant name= "struts.multipart.saveDir" value= "/tmp" /> <constant name= "struts.custom.i18n.resources" value= "app" ></constant> < package name= "default" namespace= "/" extends = "struts-default" > <action name= "upload" class = "com.yf.action.UploadAction" > <result>/index.jsp</result> <result name= "input" >/error.jsp</result> <!-- 配置拦截器限制上传文件的类型及大小 --> <interceptor-ref name= "fileUpload" > <param name= "allowedTypes" >image/bmp,image/x-png,image/gif,image/jpeg</param> <param name= "maximumSize" >2M</param> </interceptor-ref> <interceptor-ref name= "defaultStack" ></interceptor-ref> </action> </ package > </struts> |
4.新建properties文件
文件上传失败信息显示到前台,处理显示出错信息。
文件内容如下:
1
2
|
struts.messages.error.file.too/large=\u4E0A\u4F20\u6587\u4EF6\u592A\u5927\u4E86\uFF01 struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u6587\u4EF6\u7C7B\u578B\u4E0D\u7B26\uFF01 |
即添加:
Name:struts.messages.error.file.too/large
value : 上传文件太大了!
Name : struts.messages.error.content.type.not.allowed
value: 上传文件类型不符!
运行结果如下:
选择jpg图片,大小不超过2M,运行后
选择非图片文件:
如需批量上传文件,将后台upload,uploadContentType,uploadFileName均改为List,循环读取上传文件保存到硬盘,前台加入input ,name属性一致。
以上所述是小编给大家介绍的Struts2实现文件上传功能实例解析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
原文链接:http://blog.csdn.net/giving_bestself/article/details/54233172