激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語(yǔ)言|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - JS+Struts2多文件上傳實(shí)例詳解

JS+Struts2多文件上傳實(shí)例詳解

2021-05-28 13:39襲烽 Java教程

這篇文章主要為大家詳細(xì)介紹了JS+Struts2多文件上傳實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js struts2多文件上傳的具體代碼,供大家參考,具體內(nèi)容如下

1、jsp頁(yè)面:

js控制增加刪除多個(gè)上傳文件框,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<%@ page language="java" pageencoding="utf-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
 
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml"
  <head> 
    <%@include file="../../_head.html"%> 
    <title>文件上傳</title> 
    <meta http-equiv="pragma" content="no-cache"
    <meta http-equiv="cache-control" content="no-cache"
    <meta http-equiv="expires" content="0"
    <script language="javascript" type="text/javascript"
      src="../js/common/common.js"></script> 
    <script type="text/javascript"
        
       var pos = 1
      
       function addfilecomponent() { 
        var eltable = document.getelementbyid('uploadtable').getelementsbytagname('tbody')[0]; 
        var eltr = document.getelementbyid('filetr'); 
        var eltr2 = document.getelementbyid('op'); 
        var neweletr = eltr.clonenode(true); 
        neweletr.id = "filetr" + pos;    
        neweletr.style.display = ""
        inputs = neweletr.getelementsbytagname('input'); 
        inputs[0].id="file" + pos; 
        var elinput = inputs[1]; 
        elinput.onclick=delfilecomponent; 
        elinput.id="delbutton" + pos++; 
        eltable.insertbefore(neweletr, eltr2); 
       
 
      function delfilecomponent() { 
        var eltable = document.getelementbyid('uploadtable').getelementsbytagname('tbody')[0]; 
        var trarr = eltable.getelementsbytagname("tr"); 
        var el = event.srcelement; 
        for(j = 0; j < trarr.length; j++) { 
          tr = trarr[j]; 
          if(tr.getelementsbytagname("input")[1] == el) { 
            eltable.removechild(tr); 
            pos--; 
            break
          
        
      
        
      function isvalidatefile(obj){ 
        var extend = obj.value.substring(obj.value.lastindexof(".")+1); 
        if(extend==""){ 
        }else
          if(!(extend=="xls"||extend=="doc")){ 
           alert("請(qǐng)上傳后綴名為xls或doc的文件!"); 
           var nf = obj.clonenode(true); 
           nf.value=''
           obj.parentnode.replacechild(nf, obj); 
           return false
          
        
        return true
      
    </script> 
  </head> 
  <body> 
    <%@ include file="/common/message.jsp"%> 
    <div class="body-box"
      <div class="rhead"
        <div class="rpos"
          文件上傳(可同時(shí)上傳多份文件) 
        </div> 
        <div class="clear"></div> 
      </div> 
      <s:form id="ops" action="csc_muploadfile" theme="simple"
        cssclass="rhead" enctype = "multipart/form-data"
        <table id="uploadtable" width="100%" border="0"
          <tr> 
            <td> 
              <input type="file" id="file0" name="uploadfile" size="50"
                onchange="isvalidatefile(this);" /> 
            </td> 
          </tr> 
          <tr id="filetr" style="display: none;"
            <td> 
              <input type="file" size="50" name="uploadfile"
                onchange="isvalidatefile(this);" /> 
                
              <input type="button" value="刪除" /> 
            </td> 
          </tr> 
          <tr id="op"
            <td> 
              <input type="submit" id="uploadbutton" value="上傳" /> 
                
              <input type="button" value="添加" id="addbutton"
                onclick="addfilecomponent();" /> 
                
            </td> 
          </tr> 
        </table> 
      </s:form> 
      <table class="pn-ltable" width="100%" cellspacing="1" cellpadding="0"
        border="0"
        <thead class="pn-lthead"
          <tr> 
            <th> 
              序號(hào) 
            </th> 
            <th> 
              文件名 
            </th> 
            <th> 
              上傳時(shí)間 
            </th> 
          </tr> 
        </thead> 
        <tbody class="pn-ltbody"
          <tr onmouseover="pn.ltable.lineover(this);"
            onmouseout="pn.ltable.lineout(this);"
            onclick="pn.ltable.lineselect(this);"
            <td> 
            </td> 
            <td> 
            </td> 
            <td> 
            </td> 
          </tr> 
        </tbody> 
      </table> 
    </div> 
  </body> 
</html>

2、action后臺(tái)處理上傳文件:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//uploadfile對(duì)應(yīng)頁(yè)面<input type="file" name="uploadfile">
private list<file> uploadfile; 
//文件名對(duì)應(yīng)uploadfile+“filename”,要不獲取不到文件名
private list<string> uploadfilefilename;  
// 文件上傳 
public string muploadfile() { 
  if (null == uploadfile) { 
  this.addactionerror("請(qǐng)上傳文件!"); 
  } else
  string filename = ""
   try
           //在自己代碼中控制文件上傳的服務(wù)器目錄
     string directory = servletactioncontext.getservletcontext().getrealpath("/uploads"); 
           //判斷該目錄是否存在,不存在則創(chuàng)建
           fileutil.makedir(directory); 
           //循環(huán)處理上傳的文件
      for(int i=0,j=uploadfile.size();i<j;i++){ 
        filename = uploadfilefilename.get(i); 
        string filepath = directory + file.separator + filename; 
        fileutil.uploadfile(uploadfile.get(i), new file(filepath)); 
      
    } catch (ioexception e) { 
        this.addactionmessage(""); 
    
      this.addactionmessage("文件上傳成功!"); 
  
  return "fileupload"
}

fileutil代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class fileutil {
 
 private static final int buffer_size = 16 * 1024;
 
 public static void uploadfile(file src, file dst) throws ioexception {
 
 inputstream in = null;
 outputstream out = null;
 try {
  in = new bufferedinputstream(new fileinputstream(src), buffer_size);
  out = new bufferedoutputstream(new fileoutputstream(dst),
   buffer_size);
  byte[] buffer = new byte[buffer_size];
  while (in.read(buffer) > 0) {
  out.write(buffer);
  }
 } finally {
  if (null != in) {
  in.close();
  }
  if (null != out) {
  out.close();
  }
 }
 
 }
 
 public static string getextention(string filename) {
 int pos = filename.lastindexof(".");
 return filename.substring(pos);
 }
 
 public static void makedir(string directory) {
 file dir = new file(directory);
 
 if (!dir.isdirectory()) {
  dir.mkdirs();
 }
 
 }
 
 public static string generatefilename(string filename)
  throws unsupportedencodingexception {
 dateformat format = new simpledateformat("yymmddhhmmss");
 string formatdate = format.format(new date());
 string extension = filename.substring(filename.lastindexof("."));
 filename = new string(filename.getbytes("iso8859-1"), "gb2312");
 return filename + "_" + formatdate + new random().nextint(10000)
  + extension;
 }
 
}

擴(kuò)展:

1.可以實(shí)現(xiàn)帶進(jìn)度條的上傳與下載;
2.可以用xml文件記錄上傳的文件清單,并且可以根據(jù)頁(yè)面對(duì)上傳文件的操作來(lái)修改相應(yīng)的xml文件;

完畢!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/shimiso/article/details/6045169

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91精品国产福利尤物免费 | 成年人高清视频在线观看 | 在线观看一二区 | 黄色特级毛片 | 少妇色诱麻豆色哟哟 | 在线免费观看毛片视频 | 特级无码毛片免费视频尤物 | 国产一区二区影视 | 黄色av免费网站 | 韩国精品久久久 | 成人久久久久久久久 | 九草在线| 神马顶级推理片免费看 | 一级黄色在线免费观看 | 草久免费 | 美女一级毛片 | 久久蜜臀 | 欧美精品a∨在线观看不卡 午夜精品影院 | 在线成人影视 | 一级黄色淫片 | 韩国19禁在线 | 中文黄色一级片 | 国产在线观看91精品 | 亚洲网在线观看 | 久久精品4 | 激情综合视频 | 在线观看国产一区二区 | 超级av在线| 国产精品久久久久久久久久久久久久久久 | 亚洲午夜影院在线观看 | 免费中文视频 | 国产高清永久免费 | 日韩一级片一区二区三区 | 国产女同玩人妖 | 免费一级毛片观看 | 亚洲草逼视频 | 亚洲国产精品久久久久久久久久 | 国产va在线观看 | 日本不卡二区 | 在线视频观看国产 | 久久精品中文字幕一区二区三区 |