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


    今天来发布一个使用Struts2上传下载的项目,

    struts2为文件上传下载提供了好的实现机制,

    首先,可以先看一下我的项目截图

    关于需要使用的jar包,需要用到commons-fileupload-1.3.1.jar,commons-io-2.2.jar包,有想要的可以联系我,

    1. 现在让我们来看一下jsp界面:(使用了js,完成批量上传的功能)

    具体代码如下(NewFile.jsp):

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>Insert title here</title>
     8 <script type="text/javascript">
     9     function addfile() {
    10         var i = document.createElement("input");
    11         i.setAttribute("type","file");
    12         i.setAttribute("name","doc");
    13         
    14         var d = document.createElement("div");
    15         
    16         document.getElementById("ll").appendChild(i);
    17         document.getElementById("ll").appendChild(d);
    18     }
    19 </script>
    20 </head>
    21 <body>
    22     <form action="upload" method="post" enctype="multipart/form-data">
    23     <div id="ll">
    24         上传<input type="file" name="doc" /><br/>
    25         </div>
    26         <a href="javascript:addfile()">继续上传</a>
    27         <input type="submit" value="上传" />
    28     </form>
    29 </body>
    30 </html>

    2. form表单提交至upload.action代码如下:

     1 package com.llh.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.util.Date;
     8 import java.util.Random;
     9 
    10 import org.apache.struts2.ServletActionContext;
    11 import org.apache.struts2.convention.annotation.Action;
    12 import org.apache.struts2.convention.annotation.InterceptorRef;
    13 import org.apache.struts2.convention.annotation.Result;
    14 
    15 import com.opensymphony.xwork2.ActionSupport;
    16 
    17 public class UploadAction extends ActionSupport {
    18 
    19     /**
    20      * 
    21      */
    22     private static final long serialVersionUID = 1L;
    23 
    24     private File doc[];
    25 
    26     private String docFileName[];
    27 
    28     public File[] getDoc() {
    29         return doc;
    30     }
    31 
    32     public void setDoc(File[] doc) {
    33         this.doc = doc;
    34     }
    35 
    36     public String[] getDocFileName() {
    37         return docFileName;
    38     }
    39 
    40     public void setDocFileName(String[] docFileName) {
    41         this.docFileName = docFileName;
    42     }
    43 
    44     @Action(value = "upload", interceptorRefs = { @InterceptorRef(value = "fileUpload", params = { "allowedExtensions",
    45             ".jpg,.txt", "maximumSize", "2000000" }), @InterceptorRef(value = "defaultStack") }, results = {
    46                     @Result(name = "success", location = "/success.jsp"),
    47                     @Result(name = "input", location = "/error.jsp") })
    48     public String execute() {
    49 
    50         for (int i = 0; i < doc.length; i++) {
    51             File doc1 = doc[i];
    52             String docFileName1 = docFileName[i];
    53 
    54             String path = ServletActionContext.getServletContext().getRealPath("/upload/" + changeName(docFileName1));
    55             FileInputStream fis = null;
    56             FileOutputStream fos = null;
    57             try {
    58                 fis = new FileInputStream(doc1);
    59                 fos = new FileOutputStream(path);
    60                 byte[] b = new byte[1024];
    61                 int length = fis.read(b);
    62                 while (length != -1) {
    63                     fos.write(b, 0, length);
    64                     length = fis.read(b);
    65                 }
    66             } catch (IOException e) {
    67                 // TODO Auto-generated catch block
    68                 e.printStackTrace();
    69             } finally {
    70                 try {
    71                     fis.close();
    72                     fos.close();
    73                 } catch (IOException e) {
    74                     // TODO Auto-generated catch block
    75                     e.printStackTrace();
    76                 }
    77             }
    78         }
    79         return SUCCESS;
    80     }
    81 
    82     public String changeName(String oldName) {
    83         int index = oldName.indexOf(".");
    84         String name = oldName.substring(0, index);
    85         String extension = oldName.substring(index, oldName.length());
    86         Date d = new Date();
    87         Random r = new Random();
    88         return name + d.getTime() + r.nextInt(999999) + extension;
    89 
    90     }
    91 
    92 }
  • 相关阅读:
    利用if else判断几点是什么时间段
    【UML】活动图介绍
    【UML】类图介绍
    jQuery Ajax跨域问题简易解决方案
    ASP.NET MVC @Html.Label的问题
    Mysql Show ProcessList命令
    【ASP.NET MVC 学习笔记】- 20 ASP.NET Web API
    【ASP.NET MVC 学习笔记】- 19 REST和RESTful Web API
    【ASP.NET MVC 学习笔记】- 18 Bundle(捆绑)
    【ASP.NET MVC 学习笔记】- 17 Model验证
  • 原文地址:https://www.cnblogs.com/javallh/p/javallh.html
Copyright © 2020-2023  润新知