DEMO流程
- 传入一个需要下载并上传的url地址
- 下载文件
- 上传文件并返回OSS的url地址
springboot pom文件依赖
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.15version>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>springboot-rocketmqartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springboot-demoname>
<description>springboot-demodescription>
<properties>
<java.version>11java.version>
<rocketmq-client-java-version>5.1.3rocketmq-client-java-version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.13version>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.8.22version>
dependency>
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>3.13.2version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
application.yml 文件配置
ali:
oss:
end-point:
access-key-id:
access-key-secret:
bucket-name:
ali-url: https://${ali.oss.bucket-name}.${ali.oss.end-point}/
FileUtil 工具类
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import java.io.File;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileUtil {
private static final String projectUrl = System.getProperty("user.dir").replaceAll("\\\\", "/");
public static void deleteFiles(String path) {
File file = new File(path);
if (file.exists()) {
if (file.isDirectory()) {
File[] temp = file.listFiles();
for (File value : temp) {
deleteFile(value.getAbsolutePath());
}
} else {
file.delete();
}
file.delete();
}
}
public static void deleteFile(String path){
File dest = new File(path);
if (dest.isFile() && dest.exists()) {
dest.delete();
}
}
public static String getNewFileRootPath(){
return projectUrl+File.separator+ IdUtil.simpleUUID();
}
public static String getFileNameFromUrl(String url) {
Pattern pattern = Pattern.compile("[^/]*$");
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
return matcher.group();
}
return "";
}
public static String getExtName(String urlPath) {
String fileName = getFileNameFromUrl(urlPath);
return FileNameUtil.extName(fileName);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
请求配置 RestTemplateConfig
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory) {
return new RestTemplate(requestFactory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(10000);
factory.setConnectTimeout(10000);
factory.setHttpClient(httpClient());
return factory;
}
@Bean
public HttpClient httpClient() {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(500);
connectionManager.setDefaultMaxPerRoute(200);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(20000)
.setConnectTimeout(10000)
.setConnectionRequestTimeout(1000)
.build();
return HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connectionManager)
.build();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
阿里组件配置
读取配置类 AliOssProperties
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "ali.oss")
@Data
public class AliOssProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private String aliUrl;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
OSS组件类 OssComponent
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
@Component
@Slf4j
@Getter
public class OssComponent {
@Resource
private AliOssProperties aliOssProperties;
public String uploadFile(String fileDir, InputStream inputStream, String fileName) {
try {
this.uploadFile2Oss(fileDir, inputStream, fileName);
String url = getFileUrl(fileDir, fileName);
if (url != null && url.length() > 0) {
return url;
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("获取路径失败");
}
return "";
}
public String getFileUrl(String fileDir, String fileUrl) {
if (fileUrl != null && fileUrl.length() > 0) {
String[] split = fileUrl.replaceAll("\\\\","/").split("/");
String url = aliOssProperties.getAliUrl() + fileDir + split[split.length - 1];
return Objects.requireNonNull(url);
}
return null;
}
public boolean deleteFile(String fileDir, String fileName) {
OSS ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());
ossClient.deleteObject(aliOssProperties.getBucketName(), fileDir + fileName);
boolean found = ossClient.doesObjectExist(aliOssProperties.getBucketName(), fileDir + fileName);
return !found;
}
private String getShortUrl(String url) {
String[] imgUrls = url.split("\\?");
return imgUrls[0].trim();
}
private void uploadFile2Oss(String fileDir, InputStream inputStream, String fileName) {
OSS ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());
String ret;
try {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(inputStream.available());
objectMetadata.setCacheControl("no-cache");
objectMetadata.setHeader("Pragma", "no-cache");
String contentType = getContentType(fileName.substring(fileName.lastIndexOf(".")));
if(StrUtil.isNotEmpty(contentType)){
objectMetadata.setContentType(contentType);
}
objectMetadata.setContentDisposition("inline;filename=" + fileName);
PutObjectResult putResult = ossClient.putObject(aliOssProperties.getBucketName(), fileDir + fileName, inputStream, objectMetadata);
ret = putResult.getETag();
if (StrUtil.isEmpty(ret)) {
log.error("上传失败,文件ETag为空");
}
ossClient.shutdown();
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String getContentType(String filenameExtension) {
if (FileNameSuffixEnum.BMP.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "image/bmp";
}
if (FileNameSuffixEnum.GIF.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "image/gif";
}
if (FileNameSuffixEnum.JPEG.getSuffix().equalsIgnoreCase(filenameExtension) ||
FileNameSuffixEnum.JPG.getSuffix().equalsIgnoreCase(filenameExtension) ||
FileNameSuffixEnum.PNG.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "image/jpeg";
}
if (FileNameSuffixEnum.HTML.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "text/html";
}
if (FileNameSuffixEnum.TXT.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "text/plain";
}
if (FileNameSuffixEnum.VSD.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "application/vnd.visio";
}
if (FileNameSuffixEnum.PPTX.getSuffix().equalsIgnoreCase(filenameExtension) ||
FileNameSuffixEnum.PPT.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "application/vnd.ms-powerpoint";
}
if (FileNameSuffixEnum.DOCX.getSuffix().equalsIgnoreCase(filenameExtension) ||
FileNameSuffixEnum.DOC.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "application/msword";
}
if (FileNameSuffixEnum.XML.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "text/xml";
}
if (FileNameSuffixEnum.PDF.getSuffix().equalsIgnoreCase(filenameExtension)) {
return "application/pdf";
}
return "";
}
}
@Getter
enum FileNameSuffixEnum {
BMP(".bmp", "bmp文件"),
GIF(".gif", "gif文件"),
JPEG(".jpeg", "jpeg文件"),
JPG(".jpg", "jpg文件"),
PNG(".png", "png文件"),
HTML(".html", "HTML文件"),
TXT(".txt", "txt文件"),
VSD(".vsd", "vsd文件"),
PPTX(".pptx", "PPTX文件"),
DOCX(".docx", "DOCX文件"),
PPT(".ppt", "PPT文件"),
DOC(".doc", "DOC文件"),
XML(".xml", "XML文件"),
PDF(".pdf", "PDF文件");
private final String suffix;
private final String description;
FileNameSuffixEnum(String suffix, String description) {
this.suffix = suffix;
this.description = description;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
文件服务
FileService
public interface FileService {
String uploadJavaVideo(String url) throws Exception;
}
FileServiceImpl
import cn.hutool.core.util.IdUtil;
import com.example.springbootrocketmq.config.OssComponent;
import com.example.springbootrocketmq.service.FileService;
import com.example.springbootrocketmq.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
@Slf4j
@Service
public class FileServiceImpl implements FileService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private OssComponent ossComponent;
@Override
public String uploadJavaVideo(String url) throws Exception {
String extName = FileUtil.getExtName(url);
String fileName = IdUtil.simpleUUID()+"."+extName;
log.info("fileName:{}",fileName);
String newFileRootPath = FileUtil.getNewFileRootPath();
File rootFile = new File(newFileRootPath);
if(!rootFile.exists()){
rootFile.mkdirs();
}
String toPath = newFileRootPath+ File.separator + fileName;
try {
log.info("toPath:{}",toPath);
uploadBigFile(url,toPath);
return ossComponent.uploadFile("demo/",new FileInputStream(toPath),fileName);
} finally {
FileUtil.deleteFiles(newFileRootPath);
}
}
public void uploadBigFile(String url, String toPath) throws Exception {
RequestCallback requestCallback = request -> request.getHeaders()
.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
restTemplate.execute(url, HttpMethod.GET, requestCallback, clientHttpResponse -> {
Files.copy(clientHttpResponse.getBody(), Paths.get(toPath));
return null;
});
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
测试 TestController 类
import com.example.springbootrocketmq.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/")
public class TestController {
@Autowired
private FileService fileService;
@GetMapping("/uploadFileToOss")
public Object uploadJavaVideo(String url) {
try {
return fileService.uploadJavaVideo(url);
}catch (Exception e){
log.error("上传转码异常,异常原因e:{}",e);
}
return null;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
启动服务 用 postman 请求