欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-04 11:52:35
开发环境jdk1.8 eclipse struts2-2.3.31 1.创建web项目 2.导入struts2核心jar包 3.更改web.xml配...

开发环境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文件夹,作为上传文件的存储位置

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