EXCEL上传
之前一直都是听别人说上传,自己没做过这块,正好刚做完从头到尾捋一遍,有需要的可以借鉴。
我用的是spring cloud项目,中间涉及到项目之间的feign调用,被调用的称为内层controller,调用方称为外层controller。
为了方便整理,查找,我会根据流程写两篇博客。
操作流程:
1.把EXCEL上传到服务器,获取文件路径。
2.插入表中一条数据,指明文件路径。
3.定时器搜索是否有带解析的EXCEL。==》解析
外层controller:
a:要记得你的value = "file"的file要跟前端的参数名称保持一致
@ApiOperation(value = "上传批量导入数据源文件到服务器")
@RequestMapping(value = "/importData/fileUpload",method = RequestMethod.POST)
public String fileUpload(@RequestParam(value = "file") MultipartFile file,
HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String, Object> jsonMap = new HashMap<String, Object>();
if(file.isEmpty()){
jsonMap.put("err", 9999);
jsonMap.put("desc", "内部错误:参数错误");
}else{
jsonMap = importDataSourceService.fileUpload(file);
}
JSONObject jsonObject = new JSONObject(jsonMap);
log.info("/home/webapp/upload/hjcfile/ : " + jsonObject.toString());
HttpUtils.jsonOutPrint(jsonObject, request, response);
return null;
}
feign调用的接口:
划重点啦:
a:看好注解用的是@RequestPart,文件上传用的注解。(详解百度搜一下)
b:consumes = MediaType.MULTIPART_FORM_DATA_VALUE,Feign实现微服务间文件上传要用的。
@RequestMapping(value = "/importData/fileUpload",method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Map<String, Object> fileUpload(@RequestPart("file") MultipartFile file) throws IOException;
内层controller:
@ApiOperation(value = "上传批量导入数据源文件到服务器")
@RequestMapping(value = "/importData/fileUpload",method = RequestMethod.POST)
public Map<String, Object> fileUpload(@RequestPart("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String, Object> jsonMap = new HashMap<String, Object>();
if(file.isEmpty()){
jsonMap.put("err", 9999);
jsonMap.put("desc", "内部错误:参数错误");
}else{
try {
// 文件保存路径(为了方便测试,现在是将文件保存到本地)
String uploadPath = request.getSession().getServletContext().getRealPath("\\") + "resources\\platform";
String filePathName = importDataSourceService.fileUpload(file,uploadPath);
if(org.apache.commons.lang.StringUtils.isEmpty (filePathName)){
jsonMap.put("err", 0);
jsonMap.put("desc", "文件上传失败");
}
else{
jsonMap.put("err", 0);
jsonMap.put("desc", "文件上传成功");
jsonMap.put("filePathName", filePathName);
}
} catch (Exception e) {
jsonMap.put("err", 9000);
jsonMap.put("desc", e.getMessage ());
}
}
JSONObject jsonObject = new JSONObject(jsonMap);
return jsonMap;
}
内层serviceImpl
public String fileUpload(MultipartFile file, String uploadPath) throws IOException {
if(file.isEmpty()){
throw new RuntimeException("fileUpload内部错误:参数缺失");
}
else{
if (!file.isEmpty()) {
try {
// 文件保存路径
String dateStr = DateUtil.formatDate(System.currentTimeMillis(), "yyyyMMddHHmmss");
//文件原来的文件名
String originalFilename =file.getOriginalFilename();
//防止重名加了时间戳
String fileName = originalFilename.substring (0 , originalFilename.length ()-5)+dateStr+".xlsx";
File uploadDir=new File(uploadPath);
if(!uploadDir.exists()){
uploadDir.mkdirs();
}
File uploadFile=new File(uploadPath+"\\"+fileName);
file.transferTo(uploadFile);//上传
String filePathName =uploadPath+"\\"+fileName;
return filePathName;
} catch (Exception e) {
throw new RuntimeException("上传文件失败");
}
}
}
return null;
}