Skip to content
章节导航

文件上传

依赖引入

xml
<dependency>
    <groupId>com.github.itdachen</groupId>
    <artifactId>oss-spring-boot-starter</artifactId>
    <version>[最新版本]</version>
</dependency>

配置

文件上传目前支持本地上传和阿里云上传,通过 fly.oss.type 配置,通过工厂模式选择文件上传服务器。

yaml
fly:
  oss:
    ## 文件上传类型: LOCAL-本地服务存储; ALI-阿里云存储服务器, 默认是 LOCAL
    type: LOCAL
    local:
      localHttp: http://127.0.0.1:8080/fly
      diskFolder: D:/upload/
    ali:
      endpoint: oss-cn-chengdu.aliyuncs.com
      access-key-id: 123
      access-key-secret: 123
      bucket: itdachen

文件上传接口

java
import com.github.itdachen.boot.oss.FileHelper;
import com.github.itdachen.boot.oss.entity.FileInfo;
import com.github.itdachen.framework.core.response.ServerResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
 
@Controller
@RequestMapping("/oss")
public class OssCloudController {

    private final FileHelper fileHelper;

    public OssCloudController(FileHelper fileHelper) {
        this.fileHelper = fileHelper;
    }


    /***
     * 文件上传
     */
    @PostMapping("/upload")
    @ResponseBody
    public ServerResponse<FileInfo> upload(@RequestParam("file") MultipartFile file) throws Exception {
        return ServerResponse.ok(fileHelper.upload(file));
    }
 
}