Starter上传文件

Starter上传文件Starter上传文件_第1张图片

FileResult

package com.zhanggm.starter.uploader.common;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class FileResult {
     
    private String fileName;
    private String fileUrl;

}

FileService

package com.zhanggm.starter.uploader.service;

import com.zhanggm.common.utils.FileUtil;
import com.zhanggm.starter.uploader.FileProperties;
import com.zhanggm.starter.uploader.common.FileResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 文件上传的服务类
 */
public class FileService {
     

    private FileProperties fileProperties;

    public FileService(FileProperties properties) {
     
        this.fileProperties = properties;
    }

    public List<FileResult> upload(MultipartFile[] files){
     
        //文件上传信息
        List<FileResult> fileList = new ArrayList<>();
        //循环保存文件
        for (MultipartFile file : files){
     
            //生成文件名
            String originalFilename = file.getOriginalFilename();
            String extName = FileUtil.getExtName(originalFilename);
            String fileName = UUID.randomUUID()+extName;
            //保存文件
            File saveFile = new File(fileProperties.getPath(),fileName);
            try {
     
                file.transferTo(saveFile);
                //添加到文件上传信息到list
                FileResult fileResult = new FileResult(fileName, fileProperties.getDomain()+fileName);
                fileList.add(fileResult);
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }
        return fileList;
    }
}


Starter上传文件_第2张图片

FileAutoConfiguration

package com.zhanggm.starter.uploader;

import com.zhanggm.starter.uploader.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(FileProperties.class)
public class FileAutoConfiguration {
     

    @Autowired
    private FileProperties fileProperties;

    @Bean
    @ConditionalOnProperty(prefix = "file",value = "enable",havingValue = "true")
    public FileService fileService(){
     
        return new FileService(fileProperties);
    }
}

FileProperties

package com.zhanggm.starter.uploader;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "file")
@Getter
@Setter
public class FileProperties {
     
    private String path;
    private String domain;
    private boolean enable;
}

在这里插入图片描述

pom.xml

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zhanggm.starter.uploader.FileAutoConfiguration

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhanggm.starter.uploader</groupId>
    <artifactId>zhanggm-uploader-springboot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zhanggm-uploader-springboot-starter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- 自动配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <!--配置处理-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--spring-webmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.4.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <!--spring-lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--zhanggm-common-utils工具类工程-->
        <dependency>
            <groupId>com.zhanggm.common</groupId>
            <artifactId>zhanggm-common-utils</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

你可能感兴趣的:(java)