Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 455|回复: 0

[struts学习]Struts2中多文件上传

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-11 05:11:04 | 显示全部楼层 |阅读模式
    一、上传页面:  <%@ page language="java" contentType="text/HTML; charset=GBK"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
       <title>使用数组上传多个文件</title>
    </head>
    <body>
    <s:fielderror/>
    <form action="upload.action" method="post" enctype="multipart/form-data">
       文件标题:<input type="text" name="title" /><br>
       选择第一个文件:<input type="file" name="upload" /><br>
       选择第二个文件:<input type="file" name="upload" /><br>
       选择第三个文件:<input type="file" name="upload" /><br>
    <input value="上传" type="submit" />
    </form>
    </body>
    </html>



    二、上传成功页面: <%@ page language="java" contentType="text/html; charset=GBK"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <html>
    <head>
    <title>上传成功</title>
    </head>
    <body>
    上传成功!<br>
       文件标题:<s:property value=" + title"/><br>
       第一个文件为:<img src="<s:property value=""upload/" + uploadFileName[0]"/>"/><br>
       第二个文件为:<img src="<s:property value=""upload/" + uploadFileName[1]"/>"/><br>
       第三个文件为:<img src="<s:property value=""upload/" + uploadFileName[2]"/>"/><br>  </body>
    </html>



    三、动作类
    1. package lee;
    2. import com.opensymphony.xwork2.Action;
    3. import org.apache.struts2.ServletActionContext;
    4. import java.io.File;
    5. import java.io.*;
    6. import com.opensymphony.xwork2.ActionSupport;
    7. /**
    8. * @author  yeeku.H.lee kongyeeku@163.com
    9. * @version  1.0
    10. * Copyright (C), 2005-2008, yeeku.H.Lee
    11. * This program is protected by copyright laws.
    12. * Program Name:
    13. * Date:
    14. */

    15. public class UploadAction extends ActionSupport
    16. {
    17.         private String title;
    18.     private File[] upload;
    19.     private String[] uploadContentType;
    20.     private String[] uploadFileName;
    21.         //接受依赖注入的属性
    22.     private String savePath;
    23.         //接受依赖注入的方法
    24.     public void setSavePath(String value)
    25.         {
    26.         this.savePath = value;
    27.     }
    28.     private String getSavePath() throws Exception
    29.         {
    30.         return ServletActionContext.getRequest().getRealPath(savePath);
    31.     }
    32.        
    33.         public void setTitle(String title) {
    34.                 this.title = title;
    35.         }
    36.         public void setUpload(File[] upload) {
    37.                 this.upload = upload;
    38.         }
    39.         public void setUploadContentType(String[] uploadContentType) {
    40.                 this.uploadContentType = uploadContentType;
    41.         }
    42.         public void setUploadFileName(String[] uploadFileName) {
    43.                 this.uploadFileName = uploadFileName;
    44.         }
    45.         public String getTitle() {
    46.                 return (this.title);
    47.         }
    48.         public File[] getUpload() {
    49.                 return (this.upload);
    50.         }
    51.         public String[] getUploadContentType() {
    52.                 return (this.uploadContentType);
    53.         }
    54.         public String[] getUploadFileName() {
    55.                 return (this.uploadFileName);
    56.         }
    57.         @Override
    58.     public String execute() throws Exception
    59.         {
    60.                 File[] files = getUpload();
    61.                 for (int i = 0 ; i < files.length ; i++)
    62.                 {
    63.                         //以服务器的文件保存地址和原文件名建立上传文件输出流
    64.                         FileOutputStream fos = new FileOutputStream(getSavePath() + "" + getUploadFileName()[i]);
    65.                         FileInputStream fis = new FileInputStream(files[i]);
    66.                         byte[] buffer = new byte[1024];
    67.                         int len = 0;
    68.                         while ((len = fis.read(buffer)) > 0)
    69.                         {
    70.                                 fos.write(buffer , 0 , len);
    71.                         }
    72.                 }
    73.          fos.close();// 注意:流应当关闭。
    74.          fis.close();
    75.         return SUCCESS;
    76.     }
    77. }
    复制代码
    四、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>  <constant name="struts.custom.i18n.resources" value="globalMessages"/>
    <constant name="struts.i18n.encoding" value="GBK"/>  <package name="upload" extends="struts-default">

    <action name="upload" class="lee.UploadAction">
       <interceptor-ref name="fileUpload">
         <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param>
       </interceptor-ref>
       <interceptor-ref name="defaultStack"/>
       <param name="savePath">/upload</param>
       <result name="input">/upload.jsp</result>
       <result>/succ.jsp</result>
    </action>

    </package>
    </struts>

      
      
       
       

         
       

         
       
      
    复制代码

    源码下载:http://file.javaxxz.com/2014/10/11/051100218.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 07:23 , Processed in 0.297807 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表