本文基于
①3.7.0 oss包,使用oss上传文件功能
②没有oss服务,可以使用服务本地上传
在application.yml文件配置
oss:
endpoint: oss-cn-beijing.aliyuncs.com/
accessKeyID:
accessKeySecret:
bucketName:
filePath: picture/
file:
#path: /usr/local/apache-tomcat-9.0.30/
path: C:\Users\Administrator\Desktop\
tomcatPicPath: picture/
controller层代码
package com.example.boot.ztestModule.controller;
import com.example.boot.framework.Result;
import com.example.boot.utils.oss.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* @author : KimWu
* @date : 2020-03-27 12:34
* @description:
*/
@RequestMapping("/file")
@RestController
@Slf4j
public class FileController {
@RequestMapping("/ossAdd.do")
public Result add(MultipartFile file) {
String ossFilePath = FileUtils.ossAdd(file);
return Result.success(ossFilePath);
}
@RequestMapping("/localAdd.do")
public Result localAdd(MultipartFile file) {
String ossFilePath = FileUtils.localAdd(file);
return Result.success(ossFilePath);
}
}
工具类代码
package com.example.boot.utils.oss;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.example.boot.framework.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* @author : KimWu
* @date : 2020-03-27 13:52
* @description:
*/
@Component
@Slf4j
public class FileUtils {
private static String endpoint;
@Value("${oss.endpoint}")
public void setEndpoint(String endpoint) {
FileUtils.endpoint = endpoint;
}
private static String accessKeyId;
@Value("${oss.accessKeyID}")
public void setAccessKeyId(String accessKeyId) {
FileUtils.accessKeyId = accessKeyId;
}
private static String accessKeySecret;
@Value("${oss.accessKeySecret}")
public void setAccessKeySecret(String accessKeySecret) {
FileUtils.accessKeySecret = accessKeySecret;
}
private static String bucketName;
@Value("${oss.bucketName}")
public void setBucketName(String bucketName) {
FileUtils.bucketName = bucketName;
}
private static String ossFilePath;
@Value("${oss.filePath}")
public void setOssFilePath(String ossFilePath) {
FileUtils.ossFilePath = ossFilePath;
}
private static String linuxFilePath;
@Value("${file.path}")
public void setLinuxFilePath(String linuxFilePath) {
FileUtils.linuxFilePath = linuxFilePath;
}
private static String tomcatPicPath;
@Value("${file.tomcatPicPath}")
public void setTomcatPicPath(String tomcatPicPath) {
FileUtils.tomcatPicPath = tomcatPicPath;
}
public static String ossAdd(MultipartFile file) {
String ossUrlPath = "";
OSS ossClient = null;
try {
String name = getFileName(file);
//上传OSS
// 创建OSSClient实例
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//判断存储空间是否存在
boolean exists = ossClient.doesBucketExist(bucketName);
if (!exists) {
throw new BusinessException("bucketName is not exists");
} else {
String objectName = ossFilePath + name;
InputStream inputStream = file.getInputStream();
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
ossClient.putObject(putObjectRequest);
ossUrlPath = "https://" + bucketName + "." + endpoint + objectName;
}
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("upload error", e);
}
throw new BusinessException("上传失败");
} finally {
// 关闭OSSClient
ossClient.shutdown();
}
return ossUrlPath;
}
public static String localAdd(MultipartFile file) {
String name = getFileName(file);
String allPath = linuxFilePath + tomcatPicPath;
String path = new File(StrUtil.builder(allPath, name).toString()).getAbsolutePath();
FileUtil.mkParentDirs(path);
try {
file.transferTo(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
return tomcatPicPath + name;
}
private static String getFileName(MultipartFile file) {
String originalFilename = file.getOriginalFilename();
String name = StrUtil.builder(IdUtil.simpleUUID(), StrUtil.DOT,
StrUtil.subAfter(originalFilename, StrUtil.DOT, true)).toString();
return name;
}
}
在服务器上便可以查看到上传的文件,接口中返回的是图片oss地址,可以直接访问或存储。
关注公众号
每周会更新干货知识