企业级spring-boot案例-Spring Boot 上传文件(图片)

企业级spring-boot案例系列文章上线了,涵盖了大部分企业级的spring-boot使用场景,会不定期进行更新,企业级spring-boot案例源码地址:https://gitee.com/JourWon/spring-boot-example,欢迎各位大佬一起学习和指正

网站上传图片、文件等,常见操作是直接上传到服务器的webapp目录下,或者直接上传服务的一个指定的文件夹下面。这种方式对于简单的单机应用确实是很方便、简单,出现的问题也会比较少。但是对于分布式项目,直接上传到项目路径的方式显然是不可靠的,而且随着业务量的增加,文件也会增加,对服务器的压力自然就增加了。这里简单的介绍常见的几种上传图片、文件的方式。

  1. 直接上传到指定的服务器路径;
  2. 上传到第三方内容存储器,比如将图片保存到阿里云OSS;
  3. 自己搭建文件存储服务器,如:FastDFS,FTP服务器等

本文主要讲最简单的方式,即上传文件或者图片到服务器的一个指定的文件夹下面,项目结构如下图

在这里插入图片描述

1. 添加依赖

<dependencies>
    <!-- Knife4j-API接口文档 -->
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
    </dependency>

    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- springboot相关 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2. Spring配置

application.yml配置文件添加如下配置

# spring配置
spring:
  application:
    # 应用名称
    name: spring-boot-file-upload
  servlet:
    multipart:
      # 单个文件所能上传的文件大小
      max-file-size: 1MB
      # 单次请求所能上传文件的总文件大小
      max-request-size: 10MB

3. 添加Knife4j配置类

@EnableKnife4j
@Configuration
public class Knife4jConfig {

    /**
     * 创建Docket对象
     *
     * @return Docket
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * API基础信息
     *
     * @return ApiInfo
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Knife4j-API接口文档")
                .description("API接口文档")
                .contact(new Contact("JourWon", "https://thinkwon.blog.csdn.net/", "JourWon@163.com"))
                .version("1.0.0")
                .build();
    }

}

4. 添加枚举与实体类

4.1 响应编码枚举

@Getter
@AllArgsConstructor
public enum CommonResponseCodeEnum {

    /**
     * 成功
     */
    SUCCESS("00000", "成功"),

    /**
     * 用户请求参数错误
     */
    REQUEST_PARAMETER_ILLEGAL("A0400", "用户请求参数错误"),
    /**
     * 访问未授权
     */
    UNAUTHORIZED_ACCESS("A0301", "访问未授权"),
    /**
     * 不支持当前请求类型
     */
    NONSUPPORT_REQUEST_TYPE("A0444", "不支持当前请求类型"),
    /**
     * 用户id不存在
     */
    USER_ID_NOT_EXIST("A0445", "用户id不存在"),
    /**
     * 数据库字段重复
     */
    DATABSE_FIELD_DUPLICATE("A0446", "数据库字段重复"),

    /**
     * 系统执行出错
     */
    SYSTEM_EXCEPTION("B0001", "系统执行出错"),

    /**
     * 系统执行超时
     */
    SYSTEM_EXECUTION_TIMEOUT("B0100", "系统执行超时"),
    ;

    /**
     * 响应编码
     */
    private final String code;

    /**
     * 响应信息
     */
    private final String message;

}

4.2 上传文件信息

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UploadFile {

    /**
     * 文件名
     */
    private String fileName;

    /**
     * 文件url
     */
    private String url;

}

4.3 统一返回前端的响应对象

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "CommonResponse-统一返回前端的响应对象")
public class CommonResponse<T> implements Serializable {

    private static final long serialVersionUID = -1338376281028943181L;

    /**
     * MDC_KEY
     */
    public static final String MDC_KEY = "traceId";

    @ApiModelProperty(value = "响应编码")
    private String code;

    @ApiModelProperty(value = "响应信息")
    private String message;

    @ApiModelProperty(value = "业务数据")
    private T data;

    @ApiModelProperty(value = "traceId")
    private String traceId = MDC.get(MDC_KEY);

