最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS+Struts2多文件上傳實例詳解

 更新時間:2018年08月29日 10:41:54   作者:襲烽  
這篇文章主要為大家詳細(xì)介紹了JS+Struts2多文件上傳實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

1、JSP頁面:

JS控制增加刪除多個上傳文件框,代碼如下:

<%@ 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("請上傳后綴名為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">  
          文件上傳(可同時上傳多份文件)  
        </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);" />  
              &nbsp;  
              <input type="button" value="刪除" />  
            </td>  
          </tr>  
          <tr id="op">  
            <td>  
              <input type="submit" id="uploadbutton" value="上傳" />  
              &nbsp;  
              <input type="button" value="添加" id="addbutton" 
                onClick="addFileComponent();" />  
              &nbsp;  
            </td>  
          </tr>  
        </table>  
      </s:form>  
      <table class="pn-ltable" width="100%" cellspacing="1" cellpadding="0" 
        border="0">  
        <thead class="pn-lthead">  
          <tr>  
            <th>  
              序號  
            </th>  
            <th>  
              文件名  
            </th>  
            <th>  
              上傳時間  
            </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后臺處理上傳文件:

//uploadFile對應(yīng)頁面<input type="file" name="uploadFile"> 
private List<File> uploadFile;  
//文件名對應(yīng)uploadFile+“FileName”,要不獲取不到文件名 
private List<String> uploadFileFileName;   
// 文件上傳  
public String mUploadFile() {  
  if (null == uploadFile) {  
  this.addActionError("請上傳文件!");  
  } 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代碼如下:

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.可以實現(xiàn)帶進(jìn)度條的上傳與下載;
2.可以用xml文件記錄上傳的文件清單,并且可以根據(jù)頁面對上傳文件的操作來修改相應(yīng)的xml文件;

完畢!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • Java實現(xiàn)郵箱發(fā)送功能實例(阿里云郵箱推送)

    Java實現(xiàn)郵箱發(fā)送功能實例(阿里云郵箱推送)

    這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)郵箱發(fā)送功能的相關(guān)資料,利用阿里云郵箱推送,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringAOP核心對象的創(chuàng)建圖解

    SpringAOP核心對象的創(chuàng)建圖解

    這篇文章主要介紹了SpringAOP核心對象的創(chuàng)建詳解,通過使用AOP,我們可以將橫切關(guān)注點(如日志記錄、性能監(jiān)控、事務(wù)管理等)從業(yè)務(wù)邏輯中分離出來,使得代碼更加模塊化、可維護(hù)性更高,需要的朋友可以參考下
    2023-10-10
  • SWT(JFace)體驗之FillLayout布局

    SWT(JFace)體驗之FillLayout布局

    FillLayout是非常簡單的一種布局方式,它會以同樣大小對父組件中的子組件進(jìn)行布局,這些子組件將以一行或一列的形式排列。
    2009-06-06
  • 一文掌握IDEA中的Maven集成與創(chuàng)建

    一文掌握IDEA中的Maven集成與創(chuàng)建

    maven是用來幫助我們快速搭建項目結(jié)構(gòu)與開發(fā)環(huán)境的好工具,這篇文章主要介紹了一文掌握IDEA中的Maven集成與創(chuàng)建,需要的朋友可以參考下
    2023-02-02
  • java的靜態(tài)方法調(diào)用方式

    java的靜態(tài)方法調(diào)用方式

    這篇文章主要介紹了java的靜態(tài)方法調(diào)用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • SpringBoot將Spring fox更換為Springdoc的方法詳解

    SpringBoot將Spring fox更換為Springdoc的方法詳解

    由于項目中使用Spring fox已經(jīng)不維護(hù)更新了,代碼掃描,掃出問題,需要將Spring fox更換為Spring Doc,所以本文給大家介紹了SpringBoot將Spring fox更換為Springdoc的方法,文中有相關(guān)的代碼供大家參考,需要的朋友可以參考下
    2024-01-01
  • Springboot如何使用.yml配置端口號

    Springboot如何使用.yml配置端口號

    這篇文章主要介紹了Springboot如何使用.yml配置端口號問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 深入IDEA Debug問題透析詳解

    深入IDEA Debug問題透析詳解

    這篇文章主要為大家介紹了深入IDEA Debug問題透析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Java非侵入式API接口文檔工具apigcc用法詳解

    Java非侵入式API接口文檔工具apigcc用法詳解

    這篇文章主要介紹了Java非侵入式API接口文檔工具apigcc用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • MyBatis中select語句中使用String[]數(shù)組作為參數(shù)的操作方法

    MyBatis中select語句中使用String[]數(shù)組作為參數(shù)的操作方法

    在 MyBatis 中,如何在 mapper.xml 配置文件中 select 語句中使用 String[] 數(shù)組作為參數(shù)呢,并且使用IN關(guān)鍵字來匹配數(shù)據(jù)庫中的記錄,這篇文章主要介紹了MyBatis中select語句中使用String[]數(shù)組作為參數(shù),需要的朋友可以參考下
    2023-12-12

最新評論

固原市| 德兴市| 东宁县| 泗阳县| 景泰县| 平远县| 察隅县| 青冈县| 贺州市| 景谷| 高青县| 大港区| 长阳| 交口县| 巨鹿县| 南投县| 合作市| 青神县| 南丰县| 海伦市| 内江市| 安陆市| 清苑县| 汉川市| 宜兰县| 南昌县| 沁源县| 扎囊县| 诸城市| 类乌齐县| 万载县| 玛多县| 巴彦县| 图片| 蓝山县| 八宿县| 睢宁县| 永善县| 宁武县| 乌拉特前旗| 宁远县|