码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVc上传excel或csv文件

时间:2016-08-21 15:10:43      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

1、JSP页面代码

<form  enctype=""multipart/form-data"  method="post">
    <input type="file" name="file">
    <input type="submit" value="上传" >
</form>

 2、controller中代码

@RequestMapping("/sysfindheimingdan/excelmobanupload")
    @ResponseBody
        public void uploadExcelModel(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //开始上传excel
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
          MultipartFile multipartFile = multipartRequest.getFile("file");
          String sourceName = multipartFile.getOriginalFilename(); // 原始文件名
          Long size = multipartFile.getSize();
          String fileType = sourceName.substring(sourceName.lastIndexOf("."));
          System.out.println("上传的文件名为:"+sourceName+"类型为:"+fileType+"大小:"+size);
          String base = request.getSession().getServletContext().getRealPath("/upload//");  //获取文件上传的路径,在webapp下的upload中
          File file = new File(base);
          if(!file.exists()){
           file.mkdirs();
          }
          String path=base + File.separator + sourceName;
          multipartFile.transferTo(new File(path));
         
    }  

 

SpringMVc上传excel或csv文件

标签:

原文地址:http://www.cnblogs.com/guokai870510826/p/5792738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!