设置临时目录
启动时,添加参数,注意,不要加空格!
-Djava.io.tmpdir=./
默认的目录一旦被删除就会报错:
Failed to parse multipart servlet request; nested exception is java.io.IOExc
设置大小限制
默认1M
spring:servlet:multipart:max-file-size: 100MB #单个数据的大小max-request-size: 100MB #总数据的大小
后台接收
controller接收
@PostMapping("/upload")public Result uploadFile(@RequestParam("file") MultipartFile file) throws IOException {if (file.isEmpty()) {return Result.error("文件为空,添加失败");}labelTypeService.addFile(file);return Result.success("添加成功");}
保存文件
# springboot获取项目根目录new ClassPathResource("").getFile().getAbsolutePath()# 保存。file必须为绝对路径,例如:FIle toFile = new File(new File(basePath).getAbsolutePath(),file.getOriginalFilename());file.transferTo(toFile)
readLines
public static List<String> readLines(MultipartFile file) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(file.getInputStream()));String strTmp;ArrayList<String> list = new ArrayList<>();while ((strTmp = bf.readLine()) != null) {if (StringUtils.isNotBlank(strTmp)) {list.add(strTmp);}}return list;}
