导入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;
}

这段代码实现了从HttpServletRequest中接收MultipartFile类型的Excel文件,并将其保存到本地指定目录。文件名由用户ID和系统时间戳组成,确保唯一性。保存过程中使用了FileOutputStream写入文件内容,最后关闭流并返回保存的文件对象。
1万+

被折叠的 条评论
为什么被折叠?



