导入excel表格时,先将表格保存在本地,保存本次上传的excel文件到本地目录:
/**
* 保存excel文件到本地
* @param request
* @param userId
* @param file excel文件
*/
public static File saveExcelToLocal(HttpServletRequest request, Integer userId, MultipartFile file) {
File newFile=null;
try {
File newFileDir = new File(request.getServletContext().getRealPath("/") + "upload/");
newFileDir.mkdirs();
newFile = new File(request.getServletContext().getRealPath("/") + "upload/" + userId + "_" + System.currentTimeMillis() + file.getOriginalFilename());
FileOutputStream os = null;
os = new FileOutputStream(newFile);
os.write(file.getBytes());
os.close();
log.info("本次上传的excel文件,暂时保存在: {}", newFile.getAbsolutePath());
file.transferTo(newFile);
} catch (IOException e) {
e.printStackTrace();
}
return newFile;
}