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

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数据导入到数据库当中,这部分待我测试成功之后再贴出来。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值