Struts2文件上传

来源:互联网 发布:在淘宝购物的具体步骤 编辑:程序博客网 时间:2024/06/04 19:17

单个文件上传

引入的jar包


struts.xml
<?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><constant name="struts.enable.DynamicMethodInvocation" value="false" /><constant name="struts.devMode" value="true" /><constant name="struts.multipart.maxSize" value="10701096" /><!-- 最大上传文件大小限制 --><package name="he" namespace="/hello" extends="struts-default"><action name="h1_*" class="com.zero.HelloWorldAction" method="{1}"><result name="success">/WEB-INF/jsp/hello.jsp</result></action></package></struts>

    form表单中enctype设置为:"multipart/form-data"
    表单的enctype属性指定的是表单数据的编码方式,该属性有3个值
  (1)application/x-www-form-urlencoded,这是默认的编码方式,它只能处理表单域里的value属性,采用这种编码方式的表单会将表单域的值处理成URL编码方式。
  (2)multipart/form-data,采用这种编码方式会以二进制流的方式来处理表单数据 ,这种编码方式会把文件域指定文件的内容也封装到请求参数里。上传的文件会在底层封装,并通过二进制流读取。
  (3)text/plain,这种编码方式当表单的action属性为mailto:URL的形式是比较方便,这种方式主要适用于直接通过表单发送邮件的方式。

hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><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"></head><form action="<%=request.getContextPath()%>/hello/h1_execute"method="post" enctype="multipart/form-data">文件:<input type="file" name="aimage"> <input type="submit"value="上传" /></form>${message} ${pageContext.request.contextPath}</html>
    这里<%=request.getContextPath()%>${pageContext.request.contextPath}的值均是项目名称/Struts2。

HelloWorldAction.java
import java.io.File;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class HelloWorldAction {private File aimage;// 必须与jsp中的一致private String aimageFileName;// 想要获取文件名,则必须要按照xxxFileName命名private String aimageContentType;// 获取文件类型,也必须按照xxxContentType命名public File getAimage() {return aimage;}public void setAimage(File aimage) {this.aimage = aimage;}public String getAimageFileName() {return aimageFileName;}public void setAimageFileName(String aimageFileName) {this.aimageFileName = aimageFileName;}public String upload() {return "success";}public String getAimageContentType() {return aimageContentType;}public void setAimageContentType(String aimageContentType) {this.aimageContentType = aimageContentType;}public String execute() throws Exception {String realpath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");System.out.println(realpath);if (aimage != null) {File savefile = new File(new File(realpath), aimageFileName);if (!savefile.getParentFile().exists())savefile.getParentFile().mkdirs();FileUtils.copyFile(aimage, savefile);ActionContext.getContext().put("message","上传成功--" + aimageContentType);}return "success";}}


多个文件上传

HelloWorldAction.java
import java.io.File;import java.util.List;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class HelloWorldAction {private List<File> images;// 必须与jsp中的一致,定义为数组[]或List类型均可private String[] imagesFileName;// 想要获取文件名,则必须要按照xxxFileName命名,定义为数组[]或List类型均可private String[] imagesContentType;// 获取文件类型,定义为数组[]或List类型均可public String execute() throws Exception {String realpath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");System.out.println(realpath);if (images != null) {File savedir = new File(realpath);if (!savedir.exists()) {savedir.mkdirs();}for (int i = 0; i < images.size(); i++) {File savefile = new File(new File(realpath), imagesFileName[i]);FileUtils.copyFile(images.get(i), savefile);}ActionContext.getContext().put("message", "上传成功");}return "success";}public List<File> getImages() {return images;}public void setImages(List<File> images) {this.images = images;}public String[] getImagesFileName() {return imagesFileName;}public void setImagesFileName(String[] imagesFileName) {this.imagesFileName = imagesFileName;}public String[] getImagesContentType() {return imagesContentType;}public void setImagesContentType(String[] imagesContentType) {this.imagesContentType = imagesContentType;}}

hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><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"></head><form action="<%=request.getContextPath()%>/hello/h1_execute"method="post" enctype="multipart/form-data">文件1:<input type="file" name="images"> <br /> 文件2:<input type="file" name="images"> <br /> 文件3:<input type="file" name="images"> <br /> <input type="submit" value="上传" /></form>${message} ${pageContext.request.contextPath}</html>

     可能会出现Unable to find 'struts.multipart.saveDir' property setting.
     原因是:struts.multipart.saveDir没有配置。struts.multipart.saveDir用于存放指定临时文件的文件夹,该配置在struts.properties文件中。例如:在struts.properties文件中加入如下代码:struts.multipart.saveDir = /tmp。这样下次提交表单的时候就不会出现这个问题了!
     如果没有用struts.properties文件,也可以在struts.xml中配置如下代码,效果一样:<constant name="struts.multipart.saveDir" value="/tmp"/>

0 0