struts 文件上传 Struts2实现文件上传功能实例解析
Giving_bestself 人气:0想了解Struts2实现文件上传功能实例解析的相关内容吗,Giving_bestself在本文为您仔细讲解struts 文件上传的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:struts,文件上传,下面大家一起来学习吧。
一、 搭建struts2环境
在myeclipse下,右击项目->MyEclipse->Project Facets->install Apache Struts2。
如要自己搭建,需下载struts2包,写struts.xml配置文件。
web.xml文件配置如下:
<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.前台页面:
上传页面:
<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属性与后台命名一致。
上传失败页面:
<body> <h2>上传失败</h2> <s:fielderror></s:fielderror> </body>
需:
<%@ taglib uri="/struts-tags" prefix="s"%>
2.后台Action
主要属性upload,uploadContentType,uploadFileName。
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及配置拦截器限制上传文件的类型及大小。
<?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文件
文件上传失败信息显示到前台,处理显示出错信息。
文件内容如下:
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实现文件上传功能实例解析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
加载全部内容