package cn.com.mcd.controller;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.com.mcd.common.ResultModel;
import cn.com.mcd.model.FileMain;
import cn.com.mcd.service.CustemerListService;
import cn.com.mcd.util.UtilDateTime;
import cn.com.mcd.util.UtilExcelToTxt;
import cn.com.mcd.util.constans.Constants;
import cn.com.mcd.util.constans.ConstantsError;
import jxl.read.biff.BiffException;
/**
* @author soya.song
* 2017.3.16
*/
@Controller
@RequestMapping("/custemerList")
@SuppressWarnings("unused")
public class CustemerListController implements Serializable{
private static final long serialVersionUID = 2732216546553695880L;
private static final Logger log = LoggerFactory.getLogger(CustemerListController.class);
@Resource
private CustemerListService custemerListService;
static String prefix="customer_";
static String suffixTo=".txt";

/**
* 导入
* @param id
* @return
*/
@RequestMapping(value = "/uploadCustList",method = RequestMethod.POST)
@ResponseBody
public ResultModel uploadCustList(@RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws IOException {
ResultModel resultModel=new ResultModel();
Map<String,Object> resultMap=new HashMap<String,Object>();
FileMain file=new FileMain();
log.info(this.getClass().getName()+".uploadCustList.start");
ResultModel result=new ResultModel();
String originalFilename="";//上传的文件的文件名

//校验上传文件上否为空
if(null==myfiles || myfiles.length<=0){
result.setResultMsg(Constants.SERVICE_ERROR_CODE);
result.setResultMsg(Constants.FILE_NULL_ERROR);
return result;
}
List<FileMain> resultList=new ArrayList<FileMain>();
int i=1;
for (MultipartFile myfile : myfiles) {
if (!myfile.isEmpty()) {
//获得文件后缀名
String suffix = myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
//检查文件格式是否正确
if(!".lsx".equals(suffix) && !".xlsx".equals(suffix) ){
resultModel.setResultCode(ConstantsError.CHECK_EXCEL_NOT_EXIST_CODE);//cus1002
resultModel.setResultMsg(ConstantsError.CHECK_EXCEL_NOT_EXIST_MSG);//the file must be excel
return resultModel;
}

//获得文件源名
originalFilename=myfile.getOriginalFilename();

//转为File
String path=request.getSession().getServletContext().getRealPath("/file/upload/");//生成一个目录
String uuid=prefix+UtilDateTime.nowDateToString();//customer_YYYYMMDDHHMM 
String fileName = uuid+ suffix; //文件全路径
String txtName = uuid+ suffixTo; //txt文件名(customer_YYYYMMDDHHMM.txt(时间为24小时制))
File f=new File(path,fileName);

//如果path路径不存在,创建一个文件夹
if(!f.exists()){
f.mkdirs();
}
try {
myfile.transferTo(f);//没有这句话,生成的文件就是一个文件夹。有了以后,就会在path路径下,生成一个文件
//1.将文件信息保存到数据库
Map<String,Object> map=custemerListService.saveExcel(f);
// String flg=(String) map.get("flag") ;
// if(!"0".equals(flg)){//说明保存数据库没有成功
// if("1".equals(flg)){//说明校验失败
// result.setResultData(map);
// }
// result.setResultCode(Constants.SERVICE_ERROR_CODE);//500
// result.setResultMsg(Constants.FILE_CHECK_ERROR);//导入文件数据有误
// log.info(this.getClass().getName()+".uploadCustList.文件上传保存到数据库校验失败result:"+result);
// return result;
// }
//2.将excel文件转换为txt文件
// UtilExcelToTxt xt = UtilExcelToTxt.getInstance();
// resultMap=xt.excelToTxt(path+fileName, path,txtName);

//custemerListService
//将txt文件传入sftp

//如果导入成功一个文件,记录一次
file.setFlag("successful");//成功
//如果导入成功一个文件,记录一次
} catch (IllegalStateException | IOException e) {
file.setFlag("failure");//成功
log.error(this.getClass().getName()+".uploadCustList.end.上传excel文件controller报错",e);
// throw new DataBaseAccessException(5001+Constants.FILE_UPLOAD_ERROR+e);//excel文件导入失败
}
//如果导入成功一个文件,记录一次
file.setName(fileName);
file.setNum(i);
resultList.add(file);//对象装入list
i++;
}
}
result.setResultData(resultList);
return result;
}
}

本博客中所有内容为本人自学及总结内容, 仅代表个人观点,如有错误,麻烦大家及时指出并提示我更正。谢谢
 

springmvc文件上传 参数为MultipartFile 转换为File的更多相关文章

