zoukankan      html  css  js  c++  java
  • WEB文件上传二 (Struts 文件上传)

    Struts文件长传

     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 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'fileUpLoad.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26 
    27   <!-- method="post" enctype="multipart/from-data"必须这样的 -->
    28     <form action="FileUpload" method="post" enctype="multipart/form-data">
    29     username:<input type="text" name="username"><br>
    30     file:<input type="file" name="file"><br>
    31         <input type="submit" value="submit">
    32     </form>
    33   </body>
    34 </html>
    fileupload.jsp

    FileUpload.action

     1 package fileupload;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.InputStream;
     7 import java.io.OutputStream;
     8 
     9 import org.apache.struts2.ServletActionContext;
    10 
    11 import com.opensymphony.xwork2.ActionSupport;
    12 
    13 public class FileUpload extends ActionSupport
    14 {
    15     private String username;
    16     private File file;
    17     private String fileContentType;
    18     private String fileFileName;
    19     public String getUsername()
    20     {
    21         return username;
    22     }
    23     public void setUsername(String username)
    24     {
    25         this.username = username;
    26     }
    27     public File getFile()
    28     {
    29         return file;
    30     }
    31     public void setFile(File file)
    32     {
    33         this.file = file;
    34     }
    35     public String getFileContentType()
    36     {
    37         return fileContentType;
    38     }
    39     public void setFileContentType(String fileContentType)
    40     {
    41         this.fileContentType = fileContentType;
    42     }
    43     public String getFileFileName()
    44     {
    45         return fileFileName;
    46     }
    47     public void setFileFileName(String fileFileName)
    48     {
    49         this.fileFileName = fileFileName;
    50     }
    51     
    52     @Override
    53     public String execute() throws Exception
    54     {
    55         String root = ServletActionContext.getRequest().getRealPath("/upload");
    56         System.out.println("存储的路径是:" + root);
    57         InputStream in = new FileInputStream(file);
    58         System.out.println(fileFileName);
    59         File file2 = new File(root,fileFileName);
    60         OutputStream os = new FileOutputStream(file2);
    61         int length = 0;
    62         byte[] buffer = new byte[400];
    63         while(-1!=(length = in.read(buffer)))
    64         {
    65             os.write(buffer, 0, length);
    66         }
    67         in.close();
    68         os.close();
    69         
    70         return SUCCESS;
    71     }
    72     
    73     
    74     
    75     
    76     
    77     
    78     
    79     
    80     
    81     
    82     
    83     
    84     
    85     
    86     
    87     
    88     
    89     
    90     
    91     
    92     
    93     
    94     
    95     
    96     
    97     
    98 }
    FileUpLoad.java

    这里的三个属性要注意

    private File file;
    private String fileContentType;
    private String fileFileName;  这三个属性分别得是用来封装 上传文件 名字 类型的。

    名字都是固定的,再表单里要上传的File的名字是什么就因该命名为什么  例如 :file:<input type="file" name="abc"><br>,

    则三个属性的名为为:

    private File abc;
    private String abcContentType;
    private String abcFileName;

    最后莫忘记再struts.xml配置action信息

    1         <action name="FileUpload" class="fileupload.FileUpload">
    2             <result name="success">
    3                 /FileUpLoadResult.jsp
    4             </result>
    5         </action>
    FileUpLoadResult.jsp页面展示文件名以及文件类型。

    <s:property value="fileFileName"/><br>
    <s:property value = "fileContentType"/>

    多个文件长传与上面的单个文件上传的方式相差无几,就是把封装文件,文件名,文件雷系的三个的属性改为LIST

     1 package fileupload;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.InputStream;
     7 import java.io.OutputStream;
     8 import java.util.List;
     9 
    10 import org.apache.struts2.ServletActionContext;
    11 
    12 import com.opensymphony.xwork2.ActionSupport;
    13 
    14 public class MultiFileUpLoad extends ActionSupport
    15 {
    16     /**
    17      * 
    18      */
    19     private static final long serialVersionUID = 3691843913552291529L;
    20     private String username;
    21     private List<File> file;
    22     private List<String> fileFileName;
    23     private List<String> fileContentType;
    24     public String getUsername()
    25     {
    26         return username;
    27     }
    28     public void setUsername(String username)
    29     {
    30         this.username = username;
    31     }
    32     public List<File> getFile()
    33     {
    34         return file;
    35     }
    36     public void setFile(List<File> file)
    37     {
    38         this.file = file;
    39     }
    40     public List<String> getFileFileName()
    41     {
    42         return fileFileName;
    43     }
    44     public void setFileFileName(List<String> fileFileName)
    45     {
    46         this.fileFileName = fileFileName;
    47     }
    48     public List<String> getFileContentType()
    49     {
    50         return fileContentType;
    51     }
    52     public void setFileContentType(List<String> fileContentType)
    53     {
    54         this.fileContentType = fileContentType;
    55     }
    56     
    57     @Override
    58     public String execute() throws Exception
    59     {
    60         String root = ServletActionContext.getRequest().getRealPath("/upload");
    61         
    62         for(int i = 0;i<file.size();i++)
    63         {
    64             File out = new File(root,fileFileName.get(i));
    65             InputStream in = new FileInputStream(file.get(i));
    66             OutputStream os = new FileOutputStream(out);
    67             byte[] b = new byte[400];
    68             int length = 0;
    69             while(-1!=(length = in.read(b)))
    70             {
    71                 os.write(b, 0, length);
    72             }
    73             os.close();
    74             in.close();
    75         }
    76         
    77         return SUCCESS;
    78     }
    79 }
    MultiFileUpLoad

    结果页的展示与上面一样

    莫忘记配置struts.xml

    1 <action name="multlfileupload" class="fileupload.MultiFileUpLoad">
    2             <result name="success">
    3                 /FileUpLoadResult.jsp
    4             </result>
    5             <interceptor-ref name="fileUpload">
    6                 <param name="maximumSize">99999999999</param>
    7             </interceptor-ref>
    8             <interceptor-ref name="defaultStack"></interceptor-ref>
    9         </action>
    5             <interceptor-ref name="fileUpload"> struts 再处理文件上传的拦截器 fileUpload 可去strurs-....core.jar 中查看下源码
     6 <param name="maximumSize">99999999999</param> 制定默认上传问价大小的最大值。 7 </interceptor-ref> 8 <interceptor-ref name="defaultStack"></interceptor-ref>
  • 相关阅读:
    判断整除
    洛谷2018-7月月赛
    luogu_P1177 【模板】快速排序 (快排和找第k大的数)
    lowbit() 运算
    64位整数乘法 (二进制思想)
    poj_1995 Raising Modulo Numbers (快速幂)
    poj_3179 Corral the Cows (二分+二维前缀和+离散化)
    Spring-profile 不同环境配置方法
    Spring-id,name 名称,别名关系
    Leecode no.20 合理的括号
  • 原文地址:https://www.cnblogs.com/mauiie/p/3750995.html
Copyright © 2011-2022 走看看