文件上传功能有很多种写法,但是个人认为最实用的,还是这种:
List files = ((MultipartHttpServletRequest) request).getFiles("filess");
MultipartFile file = null;
for (int i = 0; i < files.size(); ++i) {
file = files.get(i);
byte[] bytes = file.getBytes();
File businessLicenseFile = new File("F:/"+id+".jpg");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(businessLicenseFile));
bufferedOutputStream.write(bytes);
bufferedOutputStream.close();
}
filess:传输给前端的上传文件字段。
适用于单个文件和多个文件上传