首页 > 开发 > Java > 正文

struts2实现多文件上传的示例代码

2024-07-13 10:05:23
字体:
来源:转载
供稿:网友

开发环境JDK1.8 eclipse struts2-2.3.31

1.创建web项目

2.导入struts2核心jar包

3.更改web.xml配置文件(只要配置好struts2的Filter就好)

4.创建src/struts.xml文件

<?xml version="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处理。    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->  <constant name="struts.action.extension" value="do" />  <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->  <constant name="struts.serve.static.browserCache" value="false" />  <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->  <constant name="struts.configuration.xml.reload" value="true" />  <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->  <constant name="struts.devMode" value="true" />  <!-- 默认的视图主题 -->  <constant name="struts.ui.theme" value="simple" />  <!--<constant name="struts.objectFactory" value="spring" />-->  <!--解决乱码  -->  <constant name="struts.i18n.encoding" value="UTF-8" />  <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) -->  <constant name="struts.multipart.maxSize" value="10701096"/>  <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir -->  <constant name="struts.multipart.saveDir " value="d:/tmp" />  <package name="upload" extends="struts-default">    <action name="fileUpload" class="com.ifan.action.FileUpload">      <!-- 动态设置savePath的属性值 -->      <param name="savePath">WEB-INF/images</param>      <result name="success">/success.jsp</result>      <result name="input">/error.jsp</result>      <interceptor-ref name="fileUpload">        <!-- 文件过滤 -->        <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>        <!-- 文件大小, 以字节为单位 -->        <param name="maximumSize">1025956</param>      </interceptor-ref>      <!-- 默认拦截器必须放在fileUpload之后,否则无效 -->      <interceptor-ref name="defaultStack" />    </action>  </package></struts>

5.创建src/com.ifan.action.FileUpload.Java

package com.ifan.action;import java.io.File;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUpload extends ActionSupport{  private File[] image; //上传的文件  private String[] imageFileName; //文件名称  private String[] imageContentType; //文件类型  public String execute() throws Exception {    ServletActionContext.getRequest().setCharacterEncoding("UTF-8");    String realpath = ServletActionContext.getServletContext().getRealPath("/images");    System.out.println(realpath);    if (image != null) {      File savedir=new File(realpath);      if(!savedir.getParentFile().exists())        savedir.getParentFile().mkdirs();      for(int i=0;i<image.length;i++){        File savefile = new File(savedir, imageFileName[i]);        FileUtils.copyFile(image[i], savefile);      }      ActionContext.getContext().put("message", "文件上传成功");    }    return "success";  }  public File[] getImage() {    return image;  }  public void setImage(File[] image) {    this.image = image;  }  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;  }}

6.创建WebContent/index.jsp ,作为上传文件的页面

<%@ 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文件夹,作为上传文件的存储位置

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VeVb武林网。


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表