茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    springMVC結(jié)合AjaxForm上傳文件
    來源:易賢網(wǎng) 閱讀:1497 次 日期:2016-07-28 15:12:51
    溫馨提示:易賢網(wǎng)小編為您整理了“springMVC結(jié)合AjaxForm上傳文件”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了springMVC結(jié)合AjaxForm上傳文件的相關(guān)資料,感興趣的小伙伴們可以參考一下

    最近在項目中需要上傳文件文件,之前一直都是form提交的,嘗試了一下AjaxForm,感覺還比較好用,寫篇隨筆mark下,供以后使用。

    準(zhǔn)備工作:

    下載jquery-form.js

    相關(guān)jar:

    commons-fileupload-1.1.1.jar

    commons-io-1.3.2.jar 

    在spring-servlet.xml進行multipartResolver配置:

    <bean id="multipartResolver"

     class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

      <property name="defaultEncoding" value="utf-8" />

      <property name="maxUploadSize" value="10485760000" />

      <property name="maxInMemorySize" value="40960" />

    </bean> 

    這個是必須的,否則不好用。

    頁面:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8" session="false" %>

    <html>

    <!-- 

    - Author(s): xieshuang

    - Date: 2016-06-20 13:46:20

    - Description:

    -->

    <head>

    <title>Title</title>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>

    <script src="<%=request.getContextPath()%>/common/nui/nui.js" type="text/javascript"></script>

    <script src="<%=request.getContextPath()%>/common/nui/jquery/jquery-form.js" type="text/javascript"></script>

    <script type="text/javascript" src="<%=request.getContextPath()%>/page4nui/master/projecttype/js/projecttype_import.js"></script>

    <script type="text/javascript">

      var contextPath="<%=request.getContextPath()%>";

    </script>

    </head>

    <body>

    <div id="test" class="nui-fit" style="padding-top:5px;min-width:300px;min-height:180px;">

      <form id="fileUpload" method="post" enctype="multipart/form-data">

      <div id="dataImport" style="width:100%;overflow:hidden;">

        <table style="width:100%;table-layout:fixed;" class="nui-form-table" >

          <tr>

            <th align="right" style="width:25%" >選擇文件:</th>

            <td>

              <input id="uploadFile" type="file" name="file" style="width:90%;"><font style="color:red;width:5%;"> *</font>

            </td>

          </tr>

        </table>   

      </div>

      <div style="width:100%;padding-top:10px;" align="center">

        <input style="width:60px;" iconCls="icon-ok" value="確定" type="submit" />

        <span style="display:inline-block;width:25px;"></span>

        <a class="nui-button" iconCls="icon-cancel" style="width:60px;" onclick="cancel">取消</a>

      </div> 

      </form>     

    </div>

    </body>

    </html> 

    核心js:

    var msg;

    $(function(){

    nui.parse();

    //ajax配置

    var options = { 

        url: contextPath+"/webapp/cfProjectType/importExcel",

        beforeSubmit: showRequest, //提交前處理 

        success:    showResponse, //處理完成 

        resetForm: true, 

        dataType: 'json'

        }; 

      $('#fileUpload').submit(function() { //注意

        $(this).ajaxSubmit(options); 

        return false;//防止dialog 自動關(guān)閉

       });

    })

    //執(zhí)行成功回調(diào)函數(shù)

    function showResponse(e) {

      nui.hideMessageBox(msg);

      if (e.importFlag == true) {

        CloseWindow("ok");

      } else {

        //對錯誤的一些處理

      }

    }

    //提交前的一些校驗

    function showRequest(formData, jqForm, options){

      if(formData[0].value=="" || formData[0].value==null){

        nui.alert("請選擇文件");

        return false;

      }

      var fileName = $("#uploadFile").val().split("\\").pop();

      var strs = new Array(); //定義一數(shù)組

      strs = fileName.split('.');

      var suffix = strs [strs .length - 1];

      if (suffix != 'xls' && suffix != 'xlsx') {

        nui.alert("請選擇excel文件!");

        return false;

      }

      msg = nui.loading("Loading", "Please waiting");

    java代碼:

    @SuppressWarnings("unchecked")

    @RequestMapping("/webapp/cfProjectType/importExcel")

    @ResponseBody

    public Map<String, Object> importExcel(@RequestParam("file") MultipartFile[] files, HttpServletRequest request)

        throws Throwable {

      //long starttiem = System.currentTimeMillis();

      InputStream fis;

      fis = null;

      File fileIn = null;

      try {

        for (MultipartFile myfile : files) {

          if (!myfile.isEmpty()) {

            String realPath = request.getSession().getServletContext().getRealPath("/export");

            fileIn = new File(realPath);

            //判斷上傳文件的保存目錄是否存在

            if (!fileIn.exists() && !fileIn.isDirectory()) {

              //創(chuàng)建目錄

              fileIn.mkdirs(路徑);

            }

            //將上傳的文件復(fù)制到文件夾下

            myfile.transferTo(new File(路徑+文件名));

          }

          }

        }  

    這里我之前用過另外一個方法FileUtils.copyInputStreamToFile(InputStream arg0, File arg1)同樣能將文件保存到路徑下面

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

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機網(wǎng)站地址:springMVC結(jié)合AjaxForm上傳文件
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報班

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)