关于spring:文件上传

31次阅读

共计 821 个字符,预计需要花费 3 分钟才能阅读完成。

    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();}

正文完
 0