文件本地和OSS上传
这里写目录标题
- 前端传出文件
- 后端本地存储
- 阿里云OSS存储
- 上传Demo
- 实现上传
- @ConfigurationProperties
前端传出文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传文件</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
姓名: <input type="text" name="username"><br>
年龄: <input type="text" name="age"><br>
头像: <input type="file" name="image"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
后端本地存储
import org.springframework.web.multipart.MultipartFile;
@PostMapping(value = "/upload")
public String upload(String username, Integer age,@RequestParam("image") MultipartFile file) throws IOException {
log.info("文件上传:{},{},{}",username,age,file);
String name=file.getOriginalFilename();
file.transferTo(new File("D:\\images\\"+name)); //存入本地
return "success";
}
阿里云OSS存储
存储步骤
查找 Endpoint 地址
上传Demo
import java.io.*;
import com.aliyun.oss.*;
public class Demo {
public static void main(String[] args) {
// 设置 OSS Endpoint 地址
String endpoint = "https://oss-cn-beijing.aliyuncs.com";
String accessKeyId = "LTAI5tHKDa7NFXnaHWrUkmNs";
String accessKeySecret = "aeQsl73MlNGDZ2Q3QD7cxES7zt5YVb";
String bucketName = "k92q-b"; //bucket名字
//上传后的名称
String objectName="狗.jpg";
//上传的文件地址
String filePatn="C:\\Users\\DELL\\Pictures\\头像背景\\狗.jpg";
OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
InputStream inputStream= null;
try {
inputStream = new FileInputStream(filePatn);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
ossClient.putObject(bucketName, objectName, inputStream);
System.out.println("2. 文件 " + objectName + " 上传成功。");
}
}
实现上传
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.UUID;
/**
* 阿里云 OSS 工具类
*/
@Component
public class AliOSSUtils {
private String endpoint = "https://oss-cn-beijing.aliyuncs.com";
private String accessKeyId = "LTAI5tHKDa7NFXnaHWrUkmNs";
private String accessKeySecret = "aeQsl73MlNGDZ2Q3QD7cxES7zt5YVb";
private String bucketName = "k92q-b";
/**
* 实现上传图片到OSS
*/
public String upload(MultipartFile file) throws IOException {
// 获取上传的文件的输入流
InputStream inputStream = file.getInputStream();
// 避免文件覆盖
String originalFilename = file.getOriginalFilename();
String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//上传文件到 OSS
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, fileName, inputStream);
//文件访问路径
String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
// 关闭ossClient
ossClient.shutdown();
return url;// 把上传到oss的路径返回
}
}
接口部分
@PostMapping(value = "/upload")
public String upload(String username,Integer age,@RequestParam("image") MultipartFile file){
String url;
try {
url=aliOSSUtils.upload(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("成功");
return url;
}
@ConfigurationProperties
为了方便修改配置项中的属性
@Component
@Data //lombok
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSUtils{
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
aliyun:
oss:
endpoint: https://oss-cn-beijing.aliyuncs.com
accessKeyId: LTAI5tHKDa7NFXnaHWrUkmNs
accessKeySecret: aeQsl73MlNGDZ2Q3QD7cxES7zt5YVb
bucketName: k92q-b