java MultipartFile多文件上传/通过文件夹解析所有文件
MultipartFile多文件:
public Boolean upload(MultipartFile[] files) throws Exception {
if (null != files && files.length > 0) {
//用于判断文件个数
final int length = files.length;
for (MultipartFile file : files) {
final String originalFilename = file.getOriginalFilename();
assert originalFilename != null;
//后缀,用于判断文件类型
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
//获取输文件入流
final InputStream inputStream = file.getInputStream();
//添加逻辑操作...
}
}
return true;
}
通过文件夹上传:
public Boolean uploadByDirectory(String path) throws Exception { File file = new File(path); File[] tempList = file.listFiles(); //遍历文件夹内文件 for (int i = 0; i < Objects.requireNonNull(tempList).length; i++) { File excel = tempList[i]; FileInputStream fileInputStream = null; final String fileName = excel.getName(); //判断后缀是什么类型 String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); //创建文件输入流 fileInputStream = new FileInputStream(excel); //执行操作.... } return true; }