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. Hive 教程(三)-DDL基础

    DDL,Hive Data Definition Language,数据定义语言: 通俗理解就是数据库与库表相关的操作,本文总结一下基本方法 hive 数据仓库配置 hive 数据仓库默认位置在 hd ...

  2. solr学习笔记-导入mysql数据

    操作系统:LINUX CENTOS 6.7 solr安装目录:/usr/local/solr-6.1.0 1.准备工作: 1.1.创建数据表: CREATE TABLE `mytable` ( `id ...

  3. Tomcat 设置80端口

    1:修改tomcat配置 vi /usr/local/tomcat/conf/server.xml 找到 Connector port="8080" protocol=" ...

  4. execjs执行js代码报错:Exception in thread Thread-1

    最近在爬一个js数据加密的网站的时候,出了点问题,困扰了我两天 直接运行js文件的时候正常,但是用execjs运行js代码的时候总是会报错 最后翻了很多博客之后,终于找到了原因:原因是有一个程序在使用 ...

  5. Hadoop网页监控配置

    接之前的内容http://www.cnblogs.com/jourluohua/p/8734406.html 在之前那的内容中,仅实现了Hadoop的安装和运行,距离实际使用还有很远.现在先完成一个小 ...

  6. Java高并发程序设计学习笔记(二):多线程基础

    转自:https://blog.csdn.net/dataiyangu/article/details/86226835# 什么是线程?线程的基本操作线程的基本操作新建线程调用run的一种方式调用ru ...

  7. golang常见的几种并发模型框架

    原文链接 package main import ( "fmt" "math/rand" "os" "runtime" ...

  8. oracle创建表前校验是否存在

    创建表前检查是否存在,并删除 --检查是否存在此表,存在则删除 declare num number; begin select count(1) into num from user_tables ...

  9. Delphi Button组件

  10. 12、Nginx代理缓存服务

    通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后端压力,提高网站并发延时 1.缓存常见类型 服务端缓存 代理缓存, 获取服务端内容进行缓存 客户端浏览器缓存 Nginx代理缓存原理 ...