• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >swift > struts2实现多文件上传的示例代码

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

作者:iFan138 字体:[增加 减小] 来源:互联网 时间:2017-05-28

iFan138 通过本文主要向大家介绍了struts2示例,struts2文件下载代码,struts2文件上传代码,struts2文件上传,struts2文件下载等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

开发环境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>

</div>

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;
  }
}

</div>

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>

</div>

7.创建WebContent/success.jsp 作为文件上传成功跳转的页面,创建WebContent/error.jsp 作为文件上传失败的页面 , 创建WebContent/images文件夹,作为上传文件的存储位置

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 2017-05-28Swift教程之方法详解
  • 2017-05-28Swift在控件中添加点击手势的方法
  • 2017-05-28分隔List集合,按指定大小,将集合分成多个的方法
  • 2017-05-28Swift流程控制之循环语句和判断语句详解
  • 2017-05-28Swift下使用UICollectionView 实现长按拖拽功能
  • 2017-05-28查看import的类是出自哪个jar包的方法
  • 2017-05-28初探Swift3.0带来的变化汇总
  • 2017-05-28详解Swift中的数据类型类型转换
  • 2017-05-28Swift类型创建之自定义一个类型详解
  • 2017-05-28swift3.0键盘弹起遮挡输入框问题的解决方案

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Swift教程之函数详解
    • Swift语言中字符串相关的基本概念解析
    • Swift类型创建之自定义一个类型详解
    • Swift实现堆排序算法的代码示例
    • 苹果公司编程语言Swift语言简介
    • gson ajax 数字精度丢失问题的解决方法
    • struts2实现文件下载功能
    • iOS开发中Swift 指纹验证功能模块实例代码
    • Swift的函数式编程详解
    • Swift编程之枚举类型详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有