    @ApiModelProperty(value = "响应日期时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    private LocalDateTime localDateTime = LocalDateTime.now();

    public CommonResponse(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public CommonResponse(CommonResponseCodeEnum commonResponseCodeEnum) {
        this.code = commonResponseCodeEnum.getCode();
        this.message = commonResponseCodeEnum.getMessage();
    }

    public CommonResponse(T data) {
        this.code = CommonResponseCodeEnum.SUCCESS.getCode();
        this.message = CommonResponseCodeEnum.SUCCESS.getMessage();
        this.data = data;
    }

    public CommonResponse(CommonResponseCodeEnum commonResponseCodeEnum, T data) {
        this.code = commonResponseCodeEnum.getCode();
        this.message = commonResponseCodeEnum.getMessage();
        this.data = data;
    }

    public static <T> CommonResponse<T> success() {
        return new CommonResponse<>(CommonResponseCodeEnum.SUCCESS);
    }

    public static <T> CommonResponse<T> success(String message) {
        return new CommonResponse<>(CommonResponseCodeEnum.SUCCESS.getCode(), message);
    }

    public static <T> CommonResponse<T> success(T data) {
        return new CommonResponse<>(CommonResponseCodeEnum.SUCCESS, data);
    }

    public static <T> CommonResponse<T> success(CommonResponseCodeEnum commonResponseCodeEnum, T data) {
        return new CommonResponse<>(commonResponseCodeEnum, data);
    }

    public static <T> CommonResponse<T> failure(CommonResponseCodeEnum commonResponseCodeEnum) {
        return new CommonResponse<>(commonResponseCodeEnum);
    }

    public static <T> CommonResponse<T> failure(CommonResponseCodeEnum commonResponseCodeEnum, T data) {
        return new CommonResponse<>(commonResponseCodeEnum, data);
    }

}

5. 文件上传接口与实现类

5.1 文件上传接口

public interface FileStorageService {

    /**
     * 初始化方法,创建文件夹
     */
    void init();

    /**
     * 保存文件
     *
     * @param multipartFile
     */
    void save(MultipartFile multipartFile);

    /**
     * 根据文件名加载文件
     *
     * @param filename
     * @return
     */
    Resource load(String filename);

    /**
     * 加载所有的文件
     *
     * @return
     */
    Stream<Path> load();

    /**
     * 递归删除文件
     */
    void clear();

}

5.2 文件上传接口实现类

@Service
public class FileStorageServiceImpl implements FileStorageService {

    private final Path path = Paths.get("fileStorage");

    @Override
    public void init() {
        try {
            if (!Files.exists(path)) {
                Files.createDirectory(path);
            }
        } catch (IOException e) {
            throw new RuntimeException("Could not initialize folder for upload!");
        }
    }

    @Override
    public void save(MultipartFile multipartFile) {
        try {
            Files.copy(multipartFile.getInputStream(), this.path.resolve(multipartFile.getOriginalFilename()));
        } catch (IOException e) {
            throw new RuntimeException("Could not store the file. Error:" + e.getMessage());
        }
    }

    @Override
    public Resource load(String filename) {
        Path file = path.resolve(filename);
        try {
            Resource resource = new UrlResource(file.toUri());
            if (resource.exists() || resource.isReadable()) {
                return resource;
            } else {
                throw new RuntimeException("Could not read the file.");
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException("Error:" + e.getMessage());
        }
    }

    @Override
    public Stream<Path> load() {
        try {
            return Files.walk(this.path, 1)
                    .filter(path -> !path.equals(this.path))
                    .map(this.path::relativize);
        } catch (IOException e) {
            throw new RuntimeException("Could not load the files.");
        }
    }

    @Override
    public void clear() {
        FileSystemUtils.deleteRecursively(path.toFile());
    }

}

6. 初始化文件存储空间

@Configuration
public class FileUploadRunner implements CommandLineRunner {

    @Resource
    FileStorageService fileStorageService;

    @Override
    public void run(String... args) {
        // 项目启动时删除文件夹里面的文件
        // fileStorageService.clear();
        // 创建文件夹
        fileStorageService.init();
    }

}

7. 文件上传控制器

@Slf4j
@RestController
@RequestMapping("/file")
@Api(value = "文件控制器")
public class FileUploadController {

    @javax.annotation.Resource
    FileStorageService fileStorageService;

    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public CommonResponse upload(@RequestParam("file") MultipartFile file) {
        try {
            fileStorageService.save(file);
            return CommonResponse.success("Upload file successfully: " + file.getOriginalFilename());
        } catch (Exception e) {
            return CommonResponse.failure(CommonResponseCodeEnum.SYSTEM_EXCEPTION);
        }
    }

    @GetMapping("/list")
    @ApiOperation("获取文件列表")
    public CommonResponse<List<UploadFile>> files(HttpServletResponse response) {
        List<UploadFile> files = fileStorageService.load()
                .map(path -> {
                    String fileName = path.getFileName().toString();
                    String url = MvcUriComponentsBuilder
                            .fromMethodName(FileUploadController.class,
                                    "getFile",
                                    path.getFileName().toString(), response
                            ).build().toString();
                    return new UploadFile(fileName, url);
                }).collect(Collectors.toList());

        return CommonResponse.success(files);
    }

    @GetMapping("/{filename:.+}")
    @ApiOperation("下载文件")
    public ResponseEntity<Resource> getFile(@PathVariable("filename") String filename, HttpServletResponse response) throws UnsupportedEncodingException {
        Resource file = fileStorageService.load(filename);
        String fileName = file.getFilename();

        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        // 这里URLEncoder.encode可以防止中文乱码
        String fn = URLEncoder.encode(Objects.requireNonNull(fileName), StandardCharsets.UTF_8.name());

        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment;filename=" + fn)
                .body(file);
    }

}

8. 启动类

@SpringBootApplication
public class SpringBootFileUploadApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootFileUploadApplication.class, args);
    }

}

通过postman可以验证上面文件上传控制器的三个接口

  • 12
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值