文件上传
Posted 朱永胜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件上传相关的知识,希望对你有一定的参考价值。
private void upload(List<MultipartFile> multipartFiles) throws Exception {
for (MultipartFile multipartFile : multipartFiles) {
String fileName = multipartFile.getOriginalFilename();
String filePath = "F:/test";
String fileTotalName = filePath + File.separator + fileName;
File f = new File(fileTotalName);
multipartFile.transferTo(f);
}
}
@PostMapping("upload")
public R upload(HttpServletRequest request) throws Exception {
List<MultipartFile> files = new ArrayList<>();
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
Iterator<String> a = multipartHttpServletRequest.getFileNames();//返回的数量与前端input数量相同, 返回的字符串即为前端input标签的name
while (a.hasNext()) {
String name = a.next();
List<MultipartFile> multipartFiles = multipartHttpServletRequest.getFiles(name);//获取单个input标签上传的文件,可能为多个
files.addAll(multipartFiles);
}
upload(files);
return R.success();
}
以上是关于文件上传的主要内容,如果未能解决你的问题,请参考以下文章
ajaxFileUpload上传带参数文件及JS验证文件大小