zoukankan      html  css  js  c++  java
  • struts 多文件上传 xml 版本

    【本文简介】

    本文将介绍 以配置 struts.xml  的方式 实现 多文件上传的功能。

    【文件夹结构】

    【struts.xml】

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
     3 <struts>
     4  
     5     <!-- 结果页面存储文件夹 -->
     6     <constant name="struts.convention.result.path" value="/pages"/>
     7     <!-- URL资源分隔符 -->
     8     <constant name="struts.convention.action.name.separator" value="_" />
     9     <!-- 默认编码 -->
    10     <constant name="struts.i18n.encoding" value="UTF-8" />
    11     <!-- 开发模式,显示详细错误信息 -->
    12     <constant name="struts.devMode" value="true" />
    13     <!-- 是否每次HTTP请求到达时,系统都重新加载资源文件,默认false -->
    14     <constant name="struts.i18n.reload" value="false" />
    15     <!-- 是否在struts.xml修改后重新加载 ,默认false-->
    16     <constant name="struts.configuration.xml.reload" value="false"/>
    17     <!-- 浏览器是否缓存静态内容,默认true -->
    18     <constant name="struts.serve.static.browserCache" value="true" />
    19      
    20      
    21     <!-- ****************************以下是文件上传的设置*********************************** -->
    22     <!-- 指定国际化资源文件的baseName为messageResource -->
    23     <!-- 设置该应用使用的解码集 -->
    24     <constant name="struts.i18n.encoding" value="utf-8"/>
    25     <!-- 上传的全部文件的最大限制-->
    26     <constant name="struts.multipart.maxSize" value="1024102400"/>
    27     <!-- 设置存放临时文件的文件夹 -->
    28     <constant name="struts.multipart.saveDir" value="/tmp"></constant>
    29     <!-- ****************************以上是文件上传的设置*********************************** -->
    30      
    31      
    32     <!-- **************************** 以下是上传文件的action **************************** -->
    33     <package name="default" extends="struts-default">
    34         <action name="uploadFile" class="com.modelsystem.action.UploadFileAction" >
    35          
    36             <!-- 限制图片的格式和图片的大小 -->
    37             <interceptor-ref name="fileUpload">
    38                 <param name="allowedTypes">
    39                     image/bmp,image/png,image/gif,image/jpeg,image/jpg,text/plain
    40                 </param>
    41                 <param name="maximumSize">102400</param>
    42             </interceptor-ref>
    43              
    44             <!-- 默认的拦截器,必须要写 -->
    45             <interceptor-ref name="defaultStack" />
    46              
    47             <result name="success">/upload.jsp</result>
    48             <result name="input">/upload.jsp</result>
    49         </action>
    50     </package>
    51     <!-- **************************** 以上是上传文件的action **************************** -->
    52  
    53 </struts> 

    【JSP代码】

    注意:
    1. <s:form 里面不能少了 enctype="multipart/form-data" 
    2. 请求要post形式
    3. <s:file 里面的name="..."要是java代码里面的List<File> ... 对应。
     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6  
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <%@taglib uri="/struts-tags" prefix="s" %>
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12      
    13     <title>My JSP 'MyJsp2.jsp' starting page</title>
    14      
    15     <meta http-equiv="pragma" content="no-cache">
    16     <meta http-equiv="cache-control" content="no-cache">
    17     <meta http-equiv="expires" content="0">    
    18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    19     <meta http-equiv="description" content="This is my page">
    20     <!--
    21     <link rel="stylesheet" type="text/css" href="styles.css">
    22     -->
    23  
    24   </head>
    25    
    26   <body>
    27     <center>
    28     <font color="red"><s:fielderror/></font>
    29         <s:form action="uploadFile" method="post" enctype="multipart/form-data">
    30             <s:file name="file" label="文件1"></s:file>
    31             <s:file name="file" label="文件2"></s:file>
    32             <s:submit label="上传"/>
    33         </s:form>    
    34          
    35         <s:iterator value="fileFileName" status="length">
    36               <img src='upload/<s:property value="fileFileName.get(#length.index)"/>'>
    37           </s:iterator>
    38            
    39 </center>
    40   </body>
    41 </html>

    【java代码】

     1 package com.modelsystem.action;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 import java.io.OutputStream;
     9 import java.util.List;
    10 
    11 import org.apache.struts2.ServletActionContext;
    12 
    13 /**
    14  * @描述 struts 多文件上传 xml 版本
    15  * @作者   小M
    16  * @博客 http://www.cnblogs.com/xiaoMzjm/
    17  * @时间 2014/07/30
    18  */
    19 public class UploadFileAction extends BaseAction {
    20     
    21     private static final long serialVersionUID = 1L;
    22     
    23     // 上传的文件,struts会自动帮我们填充至此,因为多文件,所以用List
    24     private List<File> file;
    25     
    26     // 上传的文件的文件名,因为多文件,所以用List
    27     private List<String> fileFileName;
    28     
    29     // 上传的文件的类型,因为多文件,所以用List
    30     private List<String> fileContentType;
    31      
    32     public List<File> getFile() {
    33         return file;
    34     }
    35  
    36     public void setFile(List<File> file) {
    37         this.file = file;
    38     }
    39  
    40     public List<String> getFileFileName() {
    41         return fileFileName;
    42     }
    43  
    44     public void setFileFileName(List<String> fileFileName) {
    45         this.fileFileName = fileFileName;
    46     }
    47  
    48     public List<String> getFileContentType() {
    49         return fileContentType;
    50     }
    51  
    52     public void setFileContentType(List<String> fileContentType) {
    53         this.fileContentType = fileContentType;
    54     }
    55  
    56     /**
    57      * 文件上传关键方法。
    58      */
    59     public String execute() throws IOException{
    60         
    61         // 文件所放的文件夹,通过getRealPath获得服务器下项目的地址。避免地址写死。upload是webRoot下的一个存放文件的文件夹。
    62         String root = ServletActionContext.getServletContext().getRealPath("/")+"\upload\";        
    63          
    64         //循环上传的文件
    65         for(int i = 0 ; i < file.size() ; i ++){
    66             
    67             // 获取当前遍历到的文件,new 一个文件输入流,连接到该文件。
    68             InputStream is = new FileInputStream(file.get(i));
    69              
    70             // new 一个文件,连接到要存储的文件夹处。
    71             File destFile = new File(root,this.getFileFileName().get(i));
    72              
    73             // new 一个输出流,连接到要存储的文件处。
    74             OutputStream os = new FileOutputStream(destFile);
    75 
    76             // 字节流,规定可写入的字节数。
    77             byte[] buffer = new byte[is.available()];
    78             int length  = 0 ;
    79             
    80             // 开始写入文件
    81             while((length = is.read(buffer))>0){
    82                 os.write(buffer, 0, length);
    83             }
    84             is.close();
    85             os.close();
    86         }
    87         return SUCCESS;
    88     }
    89 }
  • 相关阅读:
    CSS 与 HTML5 响应式图片
    Sublime Text3中最常用的快捷键
    JSON.parse这个是啥?
    页面的拼装配置Appache SSI
    如何保障mysql和redis之间的数据一致性?(转发)
    ORACLE异常ORA-01843 || ORA-14411 || Invalid column index
    Cause: java.sql.SQLException: 无效的列索引
    java.lang.IllegalArgumentException: host parameter is null
    java.lang.ClassNotFoundException: org.apache.commons.codec.DecoderException 异常
    java.sql.SQLIntegrityConstraintViolationException: ORA-00001: 违反唯一约束条件
  • 原文地址:https://www.cnblogs.com/xiaoMzjm/p/3879136.html
Copyright © 2011-2022 走看看