1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib uri="/struts-tags" prefix="s"%>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="UTF-8">
  8. <title>struts2的一个例子</title>
  9. </head>
  10. <body>
  11. <s:form action="files.action" method="post" enctype="multipart/form-data">
  12. <s:textarea name="username" label="用户名"/>
  13. <s:file name="files" label="请选择上传文件"/>
  14. <s:file name="files" label="请选择上传文件"/>
  15. <s:submit value="提交"/>
  16. </s:form>
  17.  
  18. </body>
  19. </html>

index.jsp代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  3. <filter>
  4. <filter-name>struts2</filter-name>
  5. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  6. </filter>
  7. <filter-mapping>
  8. <filter-name>struts2</filter-name>
  9. <url-pattern>/*</url-pattern>
  10. </filter-mapping>
  11. <display-name></display-name>
  12. <welcome-file-list>
  13. <welcome-file>index.jsp</welcome-file>
  14. </welcome-file-list>
  15. </web-app>

web.xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <constant name="struts.devMode" value="true"/>
  7. <package name="hello" extends="struts-default" namespace="/">
  8. <action name="files" class="com.xiaostudy.web.UpFiles" method="upFiles">
  9. <result name="success">/ok.jsp</result>
  10. <result name="input">/err.jsp</result>
  11. </action>
  12. </package>
  13. </struts>

struts.xml代码

  1. package com.xiaostudy.web;
  2.  
  3. import java.io.File;
  4.  
  5. import org.apache.struts2.ServletActionContext;
  6.  
  7. import com.opensymphony.xwork2.ActionSupport;
  8.  
  9. public class UpFiles extends ActionSupport {
  10.  
  11. public String username;
  12. public File[] files;
  13. public String filesFileName[];
  14. public String filesContentType[];
  15.  
  16. public String upFiles() {
  17.  
  18. String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files");
  19. File file = new File(path);
  20. if(!file.exists()) {
  21. file.mkdirs();
  22. System.out.println("创建了文件夹》》》》》》");
  23. }
  24. for(int i = 0; i < files.length && files != null; i++) {
  25. File file2 = new File(file, filesFileName[i]);
  26. System.out.println(files[i]);
  27. files[i].renameTo(file2);
  28. System.out.println(file);
  29. System.out.println(file2);
  30. }
  31.  
  32. return SUCCESS;
  33. }
  34.  
  35. public String getUsername() {
  36. return username;
  37. }
  38.  
  39. public void setUsername(String username) {
  40. this.username = username;
  41. }
  42.  
  43. public File[] getFiles() {
  44. return files;
  45. }
  46.  
  47. public void setFiles(File[] files) {
  48. this.files = files;
  49. }
  50.  
  51. public String[] getFilesFileName() {
  52. return filesFileName;
  53. }
  54.  
  55. public void setFilesFileName(String[] filesFileName) {
  56. this.filesFileName = filesFileName;
  57. }
  58.  
  59. public String[] getFilesContentType() {
  60. return filesContentType;
  61. }
  62.  
  63. public void setFilesContentType(String[] filesContentType) {
  64. this.filesContentType = filesContentType;
  65. }
  66.  
  67. }

action动作类UpFiles

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>struts2的一个例子</title>
  8. </head>
  9. <body>
  10. okokokok
  11. </body>
  12. </html>

ok.jsp代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 上传失败!!!
  11. </body>
  12. </html>

err.jsp代码

struts2——多文件上传的更多相关文章

  1. struts2的文件上传

    在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...

  2. jsp\struts1.2\struts2 中文件上传(转)

    jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...

  3. Struts2+Uploadify文件上传使用详解

    Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例是php版本的,本文将详细介绍Uploadify在java中的使用,您也可以点击下面的链接进行演示或下 ...

  4. Struts2 多文件上传

    Struts2多文件上传只需要将 单文件上传中的File变成File[]  即可,上篇文章:单文件上传 <form action="${pageContext.request.cont ...

  5. Struts2图片文件上传,判断图片格式和图片大小

    1. 配置Struts2能够上传的最大文件大小 使用Struts2进行文件上传的时候,Struts2默认文件大小最大为2MB,如果要传大一点的文件,就需要修改struts.xml配置文件,重新设置能够 ...

  6. Struts2实现文件上传下载功能(批量上传)

    今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...

  7. Struts2实现文件上传(四)

    Struts2实现文件上传 配置文件struts.xml <!-- /* * $Id: struts.xml 1364077 2012-07-21 12:57:02Z lukaszlenart ...

  8. Struts2实现文件上传(三)

    Struts2实现文件上传 配置文件web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  9. Struts2实现文件上传(二)

    Struts2实现文件上传 文件上传页面 file.jsp: <%@ page language="java" import="java.util.*" ...

  10. Struts2实现文件上传(一)

    Struts2实现文件上传 文件上传成功后结果页面 result.jsp: <%@ page language="java" import="java.util.* ...

随机推荐

  1. mysql5.7的密码

    [root@mysql ~]# grep "temporary password" /var/log/mysqld.log 2018-04-03T08:08:05.867624Z ...

  2. npm and node 的一些问题

    1. node 安装 n 模块 用来管理 node的版本 2.  初始化项目出现这个问题 Error: Attempt to unlock XXX, which hasn't been locked ...

  3. angularjs中的$destroy和$timeout

    module.controller("TestController", function($scope, $timeout) { var onTimeout = function( ...

  4. [LintCode] 二叉树的中序遍历

    The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and  ...

  5. pandas 报错:【sys:1: DtypeWarning: Columns (15) have mixed types. Specify dtype option on import or set low_memory=False.】

    错误原因 报错提示:“sys:1: DtypeWarning: Columns (15) have mixed types. Specify dtype option on import or set ...

  6. 流畅的python 对象引用 可变性和垃圾回收

    对象引用.可变性和垃圾回收 变量不是盒子 人们经常使用“变量是盒子”这样的比喻,但是这有碍于理解面向对象语言中的引用式变量.Python 变量类似于 Java 中的引用式变量,因此最好把它们理解为附加 ...

  7. 【我的Android进阶之旅】解决bug:You need to use a Theme.AppCompat theme (or descendant) with this activity.

    前言 今天用Android Studio 生成Activity的时候,默认继承AppCompatActivity ,而在AndroidManifest.xml我对该Activity设置了一个主题,然后 ...

  8. Git的使用(1)

    一.Git简介 Git 是一个开源的分布式版本控制软件,用以有效.高速的处理从很小到非常大的项目版本管理. Git 最初是由Linus Torvalds设计开发的,用于管理Linux内核开发.Git ...

  9. java jacob调用打印,word,excel横向打印

    public static boolean printOfficeFile(File f) { if (f != null && f.exists()) { String fileNa ...

  10. Taking a screen shot of a window using Delphi code is rather easy.

    Taking a screen shot of a window using Delphi code is rather easy. A screen shot (screen capture) is ...