Struts2_单文件上传

来源:互联网 发布:年终总结数据分析 编辑:程序博客网 时间:2024/05/19 19:56
第一步:在WEB-INF/lib下加入commons-fileupload-x.x.x.jar、commons-io-x.x.x.jar。这两个文件可以从http://commons.apache.org/下载第二步:把form表的enctype设置为:"multipart/form-data",如下:<form enctype="multipart/form-data"action="<%=request.getContextPath()%>/demo/fileUp_execute.action"method="post">文件:<input type="file" name="image" /> <input type="submit" value="上传" /></form>第三步:在Action类中添加以下属性:private File image;// 得到上传的文件private String imageFileName;// 得到文件的名称private String imageContentType;// 得到文件的类型public String execute() throws IOException {String realpath = ServletActionContext.getServletContext().getRealPath("/images");System.out.println(realpath);if (image != null) {// images/File savefile = new File(new File(realpath), imageFileName);if (!savefile.getParentFile().exists()) {savefile.getParentFile().mkdir();}FileUtils.copyFile(image, savefile);ActionContext.getContext().put("message", "上传成功");}return "success";}


package cn.itcast.g_action;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class HelloWorldAction {private File image;// 得到上传的文件private String imageFileName;// 得到文件的名称private String imageContentType;// 得到文件的类型public String getImageContentType() {return imageContentType;}public void setImageContentType(String imageContentType) {this.imageContentType = imageContentType;}public String getImageFileName() {return imageFileName;}public void setImageFileName(String imageFileName) {this.imageFileName = imageFileName;}public File getImage() {return image;}public void setImage(File image) {this.image = image;}public String execute() throws IOException {String realpath = ServletActionContext.getServletContext().getRealPath("/images");System.out.println(realpath);if (image != null) {// images/File savefile = new File(new File(realpath), imageFileName);if (!savefile.getParentFile().exists()) {savefile.getParentFile().mkdir();}FileUtils.copyFile(image, savefile);ActionContext.getContext().put("message", "上传成功");}return "success";}}

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="itcast" namespace="/demo" extends="struts-default"><action name="fileUp_*" class="cn.itcast.g_action.HelloWorldAction" method="{1}"><result name="success">/WEB-INF/page/message.jsp</result></action></package></struts>


<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 修改默认.action后缀 --><constant name="struts.action.extension" value="com,action"></constant><!-- 该属性设置struts2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false --><constant name="struts.enable.DynamicMethodlnvocation" value="false" /><!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker、velocity的输入 --><constant name="struts.i18n.encoding" value="UTF-8" /><!-- 上传文件的大小限制 -->  <constant name"struts.multipart.maxSize" value="10701096"/>  <!-- 集合其它struts配置 --><include file="struts_file_one.xml" /></struts>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"></head><body><form enctype="multipart/form-data"action="<%=request.getContextPath()%>/demo/fileUp_execute.action"method="post">文件:<input type="file" name="image" /> <input type="submit" value="上传" /></form></body></html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>My JSP 'msessage.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"></head><body>${message }</body></html>


原创粉丝点击