springmvc文件上传 参数为MultipartFile 转换为File
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的更多相关文章
- SpringMVC文件上传下载(单文件、多文件)
前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- 18 SpringMVC 文件上传和异常处理
1.文件上传的必要前提 (1)form 表单的 enctype 取值必须是:multipart/form-data(默认值是:application/x-www-form-urlencoded) en ...
- springmvc文件上传AND jwt身份验证
SpringMVC文件上传 思路:1.首先定义页面,定义多功能表单(enctype=“multipart/form-data”)2.在Controller里面定义一个方法,用参数(MultipartF ...
- TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式
1.传统文件上传方式 <!-- 文件上传需要的jar --> <dependency> <groupId>commons-fileupload</groupI ...
- springmvc文件上传下载简单实现案例(ssm框架使用)
springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...
- springMVC文件上传大小超过限制的问题
[转自]https://my.oschina.net/ironwill/blog/646762 springMVC是一个非常方便的web层框架,我们使用它的文件上传也非常的方便. 我们通过下面的配置来 ...
- 解决springMVC文件上传报错: The current request is not a multipart request
转自:https://blog.csdn.net/HaHa_Sir/article/details/79131607 解决springMVC文件上传报错: The current request is ...
- SpringMVC——文件上传下载
一.单文件上传 1.导入依赖 <dependency> <groupId>commons-io</groupId> <artifactId>common ...
随机推荐
- win10上面快捷方式如何放到桌面上
1.win+I 快捷方式调出windows设置 ,点击个性化. 2.进入个性化设置界面,点击主题 3.进入主题设置后,点击右边的相关的设置下的桌面图标设置 4.在弹出的窗口中,将我们希望看到 ...
- java笔记web
1,spring请求同,返回同一个界面 Dubbo消费者无法连接到生产者提供的服务?内网IP? https://blog.csdn.net/xlgen157387/article/details/52 ...
- linux下测试web访问及网络相关的命令
curl命令 curl是linux系统命令行下用来简单测试web访问的工具. curl -xip:port www.baidu.com -x可以指定ip和端口,省略写hosts,方便实用 -I ...
- 图书管理系统UML建模
图书管理系统UML建模 用例图 借阅者请求服务用例图 图书管理员处理借书还书用例图 系统管理员系统维护用例图 时序图 系统管理员添加书籍时序图 协作图 借阅者预留书籍协作图 状态图 书的状态图 活动图 ...
- shell中字符串操作【转】
转自:http://blog.chinaunix.net/uid-29091195-id-3974751.html 我们所遇到的编程语言中(汇编除外)都少不了字符串处理函数吧,当然shell编程也不例 ...
- auth
谨记:使用的任何框架在网上都会有对应的auth代码,多百度,直接引用插件就好了 tp5 auth 示例:https://blog.csdn.net/strugglm/article/details/7 ...
- android app 闪屏
main activity package com.splash.screen; import android.app.Activity; import android.content.Intent; ...
- SQL truncate 、delete与drop区别(转)
相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DDL语句(数据定义语言),执行后会自动提交. 不同点: 1. t ...
- office visio
画 流程图软件 UML 是否要用做类图.时序图?????
- centos7安装kong和kong-dashboard
1.安装Kong yum install -y https://kong.bintray.com/kong-community-edition-rpm/centos/7/kong-community- ...