【springmvc】java web使用ajaxSubmit方法实现Excel文件上传

Java 发表评论


servlet-context.xml配置:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="20000000"/>
</bean>  

需要组件

  • jquery.form.js
  • jquery-2.1.4.min.js

前台代码:

<head>
    <!--……(此处其他代码省略)-->
<script type="text/javascript" src="<%=path %>/js/jquery/jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="<%=path %>/js/jquery/jquery.form.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#importBuildInfo").bind("click",function(){
                var excelPath = $("#excelPath").val();
                if(excelPath == null || excelPath == ''){
                    alert("请选择要上传的Excel文件");
                    return;
                }else{
                var fileExtend = excelPath.substring(excelPath.lastIndexOf('.')).toLowerCase(); 
                    if(fileExtend == '.xls'){
                        $("#upload").ajaxSubmit({
                              url:"<%=path %>/rest/upload/importBuildInfoExcel",
                              cache:false,
                              dataType:'json',
                              success: function(data) {
                                if(data.result!=null){
                                   $.each(data.result,function(i,value){
                                       alert(value);
                                   });
                                }
                                alert("表单已提交!");
                              } ,
                              error:function(){
                                  alert("error");
                              }
                           });
                    }else{
                        alert("文件格式需为'.xls'格式");
                       return;
                    }
                }
            });
        });
    </script>
</head>
<body>
    <form  id="upload" enctype="multipart/form-data" action="<%=path %>/rest/upload/importBuildInfoExcel" method="post">
        <input name="excelPath" id="excelPath"   type="file" />
        <input type="button" id="importBuildInfo" value="从excel导入"/>
    </form>
</body>

Controller代码:

import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

@Controller
@RequestMapping(value="/upload")
public class ImportCrtd {

    @RequestMapping(value="/importBuildInfoExcel", method = RequestMethod.POST) 
    public List importBuildInfoExcel(@RequestParam(value = "excelPath", required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws IOException{

        System.out.println("Success!!!");
        String path = request.getSession().getServletContext().getRealPath("importBuildInfoExcel");  
        String fileName = file.getOriginalFilename();  
        System.out.println(path);
        File targetFile = new File(path, fileName);
        if(!targetFile.exists()){  
            targetFile.mkdirs();  
        }  
        //保存  
        try {  
            file.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;
    }

结果:

到path路径下查看,可看到上传的文件,另外,可以对上传的文件进行一系列操作,比如将excel数据导入到数据库当中,这部分待我测试成功之后再贴出来。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

昵称 *