  1. SpringMVC文件上传下载(单文件、多文件)

    前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...

  2. SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库

    SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库  /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...

  3. 18 SpringMVC 文件上传和异常处理

    1.文件上传的必要前提 (1)form 表单的 enctype 取值必须是:multipart/form-data(默认值是:application/x-www-form-urlencoded) en ...

  4. springmvc文件上传AND jwt身份验证

    SpringMVC文件上传 思路:1.首先定义页面,定义多功能表单(enctype=“multipart/form-data”)2.在Controller里面定义一个方法,用参数(MultipartF ...

  5. TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式

    1.传统文件上传方式 <!-- 文件上传需要的jar --> <dependency> <groupId>commons-fileupload</groupI ...

  6. springmvc文件上传下载简单实现案例(ssm框架使用)

    springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...

  7. springMVC文件上传大小超过限制的问题

    [转自]https://my.oschina.net/ironwill/blog/646762 springMVC是一个非常方便的web层框架,我们使用它的文件上传也非常的方便. 我们通过下面的配置来 ...

  8. 解决springMVC文件上传报错: The current request is not a multipart request

    转自:https://blog.csdn.net/HaHa_Sir/article/details/79131607 解决springMVC文件上传报错: The current request is ...

  9. SpringMVC——文件上传下载

    一.单文件上传 1.导入依赖 <dependency> <groupId>commons-io</groupId> <artifactId>common ...

随机推荐

  1. python进程之间的通信——Queue

    我们知道进程之间的数据是互不影响的,但有时我们需要在进程之间通信,那怎么办呢? 认识Queue 可以使用multiprocessing模块的Queue实现多进程之间的数据传递,Queue本身是一个消息 ...

  2. python基础之函数当中的装饰器

    在实际工作当中存在一个开放封闭原则 1.对扩展是开放的 为什么要对扩展开放呢? 我们说,任何一个程序,不可能在设计之初就已经想好了所有的功能并且未来不做任何更新和修改.所以我们必须允许代码扩展.添加新 ...

  3. mysql添加索引造成的影响

    尽管添加索引可以优化SQL语句的性能,但是添加索引的同时也会带来不小的开销.尤其是在有大量的索引的情况下. mysql添加索引造成的影响如下: 1.DML(数据操作语言)影响,在表上添加缩影会直接影响 ...

  4. 关于ionic4导入android studio的注意事项

    最近看IT营的视频的时候,发现视频讲解的打包真是轻松的不得了,但是当自己导入打包的时候,你就会发现,没有最坑,只有更坑,按照教程来打包,估计你这辈子很难还原成功的,下面就来说一下关于 gradle与g ...

  5. O013、动手实践Linux VLAN

    参考https://www.cnblogs.com/CloudMan6/p/5326737.html   本节我们来看如何在实验环境中实施和部署如下的VLAN 网络  

  6. Django框架——基础之视图系统(View.py)

    Django框架之View.py(视图文件) 1. 视图简介 视图层是Django处理请求的核心代码层,我们大多数Python代码都集中在这一层面. 它对外接收用户请求,对内调度模型层和模版层,统合数 ...

  7. Java泛型:利用泛型动态确认方法返回值类型

    根据泛型类型动态返回对象 public <T extends PackObject> T unPackMessage(String interfaceCode, String respVa ...

  8. Summer training round2 #4 (Training #20)

    A!:UESTC1752 B!:找区间内L到R之间内的数的个数  权值分块加莫队 C!:给你一个哈斯图 去掉其中的几条边 要求输出字典序最大的拓扑排序:线段树模拟拓扑排序 D!:要求你找到最短路树并输 ...

  9. HTML5日期时间输入类型注意事项(time,date)

    原文链接:http://www.webhek.com/post/html5-date.html 1.HTML5规范里只规定date新型input输入类型,并没有规定日历弹出框的实现和样式.所以,各浏览 ...

  10. 遗传算法解决寻路问题——Python描述

    概要 我的上一篇写遗传算法解决排序问题,当中思想借鉴了遗传算法解决TSP问题,本质上可以认为这是一类问题,就是这样认为:寻找到一个序列X,使F(X)最大. 详解介绍 排序问题:寻找一个序列,使得这个序 ...