Java多张图片上传

Java多张图片上传

之前在做多张图片上传是在网上找了很多,但是都太复杂,总是出错,所以本人写了一个比叫简单的上传代码,和大家分享也为以后自己需要的时候参考:
这里上传代码会自动转为页面所以我截图保存
index.xml
Java多张图片上传

Java controller层
/**
* 实名认证
*
* @param name 姓名
* @param identityId 身份证
* @return
*/
@PostMapping("/certification")
@ApiOperation(“实名认证”)
public BaseResponse uploadPicture(@NotEmpty List files, String name, String identityId) {
for (MultipartFile file : files) {
if (Objects.nonNull(file)) { // 判断对象是否为空
OutputStream os = null;
InputStream is = null;
String fileName = null;
try {
// 获取MultipartFile的输入流
is = file.getInputStream();
// 获取文件名
fileName = file.getOriginalFilename();
} catch (IOException e) {
e.printStackTrace();
}
try {
String path = “D:\\bbb\”; // 设置保存地址
// 保存路径
File filePath = new File(path);
if (!filePath.exists()) { // 判断文件是否存在
filePath.mkdirs(); // 检测文件夹是否存在,如果不存在就创建
}
// 把图片保存到指定路径
os = new FileOutputStream(filePath.getPath() + File.separator + fileName);
byte[] bs = new byte[1024];
int len;
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
os.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
这两个能基本满足多张图片的上传