| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package com.chuanghai.h3c_reporting.controller;
- import com.chuanghai.h3c_reporting.common.exception.BizCodeEnume;
- import com.chuanghai.h3c_reporting.common.exception.RRException;
- import com.chuanghai.h3c_reporting.common.utils.CommonResult;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.UUID;
- /**
- * @author 27951
- * @version 1.0
- * @description: TODO 文件处理
- * @date 2022/7/27 14:30
- */
- @RestController
- @RequestMapping("/file")
- public class FileController {
- @Value("${file.imgPath}")
- private String imgPath;
- @Value("${file.imgPathDown}")
- private String imgPathDown;
- /**
- * 获取文件路径链接
- */
- @PostMapping("/fileUpDown")
- public CommonResult<String> fileUpDown(@Param("file") MultipartFile file[]) {
- List<MultipartFile> fileList = Arrays.asList(file);
- List<String> files = new ArrayList<>();
- fileList.forEach(file1 -> {
- try {
- //获取文件名
- String fileName = file1.getOriginalFilename();
- //获取文件后缀名。
- assert fileName != null;
- String suffixName = fileName.substring(fileName.lastIndexOf("."));
- //为了避免发生文件替换,这里使用了文件名重新生成
- fileName = UUID.randomUUID() + suffixName;
- // 判断文件后缀名,不符合要求的不保存
- if (suffixName.equals(".jpg") || suffixName.equals(".jpeg") || suffixName.equals(".png")) {
- file1.transferTo(new File(imgPath, fileName));
- files.add(imgPathDown + fileName);
- } else {
- throw new RRException(BizCodeEnume.FILE_NOT);
- }
- } catch (IOException e) {
- throw new RRException(BizCodeEnume.FILE_UPDOWN_FAIL);
- }
- });
- return CommonResult.ok().setResult(files);
- }
- }
|